Home >Web Front-end >Front-end Q&A >Is ws a built-in module of nodejs?

Is ws a built-in module of nodejs?

青灯夜游
青灯夜游Original
2021-12-31 12:03:013043browse

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.

Is ws 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn