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

How to realize WebSocket Communication based on Node.js

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Most people do not understand the knowledge points of this article "how to achieve WebSocket communication based on Node.js", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to achieve WebSocket communication based on Node.js" article.

Dependency packages for node

There are many dependent packages to implement Websocket in node, both websocket and ws are available. In this paper, we choose ws to implement, first install dependencies.

Npm install ws

Chat room example

If all the users of An and B are connected to the Websocket service through the client, and the messages sent by each person need to be forwarded to others through Websocket, this scenario is similar to the scenario where the server broadcasts the messages of A to other users in the group.

Server-side implementation

First of all, let's take a look at the server-side program. The specific workflow is divided into the following steps:

Create a WebSocketServer service and listen for connection requests on port 8080.

Whenever a new client connects to the WebSocket successfully, the connection is push to an array of connection pools.

Listens for the message event, and when it occurs, traverses the connection pool to forward the message to the corresponding client on a connection-by-connection basis

Listens for the close event and, when it occurs, moves the connection out of the connection pool

Server code

Var WebSocketServer = require ('ws'). Server, wss = new WebSocketServer ({port: 8080}); / / connection pool var clients = []; wss.on (' connection', function (ws) {/ / add the connection to the connection pool clients.push (ws); ws.on ('message', function (message) {/ / broadcast message clients.forEach (function (ws1) {if (ws1! = ws) {ws1.send (message)) })}); ws.on ('close', function (message) {/ / when the connection is closed, move it out of the connection pool clients = clients.filter (function (ws1) {return ws1! = = ws})});})

Client implementation

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