简体   繁体   中英

Why is my connection of a NodeJS server (Sails) to another NodeJS Server (Express4) using Socket.IO failing?

I am trying to connect my Node.JS (written using Sails.JS) app to another Node.JS server (Express4 / Socket.io) using socket.io-client .

My Sails Service app/services/Watcher.js looks like

var client = require('../../node_modules/sails/node_modules/socket.io/node_modules/socket.io-client');

// callback of the form function(socket)
exports.connect = function(callback) {
  sails.log.debug("will connect socket to", sails.config.watcher.uri, "with Socket.io-client version", client.version);
  var socket = client.connect(sails.config.watcher.uri);
  socket.on('connect', function(){
    sails.log.debug("connected");
    socket.on('disconnect', function(){
      sails.log.debug("Disconnected");
    });
    socket.on('error', function(err){
      sails.log.debug("Could not connect", err);
    });
    callback(socket);
  });
};

This is invoked from config/bootstrap.js as follows:

Watcher.connect(function(socket){
  sails.log.debug("Connected watcher to relay with socket", socket);
});

On the Express side my server relay.js is as simple as:

var app = require('express')(),
    http = require('http').Server(app),
    io = require('socket.io').listen(http),
    port = process.env.RELAY_PORT || 8000;

app.get('/', function(req, res) {
  var response = {message: "some response"}; // to be implemented.
  res.json(response);
});

http.listen(port, function () {
  console.log("Relay listening on port " + port);
});

io.sockets.on('connection', function (socket) {
  console.log("Connection opened", socket);
  socket.on('disconnect', function () {
    console.log("Socket disconnected");
  });
});

When I run node relay it dutifully reports

Relay listening on port 8000

When I sails lift my other server it dutifully reports

will connect socket to http://localhost:8000 with Socket.io-client version 0.9.16

But I never see an actual connection.

If I point a browser at localhost:8000 I get the {"message":"some response"} JSON response I expect.

Why isn't my relay server accepting a connection from my socker.io-client app?

The issue here is probably that you're trying to re-use the socket.io-client from inside of Sails. In general, if you're require() -ing dependencies of Sails directly in your project, you're heading in the wrong direction. In this case, socket.io-client caches configurations and connections, so your require isn't getting a fresh copy.

Instead, do

npm install socket.io-client@~0.9.16 --save

in your project and require with

var client = require('socket.io-client');

that'll give you a fresh copy of the socket client to work with, and avoid any conflicts with the Sails core's version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM