In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about whether ws is a built-in module for nodejs. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Ws is not a built-in module for nodejs. Ws is a WebSocket library of nodejs that can be used to create services and needs to be installed through the "npm install ws" command before it can be used, so it is not a built-in module in nodejs.
This tutorial operating environment: windows7 system, nodejs version 12.19.0, DELL G3 computer.
Ws: a WebSocket library of nodejs that can be used to create services. Https://github.com/websockets/ws
Nodejs uses ws module
First download the websocket module and enter it on the command line
Npm install ws
Ws module in 1.node.js creates server (ser.js)
/ / load the websocket module ws;var ws = require ("ws") on node; / / start the websocket-based server and listen for our client access. Var server = new ws.Server ({host: "127.0.0.1", port: 6080,}); / / listen for incoming client event function websocket_add_listener (client_sock) {/ / close event client_sock.on ("close", function () {console.log ("client close");}) / / error event client_sock.on ("error", function (err) {console.log ("client error", err);}); / / end / / message event, data is already the original data decoded according to the websocket protocol / / there is a packet protocol in the underlying layer of websocket, so there is absolutely no case of sticking packets. / / every time you decode a packet, a message event will be triggered; / / there will be no sticking of packets. Once send, the send data will be encapsulated independently. / / if we are directly based on TCP, we can achieve exactly the same effect if we implement a packet protocol similar to websocket. Client_sock.on ("message", function (data) {console.log (data); client_sock.send ("Thank you!");}); / / end} / / connection event, client access; function on_server_client_comming (client_sock) {console.log ("client comming"); websocket_add_listener (client_sock) } server.on ("connection", on_server_client_comming); / / error event, indicating our listening error; function on_server_listen_error (err) {} server.on ("error", on_server_listen_error); / / headers event, the character returned to the client. Function on_server_headers (data) {/ / console.log (data);} server.on ("headers", on_server_headers)
Create client (client.js) for ws module in 2.node.js
Var ws = require ("ws"); / / url ws://127.0.0.1:6080// creates a client's socket and then lets the client connect to the server's socketvar sock = new ws ("ws://127.0.0.1:6080"); sock.on ("open", function () {console.log ("connect success!"); sock.send ("HelloWorld1") Sock.send ("HelloWorld2"); sock.send ("HelloWorld3"); sock.send ("HelloWorld4"); sock.send (Buffer.alloc (10);}); sock.on ("error", function (err) {console.log ("error:", err);}); sock.on ("close", function () {console.log ("close");}) Sock.on ("message", function (data) {console.log (data);})
3. Web client creation (using WebApi-> WebSocket) index.html
Websocket example var ws = new WebSocket ("ws://127.0.0.1:6080/index.html"); ws.onopen = function () {alert ("open"); ws.send ("WebSocket hellowrollover!");}; ws.onmessage = function (ev) {alert (ev.data);} Ws.onclose = function (ev) {alert ("close");}; ws.onerror = function (ev) {console.log (ev); alert ("error");}; Thank you for reading! This is the end of the article on "whether ws is a built-in module of nodejs". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.