所以我有一个服务器,当用户进入我的网页时,用户将连接到该服务器。我使用节点js和socket io,当新玩家加入时,我给他们一个唯一的ID。我通过以下代码执行此操作:
js on html:
<script defer type="text/javascript"
src="http://localhost:8081/socket.io/socket.io.js"></script>
server = io.connect('http://localhost:8081/game');
然后使用以下代码连接到我的服务器:
Server.prototype.startSockets = function()
{
this.socket = io.listen(this.server);
this.socket.of('game').on('connection', function(user)
{
user.userId = this.userId;
user.userName = this.userName + " " + this.userId;
this.userId++;
consoleLog('SERVER', user.userName + ' has connected. ID: ' + user.userId)
user.emit('response',
{
userId: user.userId,
userName: user.userName
});
user.on('disconnect', function()
{
this.em.emit('cancelHostGame', user.userId, user.userName);
consoleLog('SERVER', user.userName + ' has disconnected. ID: ' + user.userId)
}
.bind(this));
}
.bind(this));
};
它将为用户提供一个ID,然后将其发送回客户端:
server.on('connected', function(response)
{
userId = response.userId;
userName = response.userName;
});
我想要做的是连接具有特定ID的人。所以当有人连接时,他们会得到一个560的id,将它保存在一个会话中,我打开一个新的页面,读取会话ID然后用它们连接它们。
我尝试在我做io.connect(&#39; localhost ...&#39;,560)的客户端上传入id,但我无法让它工作。任何帮助都是适当的