我正在使用socket.io和node.js进行小型多人游戏。我有一系列用户。
index.html
socket.emit('1st word',$wordInput.val());
server.js
users = [];
//rest of code
socket.on('1st word', function(data){
//here is where i was thinking of doing a circular array
});
我想做的是制作一个圆形阵列。所以假设有3个用户。用户1发送给用户2,用户2发送给用户3,用户3发送给用户1.我希望它像那样发送3轮(因为用户的长度是3)。
任何建议都会有所帮助!如果您有任何问题,请询问q以获得更多说明!致谢
答案 0 :(得分:0)
也许你可以这样做:
//lets say all the user have different id
//index.html user 1
var user1="abcde1"
//you add user id into the server first using socket.io lets say you emit adduser to server
socket.emit("adduser",{user:user1})
//this will emit back if there is another user
socket.on("emitback",function(data){
socket.emit("adduser",{user:data.user})
})
然后在服务器端
//server.js
var user=[]
socket.on("adduser",function(data){
if(user.indexOf(data.user)==-1){
//we add user into array user
user.push(data.user)
}
//if the array already has the id of the user we emit to user 2
else if(user.indexOf(data.user)>-1){
//if user array length is longer than 1 we emit to next user if not we emit to ourselves
if(user.length>1 && user.indexOf(data.user)!=user.length-1){
socket.emit("emitback",{user:user[user.indexOf(data.user)+1]})
}
//this will emit to the first user because this is the last user
else if(user.length>1 && user.indexOf(data.user)==user.length-1){
socket.emit("emitback",{user:user[0]})
}
else if(user.length==1){
socket.emit("emitback",{user:user[user.indexOf(data.user)]})
}
}
})
答案 1 :(得分:0)
套接字不会从用户发送给用户,每个用户都连接到服务器并将其命令发送到服务器。
您必须跟踪您的用户,并在服务器端彼此之间发送命令。
function getEmails() {
return $http.get('/api/email')
.success(function(data) {
var messages = data;
})
};