简体   繁体   English

如何从客户端(Javascript)使用Socket IO来(-)。emit(-)消息

[英]How to consume the Socket IO to(-).emit(-) messages from client (Javascript)

In my Node JS code, the SocketIO emits will be done with room ID. 在我的Node JS代码中,SocketIO的发出将通过房间ID完成。 Out of all room connections to Socket IO, a corresponding room will consume the particular message. 在所有与Socket IO的房间连接中,对应的房间将占用特定消息。 Currently, I am testing this application from my local by writing a JavaScript code but I am not sure how to consume this (No issues while just using emit as I can use socket.on()). 目前,我正在通过编写JavaScript代码在本地测试此应用程序,但不确定如何使用它(仅使用发出发射时没有问题,因为我可以使用socket.on())。 Can someone help me with this? 有人可以帮我弄这个吗?

Below is the snippet of my Node JS code. 以下是我的Node JS代码片段。

var emit = function(room, type, content) {
    io.to(room).emit("message", [type, content]);
    // send standard socket.io message with specific type, for clients connecting directly
    console.log("emit to room: '" + room + "', type: '" + type + "', content: " + content);
    return io.to(room).emit(type, content);
}; 

On the client side use 在客户端使用

socket.on("message",function(data)
{
//do something
})

To respond to the event 响应事件

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

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