-
Notifications
You must be signed in to change notification settings - Fork 29
/
pomelo-client.js
238 lines (201 loc) · 5.51 KB
/
pomelo-client.js
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
(function (exports, global) {
var Protocol = exports;
var HEADER = 5;
var Message = function(id,route,body){
this.id = id;
this.route = route;
this.body = body;
};
/**
*
*pomele client encode
* id message id;
* route message route
* msg message body
* socketio current support string
*
*/
Protocol.encode = function(id, route, msg){
var msgStr = JSON.stringify(msg);
if (route.length>255) { throw new Error('route maxlength is overflow'); }
var byteArray = new Uint16Array(HEADER + route.length + msgStr.length);
var index = 0;
byteArray[index++] = (id>>24) & 0xFF;
byteArray[index++] = (id>>16) & 0xFF;
byteArray[index++] = (id>>8) & 0xFF;
byteArray[index++] = id & 0xFF;
byteArray[index++] = route.length & 0xFF;
for(var i = 0;i<route.length;i++){
byteArray[index++] = route.charCodeAt(i);
}
for (var i = 0; i < msgStr.length; i++) {
byteArray[index++] = msgStr.charCodeAt(i);
}
return bt2Str(byteArray,0,byteArray.length);
};
/**
*
*client decode
*msg String data
*return Message Object
*/
Protocol.decode = function(msg){
var idx, len = msg.length, arr = new Array( len );
for ( idx = 0 ; idx < len ; ++idx ) {
arr[idx] = msg.charCodeAt(idx);
}
var index = 0;
var buf = new Uint16Array(arr);
var id = ((buf[index++] << 24) | (buf[index++]) << 16 | (buf[index++]) << 8 | buf[index++]) >>>0;
var routeLen = buf[HEADER - 1];
var route = bt2Str(buf,HEADER, routeLen + HEADER);
var body = bt2Str(buf,routeLen + HEADER,buf.length);
return new Message(id,route,body);
};
var bt2Str = function(byteArray,start,end) {
var result = "";
for(var i = start; i < byteArray.length && i<end; i++) {
result = result + String.fromCharCode(byteArray[i]);
};
return result;
}
})('object' === typeof module ? module.exports : (this.Protocol = {}), this);
(function() {
if (typeof Object.create !== 'function') {
Object.create = function (o) {
function F() {}
F.prototype = o;
return new F();
};
}
var root = window;
var pomelo = Object.create(EventEmitter.prototype); // object extend from object
root.pomelo = pomelo;
var socket = null;
var id = 1;
var callbacks = {};
var encode = null;
var decode = null;
pomelo.init = function(params, cb) {
pomelo.params = params;
params.debug = true;
var host = params.host;
var port = params.port;
encode = params.encode || defaultEncode;
decode = params.decode || defaultDecode;
var url = 'ws://' + host;
if(port) {
url += ':' + port;
}
console.log('connecto to ' + url);
socket = io.connect(url, {'force new connection': true, reconnect: true});
socket.on('connect', function(){
console.log('[pomeloclient.init] websocket connected!');
if (cb) {
cb(socket);
}
});
socket.on('reconnect', function() {
console.log('reconnect');
});
socket.on('message', function(data){
if(decode) {
data = decode(data);
}
if(data instanceof Array) {
processMessageBatch(pomelo, data);
} else {
processMessage(pomelo, data);
}
});
socket.on('error', function(err) {
console.log(err);
});
socket.on('disconnect', function(reason) {
pomelo.emit('disconnect', reason);
});
};
pomelo.disconnect = function() {
if(socket) {
socket.disconnect();
socket = null;
}
};
var defaultEncode = pomelo.encode = function(reqId, route, msg) {
return Protocol.encode(id, route, msg);
};
var defaultDecode = pomelo.decode = function(data) {
if(typeof data === 'string') {
data = JSON.parse(data);
}
return data;
};
pomelo.request = function(route) {
if(!route) {
return;
}
var msg = {};
var cb;
arguments = Array.prototype.slice.apply(arguments);
if(arguments.length === 2){
if(typeof arguments[1] === 'function'){
cb = arguments[1];
} else if(typeof arguments[1] === 'object'){
msg = arguments[1];
}
} else if(arguments.length === 3){
msg = arguments[1];
cb = arguments[2];
}
id++;
callbacks[id] = cb;
sendMessage(id, route, msg);
};
pomelo.notify = function(route, msg) {
msg = msg || {};
sendMessage(0, route, msg);
};
var sendMessage = function(reqId, route, msg) {
if(encode) {
msg = encode(reqId, route, msg);
}
socket.send(msg);
};
var processMessage = function(pomelo, msg) {
var route;
if(msg.id) {
//if have a id then find the callback function with the request
var cb = callbacks[msg.id];
delete callbacks[msg.id];
if(typeof cb !== 'function') {
console.log('[pomeloclient.processMessage] cb is not a function for request ' + msg.id);
return;
}
cb(msg.body);
return;
}
// server push message or old format message
processCall(msg);
//if no id then it should be a server push message
function processCall(msg) {
var route = msg.route;
if(!!route) {
if (!!msg.body) {
var body = msg.body.body;
if (!body) {body = msg.body;}
pomelo.emit(route, body);
} else {
pomelo.emit(route,msg);
}
} else {
pomelo.emit(msg.body.route,msg.body);
}
}
};
var processMessageBatch = function(pomelo, msgs) {
for(var i=0, l=msgs.length; i<l; i++) {
processMessage(pomelo, msgs[i]);
}
};
module.exports = pomelo;
})();