当前位置:   article > 正文

ws搭建websocket服务器_new websocket.server

new websocket.server

ws搭建websocket服务器

ws模块安装

npm install ws
ws官方

uuid模块安装

npm install uuid
uuid官方

    //搭建ws服务器
    const WebSocket = require('ws');
	const WebSocketServer = require('ws').Server;
	const wss = new WebSocketServer({
		port: 8888
	});
	// 引入uuid模块
	const {
		v1: uuidv1
	} = require('uuid');
	//有客户端连接时
	wss.on('connection', (ws) => {
	//收到客户端发的消息时
	    ws.on('message', (message) => {
	        ws.send("给客户端发消息")
	    })
	})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

nodejs-websocket创建websocket服务器
两种方式的客户端创建基本是相同的,参考上方连接

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/964051
推荐阅读
相关标签
  

闽ICP备14008679号