37 lines
1.2 KiB
JavaScript
37 lines
1.2 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
angular.module('Services', []).
|
||
|
factory('Socket', function ($rootScope) {
|
||
|
var socket = io.connect();
|
||
|
|
||
|
return {
|
||
|
on: function(eventName, callback) {
|
||
|
socket.on(eventName, function() {
|
||
|
var args = arguments;
|
||
|
$rootScope.$apply(function() {
|
||
|
callback.apply(socket, args);
|
||
|
});
|
||
|
});
|
||
|
},
|
||
|
emit: function(eventName, data, callback) {
|
||
|
if(typeof data == 'function') {
|
||
|
callback = data;
|
||
|
data = {};
|
||
|
}
|
||
|
socket.emit(eventName, data, function() {
|
||
|
var args = arguments;
|
||
|
$rootScope.$apply(function() {
|
||
|
if(callback) {
|
||
|
callback.apply(socket, args);
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
},
|
||
|
emitAndListen: function(eventName, data, callback) {
|
||
|
this.emit(eventName, data, callback);
|
||
|
this.on(eventName, callback);
|
||
|
}
|
||
|
};
|
||
|
});
|
||
|
|
||
|
angular.module('roomstateapp', ['roomstateapp.controllers', 'Services']);
|