NIUCLOUD是一款SaaS管理后台框架多应用插件+云编译。上千名开发者、服务商正在积极拥抱开发者生态。欢迎开发者们免费入驻。一起助力发展! 广告
> 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); ```