TLDR: My websockets stop connecting after a while, though they work perfectly at first. How do I fix it?
I have a C# web service that makes use of two websockets (via websocket-sharp). They are defined like so:
public WebSocketServer sock1= new WebSocketServer("ws://localhost:802");
sock1.Log.Level = WebSocketSharp.LogLevel.Error;
sock1.Log.File = "LOG\\logfile.LOG";
sock1.AddWebSocketService<StatusWebSocket1>("/");
sock1.KeepClean = false;
sock1.Start();
public WebSocketServer sock2= new WebSocketServer("ws://localhost:803");
sock2.Log.Level = WebSocketSharp.LogLevel.Error;
sock2.Log.File = "LOG\\logfile.LOG";
sock2.AddWebSocketService<StatusWebSocket2>("/");
sock2.KeepClean = false;
sock2.Start();
These websocket servers accept client connections from my angularjs app:
$scope.socket = null;
var startStopPolling = function(action){
if(action == 'close'){
$scope.socket.close(3001, 'User leaving page.');
return;
}
var path = 'ws://' + window.location.hostname + ':802/';
$http({method: 'GET', url: apiRoot+'is-remote', timeout: 1000})
.success(function(response) {
path = 'wss://' + window.location.hostname + '/';
}).finally(function(){
$scope.socket = new WebSocket(path);
$scope.socket.onerror = function(event){
console.log('SOCKET ERROR', event);
$timeout(startStopPolling, 1000);
};
$scope.socket.onopen = function(){
var selfInfo = {
locationCode: $scope.LocationCode,
token: BrowserStorageService.get('token'),
type: 'ClientRegistration',
mode: 'status'
};
$scope.socket.send(JSON.stringify(selfInfo));
$scope.socket.onmessage = function(event){
var data = JSON.parse(event.data);
//do some stuff with data
});
};
};
});
};
startStopPolling();
And elsewhere:
var socket2 = null;
var startStopPolling2 = function(action){
if(action == 'close'){
socket2.close(3001, 'App closing.');
return;
}
var path2 = 'ws://' + window.location.hostname + ':803/';
socket2 = new WebSocket(path2);
socket2.onerror = function(event){
console.log('SOCKET ERROR', event);
$timeout(startStopPolling2, 1000);
};
socket2.onopen = function(){
var selfInfo = {
mode: 'status2',
type: 'ClientRegistration'
};
socket2.send(JSON.stringify(selfInfo));
socket2.onmessage = function(event){
var data = JSON.parse(event.data);
console.log('status2', data.status2);
if(data.status2 == "Closed"){
$state.go("status");
}
};
};
};
startStopPolling2();
$rootScope.$on('$destroy', function(){
startStopPolling2('close');
});
The application mostly sits on one page. (We display a status page most of the time, but allow the user to navigate away to do things like manage configuration.) When we receive a certain status value from the status2 socket (socket2
), we want to do some things, so we have to keep track of that all the time. The other socket is only relevant when on that page, so we want to get rid of it when we don't need it.
Here's the problem: The app periodically refreshes the page via a call like location.reload(true)
. Wile it works perfectly at first, after so many of these refreshes, the sockets no longer connect. There are no errors in the console, and neither onOpen()
function fires. What's going on, and how do I fix it?