首先创建一个目录,cd
到目录下, npm init -y
一路回车, 安装一个插件 npm i websocket
建一个server.js
文件
const WebSocketServer = require('websocket').server
const http = require('http')
const port = 8000
let time = 0
// 创建服务器
const server = http.createServer();
server.listen(port, () => {
console.log(`${new Date().toLocaleDateString()} Server is listening on port ${port}`)
})
// websocket 服务器
const wsServer = new WebSocketServer({
httpServer: server
})
// 建立连接
wsServer.on('request', (request) => {
// 当前的连接
console.log(request.origin, '=======request.origin=======')
const connection = request.accept(null, request.origin)
console.log(`${new Date().toLocaleDateString()} 已经建立连接`)
//心跳
标签:Node,const,log,console,server,connection,WebSocket,message,搭建
From: https://www.cnblogs.com/wgb1234/p/17432740.html