简体   繁体   English

客户端上的socket.io如何侦听服务器发出的事件?

[英]How does socket.io on the client listen to events emitted from server?

I have the following code on my client side: 我的客户端上有以下代码:

btn.addEventListener('click', function(){
    socket.emit('chat',{
        message: message.value,
        handle: handle.value
    });
});

So I think I understand the above. 因此,我认为我理解上述内容。 When a click event happens, run the callback function. 当发生click事件时,请运行回调函数。 Inside the callback function, have socket emit this "chat" event to the server. 在回调函数内部,让套接字向服务器发出此“聊天”事件。 Along with the event socket emits, pass the JSON data. 与事件套接字一起发出,传递JSON数据。

Now on the server side we have this: 现在在服务器端,我们有:

var io = socket(server);

io.on('connection', function(socket){    
    socket.on('chat', function(data){
        io.emit('chat', data);  
    });
});

I think I understand this as well. 我想我也明白这一点。 Bind the socket to this server. 将套接字绑定到该服务器。 Then listen to the connection event. 然后听连接事件。 When that event is invoked, socket.io (will magically?) pass a socket object to our function. 调用该事件时,socket.io(会神奇地吗?)将套接字对象传递给我们的函数。 We then listen to all of our sockets for a chat. 然后,我们听所有套接字进行聊天。 As defined on the client side, take the data emited from the chat and emit it back to all of our sockets. 按照客户端的定义,将聊天中发出的数据拿回我们所有的套接字。

socket.on('chat', function(data){
     // do cool stuff
})

Again in the above example, we have the client listening to the event on the server side. 还是在上面的示例中,我们让客户端在服务器端监听事件。

My question is, how does the event get "passed" to the client side? 我的问题是,事件如何“传递”到客户端? Is this some sort of native Javascript functionality? 这是某种本机Javascript功能吗? I would like to know more of what happens on the backend. 我想了解更多有关后端发生的情况。

The event gets passed to the server through websockets. 该事件通过websockets传递到服务器。 Its a tcp connection from the browser to the server. 它是从浏览器到服务器的tcp连接。 The connection is full duplex meaning the server can send real time data to the client and vise versa. 连接是全双工的,这意味着服务器可以将实时数据发送到客户端,反之亦然。

In your frontend code you should have something that looks similar to 在前端代码中,您应该具有类似于以下内容的内容

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io();
</script>

This code asks the server for a tcp connection using web sockets. 此代码要求服务器使用Web套接字进行TCP连接。 Once the browser is connected to the server through websockets, socket.io can send events to the send through the connection. 一旦浏览器通过websockets连接到服务器,socket.io就可以通过连接将事件发送到发送方。

The socket that is passed in the connection event is just a reference to whatever socket gets created when the frontend connects. 在连接事件中传递的套接字只是对前端连接时创建的任何套接字的引用。 The socket gets a unique id and with this reference you can communicate in real time to the web browser client. 套接字获得一个唯一的ID,通过此引用,您可以与Web浏览器客户端实时通信。

If you want a deep dive into web sockets, I'd recommend reading this 如果您想深入了解Web套接字,建议阅读此文章

https://tools.ietf.org/html/rfc6455 https://tools.ietf.org/html/rfc6455

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

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