reconnect to chat server when user back to state chat

This commit is contained in:
OldHawk
2017-05-13 00:03:15 +08:00
parent 241f6e67bf
commit 4aabcbd384
2 changed files with 27 additions and 14 deletions

View File

@@ -28,6 +28,26 @@
$state.go('home');
}
registerCallback();
// Make sure the Socket is connected
if (!Socket.socket) {
Socket.connect();
registerCallback();
}
// Remove the event listener when the controller instance is destroyed
$scope.$on('$destroy', function () {
Socket.disconnect();
Socket.removeListener('chatMessage');
Socket.removeListener('usersList');
Socket.removeListener('join');
Socket.removeListener('quit');
Socket.removeListener('ban');
});
}
function registerCallback() {
// add an event listener to the 'error' event
Socket.on('error', function (err) {
var message = {
@@ -61,20 +81,6 @@
Socket.on('quit', function (message) {
vm.onUserQuit(message);
});
// Make sure the Socket is connected
if (!Socket.socket) {
Socket.connect();
}
// Remove the event listener when the controller instance is destroyed
$scope.$on('$destroy', function () {
Socket.removeListener('chatMessage');
Socket.removeListener('usersList');
Socket.removeListener('join');
Socket.removeListener('quit');
Socket.removeListener('ban');
});
}
/**

View File

@@ -11,6 +11,7 @@
function Socket(Authentication, $state, $timeout) {
var service = {
connect: connect,
disconnect: disconnect,
emit: emit,
on: on,
removeListener: removeListener,
@@ -29,6 +30,12 @@
}
}
// disconnect to Socket.io server
function disconnect() {
service.socket.disconnect();
service.socket = null;
}
// Wrap the Socket.io 'emit' method
function emit(eventName, data) {
if (service.socket) {