-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathangular-socket.js
More file actions
102 lines (94 loc) · 3.95 KB
/
angular-socket.js
File metadata and controls
102 lines (94 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
*http://code.tutsplus.com/tutorials/more-responsive-single-page-applications-with-angularjs-socketio-creating-the-library--cms-21738
*/
var module = angular.module('socket.io', []);
module.provider('$socket', function $socketProvider() {
var ioUrl = '';
var ioConfig = {};
function setOption(name, value, type) {
if (typeof value != type) {
throw new TypeError("'"+ name +"' must be of type '"+ type + "'");
}
ioConfig[name] = value;
}
this.setResource = function setResource(value) {
setOption('resource', value, 'string');
};
this.setConnectTimeout = function setConnectTimeout(value) {
setOption('connect timeout', value, 'number');
};
this.setTryMultipleTransports = function setTryMultipleTransports(value) {
setOption('try multiple transports', value, 'boolean');
};
this.setReconnect = function setReconnect(value) {
setOption('reconnect', value, 'boolean');
};
this.setReconnectionDelay = function setReconnectionDelay(value) {
setOption('reconnection delay', value, 'number');
};
this.setReconnectionLimit = function setReconnectionLimit(value) {
setOption('reconnection limit', value, 'number');
};
this.setMaxReconnectionAttempts = function setMaxReconnectionAttempts(value) {
setOption('max reconnection attempts', value, 'number');
};
this.setSyncDisconnectOnUnload = function setSyncDisconnectOnUnload(value) {
setOption('sync disconnect on unload', value, 'boolean');
};
this.setAutoConnect = function setAutoConnect(value) {
setOption('auto connect', value, 'boolean');
};
this.setFlashPolicyPort = function setFlashPolicyPort(value) {
setOption('flash policy port', value, 'number')
};
this.setForceNewConnection = function setForceNewConnection(value) {
setOption('force new connection', value, 'boolean');
};
this.setConnectionUrl = function setConnectionUrl(value){
if ('string' !== typeof value) {
throw new TypeError("setConnectionUrl error: value must be of type 'string'");
}
ioUrl = value;
}
this.$get = function $socketFactory($rootScope) {
var socket = io(ioUrl, ioConfig);
return {
on : function on(event, callback){
socket.on(event, function(){
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
var args = arguments;
// $apply faz com que a callback possa invocar variaveis
// em $scope que tenham sido declaradas pela
// directiva ng-model ou {{}}
$rootScope.$apply(function () {
// este callback.apply regista a função callback que
// poderá conter referencias à variavel socket
callback.apply(socket, args);
});
});
},
off: function off(event, callback) {
if (typeof callback == 'function') {
//neste caso o callback nao tem acesso a $scope nem à
//scope definida pela variavel socket
socket.removeListener(event, callback);
} else {
socket.removeAllListeners(event);
}
},
emit: function emit(event, data, callback) {
if (typeof callback == 'function') {
socket.emit(event, data, function () {
var args = arguments;
$rootScope.$apply(function () {
callback.apply(socket, args);
});
});
}
else{
socket.emit(event, data);
}
}
};
};
});