我正在尝试使用socket.io-client
.
我的航行服务app/services/Watcher.js
看起来像
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);
});
};
调用config/bootstrap.js
方式如下:
Watcher.connect(function(socket){
sails.log.debug("Connected watcher to relay with socket", socket);
});
在 Express 方面,我的服务器relay.js
很简单:
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");
});
});
当我node relay
尽职尽责地运行它时
Relay listening on port 8000
当我sails lift
我的另一台服务器时,它尽职尽责地报告
will connect socket to http://localhost:8000 with Socket.io-client version 0.9.16
但我从来没有看到真正的联系。
如果我将浏览器指向localhost:8000
我会得到{"message":"some response"}
JSON
我期望的响应。
为什么我的中继服务器不接受来自我的 socker.io-client 应用程序的连接?