【发布时间】:2013-11-27 22:01:13
【问题描述】:
我正在使用 socket.io。
我导出套接字,以便在不同的模块中使用它:
io.sockets.on('connection', function (socket) {
//export socket to be used throughout the application
exports.socket = socket;
//more code...
});
我使用这个函数从其他模块发送消息:
var sendJSONReport = function (report) {
report.client_id = require("./socketMngr").socket.id;
var msg = JSON.stringify(report);
require("./socketMngr").socket.emit('sent JSON Report', msg);
};
在我的客户端 js 中,我 console.log 发送的消息。
我打开两个 chrome 选项卡并使用不同的数据在这两个选项卡上运行应用程序。 我看到其中一个选项卡获取了应该到达另一个选项卡的消息。 client_id 与应该到达该选项卡的消息相同,这意味着服务器发送到错误的客户端 ID。
我做错了什么?
【问题讨论】:
标签: javascript node.js socket.io