|
@@ -8,8 +8,9 @@ angular.module('chatServices', [])
|
|
username: '',
|
|
username: '',
|
|
error: null,
|
|
error: null,
|
|
messages: [],
|
|
messages: [],
|
|
|
|
+ status: 'disconnected',
|
|
isConnected: function() {
|
|
isConnected: function() {
|
|
- return this.username != '';
|
|
|
|
|
|
+ return this.status == 'connected';
|
|
},
|
|
},
|
|
addListener: function(f) {
|
|
addListener: function(f) {
|
|
messageListeners.add(f);
|
|
messageListeners.add(f);
|
|
@@ -43,7 +44,9 @@ angular.module('chatServices', [])
|
|
}
|
|
}
|
|
|
|
|
|
service.connect = function(username) {
|
|
service.connect = function(username) {
|
|
|
|
+ if(service.status != 'disconnected') return;
|
|
service.error = null;
|
|
service.error = null;
|
|
|
|
+ service.status = 'connecting';
|
|
chatSocket = new WS(jsRoutes.controllers.Application.chat(username, getRoom()).webSocketURL());
|
|
chatSocket = new WS(jsRoutes.controllers.Application.chat(username, getRoom()).webSocketURL());
|
|
chatSocket.onmessage = wrap(function(event) {
|
|
chatSocket.onmessage = wrap(function(event) {
|
|
var message = JSON.parse(event.data);
|
|
var message = JSON.parse(event.data);
|
|
@@ -56,6 +59,7 @@ angular.module('chatServices', [])
|
|
});
|
|
});
|
|
chatSocket.onopen = wrap(function() {
|
|
chatSocket.onopen = wrap(function() {
|
|
$rootScope.$broadcast('ws:connected', username);
|
|
$rootScope.$broadcast('ws:connected', username);
|
|
|
|
+ service.status = 'connected';
|
|
service.username = username;
|
|
service.username = username;
|
|
ping = $interval(function() {
|
|
ping = $interval(function() {
|
|
service.send('/ping');
|
|
service.send('/ping');
|
|
@@ -63,6 +67,7 @@ angular.module('chatServices', [])
|
|
});
|
|
});
|
|
chatSocket.onclose = wrap(function() {
|
|
chatSocket.onclose = wrap(function() {
|
|
$rootScope.$broadcast('ws:disconnected');
|
|
$rootScope.$broadcast('ws:disconnected');
|
|
|
|
+ service.status = 'disconnected';
|
|
service.username = '';
|
|
service.username = '';
|
|
$interval.cancel(ping);
|
|
$interval.cancel(ping);
|
|
});
|
|
});
|
|
@@ -93,12 +98,6 @@ angular.module('chatServices', [])
|
|
}
|
|
}
|
|
service.messages.push(message);
|
|
service.messages.push(message);
|
|
},
|
|
},
|
|
- getError: function() {
|
|
|
|
- return Connection.error;
|
|
|
|
- },
|
|
|
|
- connect: Connection.connect,
|
|
|
|
- disconnect: Connection.disconnect,
|
|
|
|
- isConnected: Connection.isConnected,
|
|
|
|
send: Connection.send
|
|
send: Connection.send
|
|
};
|
|
};
|
|
|
|
|