🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
> websocket server ``` /** * ws接口服务 */ const WebSocketServer = require('ws'); const wss = new WebSocketServer.Server({ port: 8080 }); // 服务 wss.on('connection', function connection(ws) { ws.isAlive = true; ws.on('pong', function () { this.isAlive = true; }); // 首次发送 ws.send('hello!'); // 收到数据 ws.on('message', function incoming(message) { }); }); // 心跳检测 const interval = setInterval(function ping() { wss.clients.forEach(function each(ws) { if (ws.isAlive === false) return ws.terminate(); ws.isAlive = false; ws.ping(function () { }); }); }, 30000); ```