简体   繁体   中英

Javascript Websocket Close = Server crash

Hi was trying to send some data back and forth, and I found out that when I closed my client my server crashed...

My Server is Node.JS using the nodejs-websocket library.

Then I found out you had to add this.

window.onbeforeunload = function() {
    connection.close()
}

Is there away around this? Because if someone wanted to crash your server couldn't he just remove that from the JavaScript?

Please keep in mind this is my first time using websockets

Server Side:

var ws = require("nodejs-websocket");
var server = ws.createServer(function(conn){
        console.log("New connection")


        conn.on("end", function(code,reason){
            console.log('Connection Lost')
        });
}).listen(8001);

Error:

events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: read ECONNRESET
    at exports._errnoException (util.js:1018:11)
    at TCP.onread (net.js:568:26)

You may just have to listen to connection error events on your socket, and it won't throw an exception (and close your app). At least that's what this answer says: Node js ECONNRESET

Basically, try this first:

var ws = require("nodejs-websocket");
var server = ws.createServer(function(conn){
        console.log("New connection");

        conn.on("error", function(err) {
            console.log("Error caught: ")
            console.log(err.stack)
        });

        conn.on("end", function(code,reason){
            console.log('Connection Lost')
        });
}).listen(8001);

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