插座之间有什么区别。IO和websockets在 node . js ? 它们都是服务器推送技术吗? 我觉得唯一的不同是,

套接字。IO允许我通过指定事件名称来发送/发出消息。 对于socket。io消息从服务器将到达所有客户端,但同样在websockets,我被迫保持一个数组的所有连接和循环通过它发送消息到所有客户端。

同时, 我想知道为什么网络检查员(如Chrome/firebug/fiddler)无法从服务器捕捉这些消息(从socket.io/websocket) ?

请澄清这一点。


当前回答

TLDR:

的套接字。Io '是一个应用层规范,可以在/使用应用层规范'websockets'之上实现。

websocket规范 套接字。io规范

我认为简单的答案是在基本的web技术定义中:

Specification: A documented standard detailing the requirements for a program to achieve in order to be labeled as "an implimentation of some sepc." It is important to achieve this rubber stamp when building programs, because any program is only as good at the machine executing the code. Programming is fundamentally built upon specifications, and if, they are not followed code will not execute correctly. However, a specification does nothing. It is just a text document. Implementation: This is actual, executable code that accomplishes what the specification says to do. Application Layer - System that defines messages and handshakes sent over transport. This is the stuff you have to know when working with HTTP/Websockets/Socketio. It defines how the connections will be made, authenticated, data will be sent, and how it will arrive.

其他回答

我想在2021年再提供一个答案。套接字。在2019年9月至2020年8月(将近2年)期间,基本上没有任何活动,我本以为该项目可能已经死亡。

套接字。io也发表了一篇文章,叫做为什么使用Socket。2020年的IO ?除了退回到HTTP长轮询之外,我认为这2个特性都是socket的。IO提供,websocket缺乏

端的自动重连接 将数据广播到给定客户端集(房间/名称空间)的方法

我发现socket的另一个特性。io方便是为ws服务器开发,特别是我使用docker部署我的服务器。因为我总是启动一个以上的服务器实例,跨ws服务器通信是必须的和套接字。IO为它提供https://socket.io/docs/v4/redis-adapter/。

使用redis-adapter,将服务器进程扩展到多个节点很容易,而ws服务器的负载平衡则很困难。点击这里https://socket.io/docs/v4/using-multiple-nodes/了解更多信息。

我将提供一个反对使用socket.io的参数。

我认为是使用socket。仅仅因为IO有后援就不是个好主意。让IE8 RIP。

在过去有很多情况下,新版本的NodeJS破坏了socket.io。你可以在这些列表中找到例子。https://github.com/socketio/socket.io/issues?q=install+error

如果你要开发一个Android应用或者其他需要与现有应用配合的东西,你可能会马上使用WS, socket。IO可能会给你带来一些麻烦…

另外,Node.JS的WS模块使用起来非常简单。

TLDR:

的套接字。Io '是一个应用层规范,可以在/使用应用层规范'websockets'之上实现。

websocket规范 套接字。io规范

我认为简单的答案是在基本的web技术定义中:

Specification: A documented standard detailing the requirements for a program to achieve in order to be labeled as "an implimentation of some sepc." It is important to achieve this rubber stamp when building programs, because any program is only as good at the machine executing the code. Programming is fundamentally built upon specifications, and if, they are not followed code will not execute correctly. However, a specification does nothing. It is just a text document. Implementation: This is actual, executable code that accomplishes what the specification says to do. Application Layer - System that defines messages and handshakes sent over transport. This is the stuff you have to know when working with HTTP/Websockets/Socketio. It defines how the connections will be made, authenticated, data will be sent, and how it will arrive.

即使现在的浏览器支持WebSockets,我认为没有必要抛弃SocketIO,它仍然在当今的任何项目中占有一席之地。它很容易理解,就我个人而言,多亏了SocketIO,我学会了WebSockets是如何工作的。

正如在本主题中提到的,有很多Angular、React等的集成库,以及TypeScript和其他编程语言的定义类型。

我要补充的另一点是Socket之间的区别。io和WebSockets是与Socket的集群。IO不是什么大问题。套接字。io提供了适配器,可用于将其与Redis链接,以增强可伸缩性。你有眼窝和眼窝。比如Io-redis。

是的,我知道,SocketCluster是存在的,但这是跑题了。

使用套接字。IO基本上就像使用jQuery -你想要支持旧的浏览器,你需要写更少的代码,库将提供回退。套接字。IO使用websockets技术,如果可用,则检查可用的最佳通信类型并使用它。