How to terminate a WebSocket connection?

后端 未结 5 1463
执笔经年
执笔经年 2020-12-17 14:18

Is it possible to terminate a websocket connection from server without closing the entire server? If it is then, how can I achieve it?

Note: I\'m using NodeJS as bac

5条回答
  •  囚心锁ツ
    2020-12-17 15:21

    If you use var client = net.createConnection() to create the socket you can use client.destroy() to destroy it.

    With ws it should be:

    var server = new WebSocketServer();
    server.on('connection', function (socket) {
      // Do something and then
      socket.close(); //quit this connection
    });
    

提交回复
热议问题