为什么我不能使用Primus.connect()?

时间:2016-06-09 03:24:12

标签: node.js socket.io

我在https://github.com/strongloop/strong-pubsub-example下载了一个示例 当我使用Primus.connect(url)时。发生错误:"Primus.connect is not a function"

这是我的pub.js

var PORT = process.env.PORT;
var TOPIC = process.env.TOPIC;

var Client = require('strong-pubsub');
var Adapter = require('strong-pubsub-mqtt');
var readline = require('readline');
var client = new Client({port: PORT}, Adapter);
var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

(function prompt() {
  rl.question('to "' + TOPIC + '": ', function(answer) {
    client.publish(TOPIC, answer);
    prompt();
  });
})();

这是我的sub.js

var PORT = process.env.PORT;
var TOPIC = process.env.TOPIC;

var Client = require('strong-pubsub');
var Adapter = require('strong-pubsub-mqtt');

var client = new Client({port: PORT}, Adapter);

client.subscribe(TOPIC);
var duplex = require('duplex');
var Primus = require('primus');
Primus.Stream = require('stream');

  var server = new Client({port: PORT, host: 'localhost'}, Adapter, {
    createConnection: function(port, host) {
      var connection = duplex();
      var primus = Primus.connect('http://' + host + ':' + port, {
        transformer: 'engine.io',
        parser: 'binary'
      });

      connection.on('_data', function(chunk) {
        // someone called `connection.write(buf)`
        primus.write(chunk);
      });

      primus.on('data', function(chunk) {
        // chunk is an arrayBuffer
        connection._data(toBuffer(chunk));
      });

      primus.on('open', function() {
        connection.emit('connect');
      });

      connection.on('_end', function() {
        primus.end();
        this._end();
      });

      return connection;
    }
  });

client.on('message', function(topic, msg) {
  console.log(msg.toString());
  server.publish(topic,msg);
});

错误

0 个答案:

没有答案