简体   繁体   English

socket.io 的默认行为实际上是什么?

[英]what is actually the default behavior of socket.io?

I'm using socket.io on the client as well as on the server side.我在客户端和服务器端都使用socket.io It is a great library, but I must say that the default behavior is not described in the docs, which make using the library so confusing.这是一个很棒的库,但我必须说文档中没有描述默认行为,这使得使用该库变得如此混乱。 At least I didn't find any references for the default behavior.至少我没有找到任何默认行为的参考。

Basically, I watched tutorials, where a basic chat app was build with socket.io.基本上,我看了教程,其中使用 socket.io 构建了一个基本的聊天应用程序。 In the tutorials, the server send a message automatically to all conected clients.在教程中,服务器自动向所有连接的客户端发送消息。 Is this the default behavior in the server side?这是服务器端的默认行为吗?

I'm not sure about this.我不确定这一点。 I'm developing an app where the user can (un)subscribe to specific topics and receive values from the server.我正在开发一个应用程序,用户可以(取消)订阅特定主题并从服务器接收值。 Let's say I have two topics (topic1 and topic2).假设我有两个主题(topic1 和 topic2)。 I opened two clients (client1 and client2) and I subscribed to topic1 from client1.我打开了两个客户端(client1 和 client2),并从 client1 订阅了 topic1。 I noticed that I received the value1 of topic1 in client1 but client2 received nothing.我注意到我在 client1 中收到了 topic1 的 value1 但 client2 什么也没收到。

const io = require('socket.io')(3000); // create server
  io.on('connection', socket => {
      console.log("client is connected over sockets");
    socket.on('subscribe', () => {socket.emit('send-msg', "send to client");})
  });

In this case above, will the server send to all clients or to only one client.在上面的这种情况下,服务器将发送给所有客户端还是仅发送给一个客户端。 Can you clarify this to me and tell me what is the default behavior of socket.io please您能否向我澄清这一点并告诉我 socket.io 的默认行为是什么


PS: Another thing I noticed about socket.io is that there is many ways to do the same thing and it is not documented well. PS:我注意到 socket.io 的另一件事是有很多方法可以做同样的事情,但没有很好的记录。 Like for example, I'm instantiating a client with socketIOClient function: const socket = socketIOClient("my_host");例如,我正在使用 socketIOClient 函数实例化一个客户端: const socket = socketIOClient("my_host"); But I ve seen many tutorials that uses the openSocket function or even directly the io function (here for some reason the author added this in the html <script defer src="http://localhost:3000/socket.io/socket.io.js"></script> )但是我看过很多使用openSocket函数甚至直接使用io函数的教程(这里出于某种原因作者在html中添加了这个<script defer src="http://localhost:3000/socket.io/socket.io.js"></script> )

All these function do the same thing, right?所有这些功能都做同样的事情,对吧?

You're looking at the difference between namespace.emit and socket.emit .您正在查看namespace.emitsocket.emit之间的区别。 A socket is one specific connection, and emitting an event on it sends it only to that one connection.一个套接字是一个特定的连接,在它上面发出一个事件只将它发送到那个连接。 A namespace on the other hand is a group of several sockets, and emitting an event on it emits it to every socket in the group.另一方面,命名空间是一组多个套接字,在其上发出事件会将其发送到组中的每个套接字。 The entire io server is one namespace by default, so io.emit(...) sends a message to all connected clients.默认情况下,整个io服务器是一个命名空间,因此io.emit(...)向所有连接的客户端发送一条消息。 You can group your sockets into arbitrary namespaces and rooms to make it easy to send messages to selected groups.您可以将套接字分组到任意命名空间和房间中,以便轻松地向选定的组发送消息。

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

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