简体   繁体   English

从客户端添加套接字发射

[英]Add socket emit from client side

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:游戏使用 socket.io,这是我到目前为止所做的:我添加了一个用户端 socket.on 处理程序,如下所示:

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 .只有发出emit 的玩家在hello there登出hello there

My guess is that the server-side doesn't have a handler that emits the event with name hello .我的猜测是服务器端没有发出名称为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 will add a bit and turn my comment into an answer:我将添加一点并将我的评论变成答案:

A socket.io connection is a connection between client and server. socket.io 连接是客户端和服务器之间的连接。 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.因此game.socket.emit("hello")只会向服务器发送一条消息。

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. socket.io 是客户端到服务器的连接。

You cannot send directly from one client to another in socket.io (or in websocket which socket.io is based on).您不能在 socket.io(或 socket.io 所基于的 websocket)中直接从一个客户端发送到另一个客户端。 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 : Socket.io 引入了room ( check ),你可以从客户端发送你的消息并使用服务器端将此消息发送给所有房间成员,考虑到你的房间 id 是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'
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM