forked from yunba/yunba-javascript-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyunba-1.0.1.js
More file actions
311 lines (260 loc) · 9.59 KB
/
yunba-1.0.1.js
File metadata and controls
311 lines (260 loc) · 9.59 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
var Yunba;
var DEF_SERVER = 'sock.yunba.io';
var DEF_PORT = 3000;
var QOS0 = 0;
var QOS1 = 1;
var QOS2 = 2;
var SUB_CHANNEL_LIST = [];//已经订阅的频道或频道列表
var MSG_MISSING_MESSAGE = 'Missing Message';
var MSG_MISSING_CHANNEL = 'Missing Channel';
var MSG_SUB_FAIL = '订阅失败';
var MSG_MISSING_CALLBACK = 'Missing Callback';
var MSG_SUB_REPEAT_ERROR = '不能重复订阅一个频道';
var MSG_UNSUB_FAIL = '取消订阅操作失败';
var MSG_CONNECT_FAIL = '连接 Yunba 服务失败';
var MSG_DISCONNECT_FAIL = '关闭连接失败';
var MSG_NO_THIS_CHANNEL = '未订阅该频道';
var MSG_PUB_FAIL = '信息发布失败';
var MSG_NEED_CONNECT = '请先连接到 Yunba 服务';
var MSG_NEED_SOCKET_CONNECT = 'JavaScript SDK 与消息服务器已经断开链接,请刷新页面重新链接。';
var __bind = function (fn, me) {
return function () {
return fn.apply(me, arguments);
};
};
var __error = function (msg) {
console.log(msg);
return true;
}
Array.prototype.contain = function (val) {
for (var i = 0; i < this.length; i++) {
if (this[i] == val) {
return true;
}
}
return false;
};
Array.prototype.indexOf = function (val) {
for (var i = 0; i < this.length; i++) {
if (this[i] == val) return i;
}
return -1;
};
Array.prototype.remove = function (val) {
var index = this.indexOf(val);
if (index > -1) {
this.splice(index, 1);
}
};
Yunba = (function () {
function Yunba(setup) {
setup = setup || {};
this.server = setup['server'] || DEF_SERVER;
this.port = setup['port'] || DEF_PORT;
if (!setup['appkey']) {
return false;
} else {
this.appkey = setup['appkey'];
}
this.connected = false;//mqtt连接状态
this.socket_connected = false;//socket.io连接状态
}
Yunba.prototype.init = function (init_callback, rec_callback) {
var me = this;
init_callback = init_callback || function () {
};
rec_callback = rec_callback || function(){};
me.message_cb = function() {};
// me.receive_msg_cb_list = {};
me.get_alias_cb = function() {};
me.set_alias_cb = function() {};
try {
console.log('js client start init...');
me.socket = io.connect('http://' + this.server + ':' + this.port);
me.socket.on('connect', function () {
console.log('js client init success.');
me.socket_connected = true;
init_callback(true);
});
me.socket.on('error',function(e){
console.log('js client init error:',e);
me.socket_connected=false;
init_callback(false);
});
me.socket.on('disconnect', function () {
console.log('js client disconnect.');
me.socket_connected = false;
init_callback(false);
});
me.socket.on('reconnect', function () {
console.log('js client reconnect.');
if (rec_callback) {
rec_callback();
}
});
me.socket.on('reconnect_failed', function () {
console.log('js client reconnect failed.');
});
me.socket.on('puback', function (result) {
if (result.success) {
if (me.puback_cb)
me.puback_cb(true, {messageId: result.messageId});
} else {
if (me.puback_cb)
me.puback_cb(false, MSG_PUB_FAIL);
return __error(MSG_PUB_FAIL);
}
});
me.socket.on('message', function (data) {
me.message_cb(data);
});
me.socket.on('alias', function(data) {
me.get_alias_cb(data);
})
me.socket.on('set_alias_ack', function(data) {
me.set_alias_cb(data);
})
me.socket.on('suback', function (data) {
if (data.success) {
//如果订阅成功,则监听来自服务端的message消息
if (me.suback_cb)
me.suback_cb(true);
} else {
if (me.suback_cb)
me.suback_cb(false, MSG_SUB_FAIL)
return __error(MSG_SUB_FAIL);
}
});
me.socket.on('unsuback', function (result) {
if (result.success) {
if (me.unsuback_cb)
me.unsuback_cb(true);
} else {
if (me.unsuback_cb)
me.unsuback_cb(false, MSG_UNSUB_FAIL);
return __error(MSG_UNSUB_FAIL);
}
});
me.socket.on('connack', function (result) {
if (result.success) {
me.connected = true
if (me.connack_cb)
me.connack_cb(true);
} else {
if (me.connack_cb)
me.connack_cb(false, result.msg);
}
});
} catch (err) {
return __error(MSG_CONNECT_FAIL) && init_callback(false, MSG_CONNECT_FAIL);
}
};
Yunba.prototype.connect = function (callback) {
if(this.socket_connected === false){
return false;
}
this.connack_cb = callback;
try {
this.socket.emit('connect', {appkey: this.appkey});
} catch (err) {
return __error(MSG_SOCKET_EMIT_ERROR) && callback(false, MSG_SOCKET_EMIT_ERROR);
}
};
Yunba.prototype.disconnect = function (callback) {
var self = this;
if (!self.connected) {
return callback(true);
}
try {
this.socket.emit('disconn', {});
self.connected = false;
callback(true);
} catch (err) {
return __error(MSG_SOCKET_EMIT_ERROR) && callback(false, MSG_SOCKET_EMIT_ERROR);
}
};
Yunba.prototype.set_message_cb = function (cb) {
this.message_cb = cb;
}
Yunba.prototype.subscribe = function (args, cb1, cb2) {
if (this.socket_connected === false) {
return false;
}
var self = this;
var channel = args['topic'];
var qos = args['qos'] || QOS1;
this.suback_cb = args['callback'] || cb1 || function () {
};
if (!this.connected) {
return __error(MSG_NEED_CONNECT) && this.suback_cb(false, MSG_NEED_CONNECT);
}
if (!this.suback_cb) return __error(MSG_MISSINGl_CALLBACK);
if (!channel) return __error(MSG_MISSING_CHANNEL) && this.suback_cb(false, MSG_MISSING_CHANNEL);
//检查是否已经订阅该频道
if (SUB_CHANNEL_LIST.contain(channel)) {
// return __error(MSG_SUB_REPEAT_ERROR) && this.suback_cb(false, MSG_SUB_REPEAT_ERROR);
} else {
SUB_CHANNEL_LIST.push(channel);
}
try {
this.socket.emit('subscribe', { 'topic': channel, 'qos': qos });
} catch (err) {
return __error(MSG_SOCKET_EMIT_ERROR) && this.suback_cb(false, MSG_SOCKET_EMIT_ERROR);
}
};
Yunba.prototype.unsubscribe = function (args, callback) {
if (this.socket_connected === false) {
return false;
}
this.unsuback_cb = callback;
if (!this.connected) {
return __error(MSG_NEED_CONNECT) && callback(false, MSG_NEED_CONNECT);
}
var channel = args['topic'];
var callback = args['callback'] || callback || function () {
};
if (!channel) return __error(MSG_MISSING_CHANNEL) && callback(false, MSG_MISSING_CHANNEL);
//检查是否已经订阅该频道
if (!SUB_CHANNEL_LIST.contain(channel)) {
return __error(MSG_NO_THIS_CHANNEL) && callback(false, MSG_NO_THIS_CHANNEL);
}
try {
this.socket.emit('unsubscribe', { 'topic': channel});
} catch (err) {
return __error(MSG_SOCKET_EMIT_ERROR) && callback(false, MSG_SOCKET_EMIT_ERROR);
}
};
Yunba.prototype.publish = function (args, callback) {
if(this.socket_connected === false){
return false;
}
if (!this.connected) {
return __error(MSG_NEED_CONNECT) && callback(false, MSG_NEED_CONNECT);
}
this.puback_cb = callback;
var channel = args['topic'] || args['channel'];
var msg = args['msg'];
var qos = args['qos'] || QOS1;
var callback = args['callback'] || callback || function () {
};
if (!channel) return __error(MSG_MISSING_CHANNEL) && callback(false, MSG_MISSING_CHANNEL);
if (!msg) return __error(MSG_MISSING_MESSAGE) && callback(false, MSG_MISSING_MESSAGE);
try {
this.socket.emit('publish', {'topic': channel, 'msg': msg, 'qos': qos });
} catch (err) {
return __error(MSG_SOCKET_EMIT_ERROR) && callback(false, MSG_SOCKET_EMIT_ERROR);
}
};
Yunba.prototype.publish_to_alias = function (args, callback) {
this.socket.emit('publish_to_alias', args, callback);
};
Yunba.prototype.set_alias = function (args, callback) {
this.set_alias_cb = callback;
this.socket.emit('set_alias', args);
}
Yunba.prototype.get_alias = function (callback) {
this.get_alias_cb = callback;
this.socket.emit('get_alias');
}
return Yunba;
})();