wss.on('connection', function (client) { client.on('message', function (_message) { var _messageObj = JSON.parse(_message); _messageObj.status = 1; this.message = _messageObj; wss.broadcast(_messageObj); });
client.on('close', function() { try{ this.message = this.message || {}; this.message.status = 0; wss.broadcast(this.message); }catch(e){ console.log('刷新页面了'); } }); });
wss.broadcast = function broadcast(_messageObj) { wss.clients.forEach(function(client) { client.send(JSON.stringify(_messageObj)) }); };
|