简体   繁体   中英

nodejs + WS Read Timeout

I am using nodejs ws websocket library. How can set socket read timeout on ws library?

var WebSocketServer = require('ws').Server, 
    wss = new WebSocketServer({ port: 8080 });

wss.on('connection', function connection(ws) {

ws.on('message', function incoming(message) {
        console.log('received: %s', message);
    });
});

You can setTimeout as example below:

ws.on('message', function message(data, flags) {
  console.log('Roundtrip time: ' + (Date.now() - parseInt(data)) + 'ms', flags);

  setTimeout(function timeout() {
    ws.send(Date.now().toString(), {mask: true});
  }, 500);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM