Quantcast
Channel: Active questions tagged crash - Stack Overflow
Viewing all articles
Browse latest Browse all 7191

Nodejs + ws: how to simulate client crash and unclosed TCP connection?

$
0
0

I am using Node.js + ws for TCP websocket connections (and I am new to ws).

The current code consists of a server program and a test client program.

The test client program code looks like the following:

const ws = require('ws');const wss = new ws.WebSocket('ws://127.0.0.1:8080');wss.on('open', function open() {    const arr = {'message' : 'hello'};    wss.send(JSON.stringify(arr));});wss.on('message', function incoming(message) {    console.log(JSON.parse(message));});setTimeout(() => process.exit(), 5000);

and the server program code looks like the following:

const ws = require('ws');const wss = new ws.WebSocketServer({port: 8080});wss.on('connection', function(connection) {    connection.on('close', function() {        console.log('connection closed');    });});

Now when the process.exit() in test client is called, the server program immediately knows that the TCP connection is closed and "connection closed" is outputted.

What I want:

What I want is to simulate the situation where the test client program crashes and the TCP connection is not properly closed.

First, I want to know whether it is even possible to crash the test client without closing the TCP connection or does the websocket server program somehow keeps pinging and when no response received then it immediately made the decision to call the on('close') callback.

Second, if it is indeed possible for such scenario to happen, then how to simulate it?

What I have tried:

Besides the process.exit() shown in the code I also tried kill -9 pid and ctrl+c to kill the program, but everything I did ends up with the server outputting connection closed.


Viewing all articles
Browse latest Browse all 7191

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>