-
Notifications
You must be signed in to change notification settings - Fork 67
/
blynk-browser.js
66 lines (55 loc) · 1.63 KB
/
blynk-browser.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
/* Copyright (c) 2015 Volodymyr Shymanskyy. See the file LICENSE for copying permission. */
var events = require('events');
var util = require('util');
if (!window.WebSocket) {
window.WebSocket = window.MozWebSocket;
}
function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
}
function str2ab(str) {
var buf = new ArrayBuffer(str.length); // 2 bytes for each char
var bufView = new Uint8Array(buf);
for (var i=0, strLen=str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
exports.WsClient = function(options) {
var self = this;
events.EventEmitter.call(this);
var options = options || {};
self.addr = options.addr || "blynk-cloud.com";
self.port = options.port || 8082;
self.path = options.path || "/websockets";
this.write = function(data) {
if (self.sock) {
self.sock.send(str2ab(data));
}
};
this.connect = function(done) {
if (self.sock) {
self.sock.close();
}
try {
self.sock = new WebSocket('ws://' + self.addr + ':' + self.port + options.path);
self.sock.binaryType = 'arraybuffer';
self.sock.onopen = function(evt) { done() };
self.sock.onclose = function(evt) { self.emit('end'); };
self.sock.onmessage = function(evt) {
var data = ab2str(evt.data);
self.emit('data', data);
};
self.sock.onerror = function(evt) { self.emit('end'); };
} catch(exception){
console.log(exception);
}
};
this.disconnect = function() {
if (self.sock) {
self.sock.close();
self.sock = null;
}
};
};
util.inherits(exports.WsClient, events.EventEmitter);