Skip to content

Commit

Permalink
reconnect to chat server when user back to state chat
Browse files Browse the repository at this point in the history
  • Loading branch information
taobataoma committed May 12, 2017
1 parent 241f6e6 commit 4aabcbd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
34 changes: 20 additions & 14 deletions modules/chat/client/controllers/chat.client.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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');
});
}

/**
Expand Down
7 changes: 7 additions & 0 deletions modules/core/client/services/socket.io.client.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
function Socket(Authentication, $state, $timeout) {
var service = {
connect: connect,
disconnect: disconnect,
emit: emit,
on: on,
removeListener: removeListener,
Expand All @@ -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) {
Expand Down

0 comments on commit 4aabcbd

Please sign in to comment.