Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
Merge pull request #701 from mleanos/socket-connect
Browse files Browse the repository at this point in the history
Socket IO client enhancement with connect() method
  • Loading branch information
lirantal committed Jul 25, 2015
2 parents 7c5b311 + 65c6d1f commit 0c76179
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
8 changes: 8 additions & 0 deletions modules/chat/client/controllers/chat.client.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 9 additions & 6 deletions modules/core/client/services/socket.io.client.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 0c76179

Please sign in to comment.