Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function and usage of WebSocket, SocketIO and SignalR

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In this issue, the editor will bring you about the function and usage of WebSocket and SocketIO as well as SignalR. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

What is WebSocket?

WebScoket is a technology that enables two-way real-time communication between the client and the server. It is a protocol specification of the latest HTML standard HTML5, which is essentially a TCP-based protocol. It creates a TCP connection after sending a special request for handshake through the HTTP/HTTPS protocol, and then the browser / client and server can communicate in two-way real-time through this connection.

Why use WebSocket?

1) for a long time, the HTTP protocol is stateless and one-way communication, that is, the client requests once and the server replies once. If you want the server messages to be sent to the client in time, you need to adopt a mechanism similar to polling, that is, the client sends requests to the server regularly and frequently, which is very inefficient, and the HTTP packet header itself has a large number of bytes, which wastes a lot of bandwidth and server resources.

2) in order to improve efficiency, AJAX/Comet technology appears, which realizes two-way communication and saves some bandwidth, but still needs to make a request, which is still polling in essence.

3) the new generation of HTML standard HTML5 introduces WebSocket technology, which enables clients and servers to establish TCP connections through HTTP protocol, and then two-way communication can be carried out anytime and anywhere, and the amount of packet header information exchanged is very small.

How to use WebSocket

In browsers that support WebSocket, after the creation of Socket, the response of Socket is handled through the implementation of onopen, onmessage, onclose and onerror events.

The relationship between WebSocket, HTTP and TCP

WebSocket and HTTP both belong to the application layer protocol and are based on TCP. Their send functions are finally transmitted through the TCP system interface. What about the relationship between WebSocket and HTTP? When WebSocket establishes a handshake connection, the data is transmitted through the HTTP protocol, but after the connection is established, the real data transmission phase does not need the participation of the HTTP protocol. The relationship between them is shown below:

Under what circumstances do you use WebSocket

If the game needs to support both the mobile side and the web side, there is no doubt that WebSocket should be used, and now each platform provides the corresponding WebSocket implementation. If the game does not need to support the web end, and requires high real-time requirements, such as multi-player shooting, MMORPG and so on, then the native Socket combined with TCP/UDP will be better.

What is SocketIO?

WebSocket is the latest specification proposed by HTML5, although mainstream browsers have supported it, there may still be incompatibility. In order to be compatible with all browsers and provide programmers with a consistent programming experience, SocketIO encapsulates WebSocket, AJAX and other communication methods into a unified communication interface, that is to say, when we use SocketIO, we do not have to worry about compatibility, the bottom will automatically choose the best way of communication. Therefore, WebSocket is a subset of SocketIO.

Matters needing attention in using SocketIO Project

To put it simply, if you use Socket.IO, then both your client and server must use a set of solutions based on it, which is a bit scary!

But does .NET have a solution based on Socket.IO implementation?

The answer is yes:

SocketIOSharp

Https://github.com/uhm0311/SocketIOSharp

Net project, how do we choose a real-time communication framework?

It is not recommended to use Socket.IO as a solution for real-time communication in the project, because Microsoft provides us with a stronger and better family bucket! ASP.NET Core can either use native Websocket or SignalR!

What is SignalR?

ASP.NET SignalR is a library for ASP.NET developers that simplifies the process for developers to add real-time Web functionality to their applications. Real-time Web functionality refers to the ability for server code to push content to a connected client as soon as it becomes available, rather than having the server wait for the client to request new data. WebSockets is a new API provided by HTML5, which can establish a Socket connection between the Web web page and the server. When WebSockets is available (that is, the browser supports Html5) SignalR uses WebSockets, and when it does not support it, SignalR will use other technologies to ensure the same effect.

The above is the role and usage of WebSocket and SocketIO and SignalR shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report