I am using WebSockets in a Web application. In Chrome and Firefox it all works. In the Edge browser, after some time of inactivity simply breaks the connection. And in the console appears the message:
websocket error: network error 12030, the connection with the server was terminated abnormally.
It is important for the application that the connection is permanent.
The following are parts of the server and the client:
Server:
var options = {
key: fs.readFileSync(path.join(__dirname, 'cert/server.key')),
cert: fs.readFileSync(path.join(__dirname, 'cert/server.crt'))
};
var server = https.createServer(options, app);
wsServer = new WebSocketServer({
httpServer: server,
autoAcceptConnections: false
});
// Handle Socket Request
wsServer.on('request', function(request) {
try {
// Do Stuff
var connection = request.accept('echo-protocol', request.origin);
// Do Stuff
} catch(error) {
console.log('Error during socket request: ', error);
}
// Handle Socket message
connection.on('message', function(message) {
try {
// Do Stuff
} catch(error) {
console.log('Error while receiving message: ', error);
}
});
// Handle Socket disconnect
connection.on('close', function(reasonCode, description) {
try {
// Make sure to remove closed connections from the global pool
delete clients[connection.location][connection.id];
} catch(error) {
console.log('Error while disconnecting socket: ', error);
}
});
});
// start server, listen to port
server.listen(49152, function() {
console.log((new Date()) + ' Server is listening on port 49152');
});
Client
// connecting to the web server
var socket = new WebSocket(socketProtocol + '://' + socketServer + '', 'echo-protocol');
// listening for server response
socket.onmessage = function (message) {
// Do Stuff
};
// listening for any socket error
socket.onerror = function (error) {
console.log('WebSocket error: ' + error);
};
// listening for connection to be open
socket.onopen = function (e) {
console.log('connected');
};
Any ideas why this happens?
I've already thought about using socket.io and / or doing some sort of polling so the connection persists. But I hope there is a way that I do not have to change everything.
via ws_dev
No comments:
Post a Comment