Socket IO client enhancement with connect() method

Updated the Socket client service, with a connect() method. Moved state
redirect out of from Socket service.  Added the Authentication.user
check to the Chat client controller, and added a check to make sure the
Socket client service is connected to the server; if not, then connect
using the new connect() method.

Had to do a hard reset from 0.4.0 due to conflicts when merging and
pushing to remote.
This commit is contained in:
mleanos
2015-07-25 00:36:01 -07:00
parent c8880ea65d
commit 65c6d1ffe0
2 changed files with 17 additions and 6 deletions

View File

@@ -6,6 +6,14 @@ angular.module('chat').controller('ChatController', ['$scope', 'Socket',
// Create a messages array
$scope.messages = [];
// If user is not signed in then redirect back home
if (!Authentication.user) $location.path('/');
// Make sure the Socket is connected
if (!Socket.socket) {
Socket.connect();
}
// Add an event listener to the 'chatMessage' event
Socket.on('chatMessage', function(message) {
$scope.messages.unshift(message);

View File

@@ -3,12 +3,15 @@
// Create the Socket.io wrapper service
angular.module('core').service('Socket', ['Authentication', '$state', '$timeout',
function(Authentication, $state, $timeout) {
// Connect to the Socket.io server only when authenticated
if (Authentication.user) {
this.socket = io();
} else {
$state.go('home');
}
// Connect to Socket.io server
this.connect = function () {
// Connect only when authenticated
if (Authentication.user) {
this.socket = io();
}
};
this.connect();
// Wrap the Socket.io 'on' method
this.on = function(eventName, callback) {