发送客户端其他玩家的得分?socket . io
Send client other players score? Socket.io
我有一种情况,我不确定如何向其他客户发送他们竞争对手的当前分数。
例如:
当玩家1点击时,我希望它能告诉玩家2玩家1在客户端的得分。
userOne = userIDs[0];
userTwo = userIDs[1];
socket.on("player one", function(data) {
console.log(data.pScore + "THIS IS WORKING");
score = data.pScore;
console.log("THIS IS PLAYER TWO" + data.pNameTwo);
if (isInArray(data.pNameTwo,allUsers)) {
console.log(users[socket.id].username);
}
socket.emit("player ones score", {p1Score: score});
});
socket.on("player two", function(data) {
console.log(data.pScore + "THIS IS WORKING");
score = data.pScore;
socket.emit("player twos score", {p2Score: score});
});
我打算通过仅向特定用户ID发送分数。但我不确定最好的方法。
最好的方法是创建一个包含所有连接的对象,每次用户连接/断开连接时更新该对象。我看到你已经有了它们的id的userid列表,所以,如果你的对象被称为userConnections,你的代码应该是…
userOne = userIDs[0];
userTwo = userIDs[1];
socket.on("player one", function(data) {
console.log(data.pScore + "THIS IS WORKING");
score = data.pScore;
console.log("THIS IS PLAYER TWO" + data.pNameTwo);
if (isInArray(data.pNameTwo,allUsers)) {
console.log(users[socket.id].username);
}
userConnections[userTwo].emit("player ones score", {p1Score: score});
});
socket.on("player two", function(data) {
console.log(data.pScore + "THIS IS WORKING");
score = data.pScore;
userConnections[userOne].emit("player twos score", {p2Score: score});
});
好,我也将展示如何初始化和管理userConnections。它是这样的(我只是猜测你代码中的一些变量是如何被调用的,所以我可能会得到一些错误的名字)。
var userConnections = {}
io.on('connection', function(socket) {
userConnections[socket.id] = socket;
//socket.on('player one', function(data) { ... } );
//socket.on('player two', function(data) { ... } );
socket.on('disconnect') {
delete userConnections[socket.id];
}
}
相关文章:
- 如何使用Socket.io将命令从客户端发送到服务器
- Socket.IO未定义变量
- Socket.io on Mozilla Rhino
- socket.io发射三次
- AngularJS$q承诺使用socket.io
- 如何知道使用socket.io订阅/取消订阅频道
- 通过socket.io发射给特定的人
- 通过socket.io从浏览器流式传输视频
- 通过node.js和socket.io向特定用户发送数据
- 如何使用socket.io在meanjs项目中配置jshint
- 如何使用Socket.io和Node.js开发大型聊天应用程序
- RangeError nodejs socket.io
- 通过node.js和socket.io在两个用户之间发送消息
- 使用socket.io更新express实时视图
- socket.io cookie解析握手错误
- 如何在同一端口上使用ExpressJS和Socket.io
- 使用cordova快速nodejs socket.io
- 通过socket.io与heroku应用程序进行通信
- Socket.io客户端在使用名称空间时忽略端口[Bug?]
- Node.js-socket.io-对io.sockets的引用突然未定义