请继续参考每个用户的连接套接字
我有一个(为了清晰起见)聊天。
用户可以login,写信息,其他人可以看到[name]:[message]
。
我不想在每次写socket.emit('say', message);
发送用户名和ID socket.emit('say', message);
因为这是多余的,所以我在服务器上做的是这样的:
var io = require("socket.io").listen(server), sockets = {}; io.sockets.on('connection', function (socket){ socket.on('savePersonToSocket', function(user){ socket.user = user; sockets[user.id] = socket; } socket.on('doSomething', doSomething); }); // must be outside of 'connection' since I have TONS of these in a much more //complex structure and I don't want to create them all per user connection. function doSomething(){ ... sockets[userID].emit('foo'); // how to get the userID at this point? ... }
那么,我该如何获得用户ID呢?
笔记:
- 对于每个用户login并连接他们的Facebook帐户,客户端将告诉服务器保存该人的姓名和ID。
我想用cookie来保存用户的名字和ID,服务器通过读取cookie知道是哪个用户,但是这有点难以理解:每次发送这些信息都是多余的。
我也可以劫持'on'函数(以某种方式)并添加将知道是哪个用户的function,因为所有'on'侦听器都必须驻留在'connection'侦听器中。
可能发生的事情是用户连接到您的聊天应用程序,所以在这里
io.sockets.on('connection', function (s){ socket = s; // cache for later usage
您将“他的套接字”分配给从上下文中解除绑定的variablessocket
,它是自己的。 现在让我们说第二个用户到达, socket
被重新分配给第二个用户的第二个,但是如果你得到一个savePersonToSocket
事件,那么socket
将被使用,这对每个人都是一样的,更详细地说,它涉及到最后一个用户连接的。
你不需要保留对s
的引用,当你有很多用户连接到你的应用程序时,你可能不得不处理这个问题,但是这个解决scheme与你的方法有很大的不同。
编辑:另一种方式可以通过映射用户ID和socket
S:
// this assumes that the name of a user is unique var userSocket = {}; io.sockets.on('connection', function (s){ socket.on('savePersonToSocket', function(user){ userSocket[user.name] = s; }
所以基本上你是说你不想传递给doSomething
事件有效载荷的userID
? 因为这对我来说似乎是一个有效的解决scheme,所以它不是简化的,这是让服务器知道它正在处理的用户的最简单的方法。 其他解决scheme可能更优雅,但我怀疑它们是如此简单,易于维护。
如果你可以在一个帐号下保存套接字,可以坐几个人,例如,这样一个系统就是:
var socketsList io.sockets.on('connection', function (socket) { socket.on('auth', function (data) { // request to database if ( ! (UNIQUE_ID in socketsList)) { socketsList[UNIQUE_ID] = {}; socketsList[UNIQUE_ID][socket.id] = socket; }); }); // ONEXIT - BROWSER EVENT var onExit = function () { socket.emit('exit', { UNIQUE_ID : 123}) } window.onunload = onExit; // socket.on('exit', function (data) { delete socketsList[UNIQUE_ID][socket.id] })
如我错了请纠正我
你不能从字面上保存连接,它会频繁地改变。 不过你可以保存socket.id。 你有io.sockets.sockets [socketid]这可能是你在找什么。
你可以保存ID和名称数组,如果你喜欢,可以重复使用。
(请注意,与stream程相关的variables使其变得不那么可缩放,但是您可以长时间不关心它:)
你也有套接字相关的存储对象socket.handshake socket.handshake.somevariable = simple_value_a_string_or_a_number
从理论上讲,这样的东西应该工作,但应用程序的apply
是不好的恕我直言,缺乏简单的函数指针使得代码丑陋
var io = require("socket.io").listen(server); io.sockets.on('connection', function (socket){ socket.on('savePersonToSocket', function(user){ socket.user = usr; } // No more simple function pointers.... socket.on('doSomething', function(){ // pass the Socket as the scope doSomething.apply(socket, arguments); }); }); function doSomething(){ ... this.emit('foo'); ... }
这个怎么样:
var io = require("socket.io").listen(server), sockets = {}; io.sockets.on('connection', function (socket){ socket.on('savePersonToSocket', function(user){ socket.user = user; sockets[user.id] = socket; } socket.on('doSomething', function() { doSomething(socket.user) }); }); // must be outside of 'connection' since I have TONS of these in a much more //complex structure and I don't want to create them all per user connection. function doSomething(user){ ... sockets[user.id].emit('foo'); ... }
我知道你问了这个问题已经很长时间了,但是就在前4天,我发布了一个节点js,express和socket.io的模块,pipe理你想要的东西。 检查用法和例子,我希望你会发现这个模块有帮助!
您可以通过NPM来安装socket.io.users 这是一个用于socket.io应用程序的节点js模块。 每个客户一个用户。
你可以在这个答案上使用一些使用代码