Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My node.js server randomly shuts down after a client has connected from a web and then closes the tab (simple disconnection).

The server:
JavaScript
var websocketServer = require("ws").Server, 
    server = new websocketServer({port: 9000});

var date = require("./modules/date");

console.log("[" + date.timestamp() + "] creating the websocket server..");

server.on("connection", function(websocket) {
	console.log("[" + date.timestamp() + "] receiving a connection from " + websocket._socket.remoteAddress);

    websocket.on("message", function(message) {
        console.log("received: %s", message); 
        websocket.send(message);
    });
    websocket.send("something");
});

console.log("[" + date.timestamp() + "] server is open for connections on port 9000");



Client:
JavaScript
var socket;

$(document).ready(function() {
	socket = new WebSocket("ws://127.0.0.1:9000");

	socket.onopen = function(){
		if(stage == "5fb26f935a4358dce53e6ce67a08c1b5e73dc7f8c554615ae33ac1d6ecb214b6") {
			stage = "27e1c570b1f1e08cb35019c63f3830828ce18f70fa95eecfcfa84f1e019c3929";
			$("section.loader").animate({"top": "-100%"}, 1000);
		}
	};

	socket.onmessage = function(e) {
		alert("recieved: " + e.data);
	};

	socket.onerror = function(e) {
		if(stage == "5fb26f935a4358dce53e6ce67a08c1b5e73dc7f8c554615ae33ac1d6ecb214b6") {
			$("body").append("<div class='error'>\
					Something went wrong!<br>The server could not respond to your request, please try again in a bit.\
				</div>");
			$("body div.error").animate({"bottom": "0px"}, 500);

			setTimeout(function(){
				$("body div.error").animate({"bottom": "-58px"}, 500, function() {
					location.reload();
				});
			}, 5000);
		}
	};

	socket.onclose = function() {
	};
});


What I have tried:

I'm fairly new to node.js and I don't know what else to really try to edit since I can't see any malfunction code by my eye.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900