-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcfugolight.js
More file actions
178 lines (121 loc) · 2.44 KB
/
cfugolight.js
File metadata and controls
178 lines (121 loc) · 2.44 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
https://github.com/datalog/centrifuge-js
under MIT license
# cfugolight.js has no dependencies
Copyright (c) 2020 Constantine
*/
/**
Sometimes, the best tool is absence of tool.
*/
/**
Centrifuge API:
CONNECT : 0
SUBSCRIBE : 1
UNSUBSCRIBE : 2
PUBLISH : 3
PRESENCE : 4
PRESENCE_STATS : 5
HISTORY : 6
PING : 7
SEND : 8
RPC : 9
REFRESH : 10
SUB_REFRESH : 11
*/
'use strict';
var
url = 'ws://centrifuge.example.com/connection/websocket'
,cnl = 'news' /* <-- channel name */
,tkn = '[[[TOKEN]]]' /* <-- should be issued by server */
;
___cfupdate();
function ___parsejsondata( d ) {
try {
return JSON.parse( d );
} catch( e ) {}
return {};
};
function ___decodedata( data ) {
var
msgs = [];
data.split('\n').forEach( function( d ) {
if( d ) {
msgs.push( ___parsejsondata( d ) );
}
} );
return msgs;
}
function ___cfupdate( attempt ) {
var
ws = new WebSocket( url ),
maxdelay = 25,
attempt = ( attempt ) ? attempt : 1,
_msgcounter = 0,
_revive = 1;
function ecc( error ) {
var
err = error.code,
msg = error.message,
actions = {
109 : function() {
console.log('wsocket error [ ' + msg + ' ]');
_revive = 0;
ws.close();
return false;
}
};
return actions[ err ] ? actions[ err ]() : false;
}
function wspush( msg ) {
msg.id = ++_msgcounter;
ws.send( JSON.stringify( msg ) );
};
ws.onclose = function( e ) {
console.log('wsocked was closed [ ' + e.code + ' ]');
var
reconnect = ___parsejsondata( e.reason ).reconnect;
if( _revive || reconnect ) {
setTimeout( function() {
___cfupdate( ++attempt );
}, !reconnect * (( attempt > maxdelay ) ? maxdelay : attempt ) * 1000 );
}
};
ws.onerror = function() {
_revive = 1;
};
ws.onopen = function() {
attempt = 0;
wspush( {
params : {
token : tkn
}
/** method : 0 */
} );
};
ws.onmessage = function( e ) {
/** CAUTION! We can receive more than one line */
___decodedata( e.data ).forEach( function( msg ) {
var
error = msg.error || 0,
result = msg.result || {},
client = result.client,
data = result.data;
if( error ) {
/** error code and explanation comes as a last cry with json */
return ecc( error );
}
if( client ) {
wspush( {
params : {
channel : cnl
}
,method : 1
} );
} else
if( data ) {
/* MESSAGE FROM CFSERVER RECEIVED */
console.log( data );
}
} );
};
}