admin管理员组文章数量:1023848
I'm trying to create a new socket event handler in an online browser game I play so I can send data between players who have the same userscript installed.
The game uses socket.io and here's what I've done so far: I added a user-side socket.on handler, like this:
game.socket.on("hello", function() { console.log("hello there") })
I did this on two players that are connected to the same game, but when I do this:
game.socket.emit("hello")
Only the player who sent out the emit logs out hello there
.
My guess is that the server-side doesn't have a handler that emits the event with name hello
. Is there a way I can add that without access to the server-side code, or circumvent it and send the data directly to the other players?
I'm trying to create a new socket event handler in an online browser game I play so I can send data between players who have the same userscript installed.
The game uses socket.io and here's what I've done so far: I added a user-side socket.on handler, like this:
game.socket.on("hello", function() { console.log("hello there") })
I did this on two players that are connected to the same game, but when I do this:
game.socket.emit("hello")
Only the player who sent out the emit logs out hello there
.
My guess is that the server-side doesn't have a handler that emits the event with name hello
. Is there a way I can add that without access to the server-side code, or circumvent it and send the data directly to the other players?
-
You will have to show a larger context of your code.
game.socket.emit("hello")
will only send a message to one socket endpoint. If that's endpoint is your server and you expect your server to then send that message out to other users, you will need to add code to the server to listen for that message and then send it out to other players when it is received. socket.io is a client to server connection. You cannot send directly from one client to another in socket.io (or in websocket). Instead, you have to send to the server and have the server forward it on to some other client. – jfriend00 Commented Jul 4, 2016 at 0:51 - Thanks for the answer, that's what I thought – MarksCode Commented Jul 4, 2016 at 1:24
3 Answers
Reset to default 3I will add a bit and turn my ment into an answer:
A socket.io connection is a connection between client and server. From the client, all you can do on that connection is send to the server. Thus game.socket.emit("hello")
will only send a message to the server.
If you want your server to then send that message out to other users, you will need to add code to the server to listen for that message and then send it out to other players when it is received. socket.io is a client to server connection.
You cannot send directly from one client to another in socket.io (or in websocket which socket.io is based on). Instead, you have to send to the server and have the server forward it on to some other client.
Socket.io have introduced room (check),You can send your message from client and use server side to emit this message to all the room members,considering your room id is room_one:
Client:
var socket = io();
socket.on('connect',function(){
socket.emit('join','room_one');
socket.emit('room_message',{hello:'hello'});
});
socket.on('room_message',function(data){
console.log(data);
})
Server:
io.sockets.on('connection',function(socket){
socket.on('join',function(room){
socket.join(room);
});
socket.on('room_message', function(data) {
io.to('room_one').emit('room_message',{world:'world'});
});
});
wish this would be helpful!
This is what I did to display the current users in a room to all connected sockets:
server side
socket.join(user.room);
io.to(user.room).emit('roomData',{room:user.room, users: getUsersInRoom(user.room)});
client side
socket.on('roomData',(data)=>{
console.log(data);
currentUsers.innerHTML='${data.users.length} Users Online in ${data.room} Room'
});
I'm trying to create a new socket event handler in an online browser game I play so I can send data between players who have the same userscript installed.
The game uses socket.io and here's what I've done so far: I added a user-side socket.on handler, like this:
game.socket.on("hello", function() { console.log("hello there") })
I did this on two players that are connected to the same game, but when I do this:
game.socket.emit("hello")
Only the player who sent out the emit logs out hello there
.
My guess is that the server-side doesn't have a handler that emits the event with name hello
. Is there a way I can add that without access to the server-side code, or circumvent it and send the data directly to the other players?
I'm trying to create a new socket event handler in an online browser game I play so I can send data between players who have the same userscript installed.
The game uses socket.io and here's what I've done so far: I added a user-side socket.on handler, like this:
game.socket.on("hello", function() { console.log("hello there") })
I did this on two players that are connected to the same game, but when I do this:
game.socket.emit("hello")
Only the player who sent out the emit logs out hello there
.
My guess is that the server-side doesn't have a handler that emits the event with name hello
. Is there a way I can add that without access to the server-side code, or circumvent it and send the data directly to the other players?
-
You will have to show a larger context of your code.
game.socket.emit("hello")
will only send a message to one socket endpoint. If that's endpoint is your server and you expect your server to then send that message out to other users, you will need to add code to the server to listen for that message and then send it out to other players when it is received. socket.io is a client to server connection. You cannot send directly from one client to another in socket.io (or in websocket). Instead, you have to send to the server and have the server forward it on to some other client. – jfriend00 Commented Jul 4, 2016 at 0:51 - Thanks for the answer, that's what I thought – MarksCode Commented Jul 4, 2016 at 1:24
3 Answers
Reset to default 3I will add a bit and turn my ment into an answer:
A socket.io connection is a connection between client and server. From the client, all you can do on that connection is send to the server. Thus game.socket.emit("hello")
will only send a message to the server.
If you want your server to then send that message out to other users, you will need to add code to the server to listen for that message and then send it out to other players when it is received. socket.io is a client to server connection.
You cannot send directly from one client to another in socket.io (or in websocket which socket.io is based on). Instead, you have to send to the server and have the server forward it on to some other client.
Socket.io have introduced room (check),You can send your message from client and use server side to emit this message to all the room members,considering your room id is room_one:
Client:
var socket = io();
socket.on('connect',function(){
socket.emit('join','room_one');
socket.emit('room_message',{hello:'hello'});
});
socket.on('room_message',function(data){
console.log(data);
})
Server:
io.sockets.on('connection',function(socket){
socket.on('join',function(room){
socket.join(room);
});
socket.on('room_message', function(data) {
io.to('room_one').emit('room_message',{world:'world'});
});
});
wish this would be helpful!
This is what I did to display the current users in a room to all connected sockets:
server side
socket.join(user.room);
io.to(user.room).emit('roomData',{room:user.room, users: getUsersInRoom(user.room)});
client side
socket.on('roomData',(data)=>{
console.log(data);
currentUsers.innerHTML='${data.users.length} Users Online in ${data.room} Room'
});
本文标签: javascriptAdd socket emit from client sideStack Overflow
版权声明:本文标题:javascript - Add socket emit from client side - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745604008a2158623.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论