mirror of
https://github.com/taobataoma/meanTorrent.git
synced 2026-01-15 11:52:23 +01:00
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.
43 lines
1.2 KiB
JavaScript
43 lines
1.2 KiB
JavaScript
'use strict';
|
|
|
|
// Create the 'chat' controller
|
|
angular.module('chat').controller('ChatController', ['$scope', 'Socket',
|
|
function($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);
|
|
});
|
|
|
|
// Create a controller method for sending messages
|
|
$scope.sendMessage = function() {
|
|
// Create a new message object
|
|
var message = {
|
|
text: this.messageText
|
|
};
|
|
|
|
// Emit a 'chatMessage' message event
|
|
Socket.emit('chatMessage', message);
|
|
|
|
// Clear the message text
|
|
this.messageText = '';
|
|
};
|
|
|
|
// Remove the event listener when the controller instance is destroyed
|
|
$scope.$on('$destroy', function() {
|
|
Socket.removeListener('chatMessage');
|
|
});
|
|
|
|
}
|
|
]);
|