Is ws a built-in module of nodejs?
ws is not a built-in module of nodejs. ws is a WebSocket library of nodejs that can be used to create services. It needs to be installed through the "npm install ws" command before it can be used, so it is not a built-in module of nodejs.
The operating environment of this tutorial: windows7 system, nodejs version 12.19.0, DELL G3 computer.
ws is not a built-in module of nodejs.
ws: It is a WebSocket library for nodejs that can be used to create services. https://github.com/websockets/ws
nodejs uses ws module
First download the websocket module and enter on the command line
npm install ws
1. The ws module in node.js creates the server (ser.js)
// 加载node上websocket模块 ws; var ws = require("ws"); // 启动基于websocket的服务器,监听我们的客户端接入进来。 var server = new ws.Server({ host: "127.0.0.1", port: 6080, }); // 监听接入进来的客户端事件 function websocket_add_listener(client_sock) { // close事件 client_sock.on("close", function() { console.log("client close"); }); // error事件 client_sock.on("error", function(err) { console.log("client error", err); }); // end // message 事件, data已经是根据websocket协议解码开来的原始数据; // websocket底层有数据包的封包协议,所以,绝对不会出现粘包的情况。 // 每解一个数据包,就会触发一个message事件; // 不会出现粘包的情况,send一次,就会把send的数据独立封包。 // 如果我们是直接基于TCP,我们要自己实现类似于websocket封包协议就可以完全达到一样的效果; client_sock.on("message", function(data) { console.log(data); client_sock.send("Thank you!"); }); // end } // connection 事件, 有客户端接入进来; function on_server_client_comming (client_sock) { console.log("client comming"); websocket_add_listener(client_sock); } server.on("connection", on_server_client_comming); // error事件,表示的我们监听错误; function on_server_listen_error(err) { } server.on("error", on_server_listen_error); // headers事件, 回给客户端的字符。 function on_server_headers(data) { // console.log(data); } server.on("headers", on_server_headers);
2. The ws module in node.js creates the client (client. js)
var ws = require("ws"); // url ws://127.0.0.1:6080 // 创建了一个客户端的socket,然后让这个客户端去连接服务器的socket var 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
<!DOCTYPE html> <html> <head> <title>websocket example</title> </head> <body> <script> var ws = new WebSocket("ws://127.0.0.1:6080/index.html"); ws.onopen = function(){ alert("open"); ws.send("WebSocket hellowrold!!"); }; ws.onmessage = function(ev){ alert(ev.data); }; ws.onclose = function(ev){ alert("close"); }; ws.onerror = function(ev){ console.log(ev); alert("error"); }; </script> </body> </html>
For more node-related knowledge, please visit: nodejs tutorial! !
The above is the detailed content of Is ws a built-in module of nodejs?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Node.js is a server-side JavaScript runtime, while Vue.js is a client-side JavaScript framework for creating interactive user interfaces. Node.js is used for server-side development, such as back-end service API development and data processing, while Vue.js is used for client-side development, such as single-page applications and responsive user interfaces.

Node.js can be used as a backend framework as it offers features such as high performance, scalability, cross-platform support, rich ecosystem, and ease of development.

To connect to a MySQL database, you need to follow these steps: Install the mysql2 driver. Use mysql2.createConnection() to create a connection object that contains the host address, port, username, password, and database name. Use connection.query() to perform queries. Finally use connection.end() to end the connection.

There are two npm-related files in the Node.js installation directory: npm and npm.cmd. The differences are as follows: different extensions: npm is an executable file, and npm.cmd is a command window shortcut. Windows users: npm.cmd can be used from the command prompt, npm can only be run from the command line. Compatibility: npm.cmd is specific to Windows systems, npm is available cross-platform. Usage recommendations: Windows users use npm.cmd, other operating systems use npm.

The following global variables exist in Node.js: Global object: global Core module: process, console, require Runtime environment variables: __dirname, __filename, __line, __column Constants: undefined, null, NaN, Infinity, -Infinity

The main differences between Node.js and Java are design and features: Event-driven vs. thread-driven: Node.js is event-driven and Java is thread-driven. Single-threaded vs. multi-threaded: Node.js uses a single-threaded event loop, and Java uses a multi-threaded architecture. Runtime environment: Node.js runs on the V8 JavaScript engine, while Java runs on the JVM. Syntax: Node.js uses JavaScript syntax, while Java uses Java syntax. Purpose: Node.js is suitable for I/O-intensive tasks, while Java is suitable for large enterprise applications.

Yes, Node.js is a backend development language. It is used for back-end development, including handling server-side business logic, managing database connections, and providing APIs.

Server deployment steps for a Node.js project: Prepare the deployment environment: obtain server access, install Node.js, set up a Git repository. Build the application: Use npm run build to generate deployable code and dependencies. Upload code to the server: via Git or File Transfer Protocol. Install dependencies: SSH into the server and use npm install to install application dependencies. Start the application: Use a command such as node index.js to start the application, or use a process manager such as pm2. Configure a reverse proxy (optional): Use a reverse proxy such as Nginx or Apache to route traffic to your application
