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?

Share Improve this question asked Jul 3, 2016 at 23:01 MarksCodeMarksCode 8,61417 gold badges71 silver badges143 bronze badges 2
  • 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
Add a ment  | 

3 Answers 3

Reset to default 3

I 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?

Share Improve this question asked Jul 3, 2016 at 23:01 MarksCodeMarksCode 8,61417 gold badges71 silver badges143 bronze badges 2
  • 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
Add a ment  | 

3 Answers 3

Reset to default 3

I 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