- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathangular-socket.js
More file actions
Latest commit
102 lines (94 loc) · 3.95 KB
/
Copy pathangular-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
*/
varmodule=angular.module('socket.io',[]);
module.provider('$socket',function$socketProvider(){
varioUrl='';
varioConfig={};
functionsetOption(name,value,type){
if(typeofvalue!=type){
thrownewTypeError("'"+name+"' must be of type '"+type+"'");
}
ioConfig[name]=value;
}
this.setResource=functionsetResource(value){
setOption('resource',value,'string');
};
this.setConnectTimeout=functionsetConnectTimeout(value){
setOption('connect timeout',value,'number');
};
this.setTryMultipleTransports=functionsetTryMultipleTransports(value){
setOption('try multiple transports',value,'boolean');
};
this.setReconnect=functionsetReconnect(value){
setOption('reconnect',value,'boolean');
};
this.setReconnectionDelay=functionsetReconnectionDelay(value){
setOption('reconnection delay',value,'number');
};
this.setReconnectionLimit=functionsetReconnectionLimit(value){
setOption('reconnection limit',value,'number');
};
this.setMaxReconnectionAttempts=functionsetMaxReconnectionAttempts(value){
setOption('max reconnection attempts',value,'number');
};
this.setSyncDisconnectOnUnload=functionsetSyncDisconnectOnUnload(value){
setOption('sync disconnect on unload',value,'boolean');
};
this.setAutoConnect=functionsetAutoConnect(value){
setOption('auto connect',value,'boolean');
};
this.setFlashPolicyPort=functionsetFlashPolicyPort(value){
setOption('flash policy port',value,'number')
};
this.setForceNewConnection=functionsetForceNewConnection(value){
setOption('force new connection',value,'boolean');
};
this.setConnectionUrl=functionsetConnectionUrl(value){
if('string'!==typeofvalue){
thrownewTypeError("setConnectionUrl error: value must be of type 'string'");
}
ioUrl=value;
}
this.$get=function$socketFactory($rootScope){
varsocket=io(ioUrl,ioConfig);
return{
on : functionon(event,callback){
socket.on(event,function(){
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
varargs=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: functionoff(event,callback){
if(typeofcallback=='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: functionemit(event,data,callback){
if(typeofcallback=='function'){
socket.emit(event,data,function(){
varargs=arguments;
$rootScope.$apply(function(){
callback.apply(socket,args);
});
});
}
else{
socket.emit(event,data);
}
}
};
};
});