ThinkSSL🔒 一键申购 5分钟快速签发 30天无理由退款 购买更放心 广告
> 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); ```