Files
meanTorrent/modules/core/client/services/socket.io.client.service.js
Ryan Hutchison b2462ec86c feat(core): Modify core module to implement style guidelines.
Update the core module to implement the style guidelines.
Reduce size of init.js - moved filter logic out to it's own config.
Rename Menus to menuService
2016-03-23 15:41:57 -04:00

58 lines
1.2 KiB
JavaScript

(function () {
'use strict';
// Create the Socket.io wrapper service
angular
.module('core')
.factory('Socket', Socket);
Socket.$inject = ['Authentication', '$state', '$timeout'];
function Socket(Authentication, $state, $timeout) {
var service = {
connect: connect,
emit: emit,
on: on,
removeListener: removeListener,
socket: null
};
connect();
return service;
// Connect to Socket.io server
function connect() {
// Connect only when authenticated
if (Authentication.user) {
service.socket = io();
}
}
// Wrap the Socket.io 'emit' method
function emit(eventName, data) {
if (service.socket) {
service.socket.emit(eventName, data);
}
}
// Wrap the Socket.io 'on' method
function on(eventName, callback) {
if (service.socket) {
service.socket.on(eventName, function (data) {
$timeout(function () {
callback(data);
});
});
}
}
// Wrap the Socket.io 'removeListener' method
function removeListener(eventName) {
if (service.socket) {
service.socket.removeListener(eventName);
}
}
}
}());