-
Notifications
You must be signed in to change notification settings - Fork 32
/
ws.js
160 lines (132 loc) · 3.17 KB
/
ws.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
(function umd(factory) {
if (typeof define === 'function' && define.amd) {
define(['./wormhole'], factory);
} else if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = factory(require('./wormhole'));
} else {
window.wormhole.WS = factory(window.wormhole);
}
})(function factory(wormhole) {
'use strict';
var ns = '__wormhole.js/ws__:';
var META_CMD = ns + 'meta:cmd';
var SEND_CMD = ns + 'send:cmd';
var META_EVENT = ns + 'meta';
var MESSAGE_EVENT = ns + 'msg';
/**
* Wormhole WebSocket (proxy object)
* @param {string} url
* @param {[]string} [protocols]
* @param {womrhole.Hole} [hole]
*/
function WS(url, protocols, hole) {
var _this = this;
_this.master = false;
_this.url = url;
_this.protocols = protocols;
_this.hole = hole = (hole || wormhole());
// Meta info about connection state
_this.meta = {
error: null,
closed: null,
};
// Subscribe on change meta info
hole.on(META_EVENT, function (meta) {
_this.handleMeta(meta);
});
// Subscribe on WebSocket.onmessage
hole.on(MESSAGE_EVENT, function (evt) {
_this.onmessage(evt);
});
hole.on('master', function () {
_this.connect();
});
// Register command on master for getting meta info for slaves
hole[META_CMD] = function () {
return _this.meta;
};
// Register command for send data to WS
hole[SEND_CMD] = function (data) {
_this.socket.send(data);
};
if (hole.master) {
_this.connect();
} else {
hole.call(META_CMD, null, function (err, meta) {
err && console.warn('[meta] wormhole.js/ws:', err);
meta && _this.handleMeta(meta)
});
}
}
WS.prototype = {
constructor: WS,
/** @private */
handleMeta: function (meta) {
var m = this.meta;
this.meta = meta;
if (meta.error) {
!m.error && this.onerror(meta.error);
} else if (meta.closed) {
!m.closed && this.onclose(meta.closed);
} else if (meta.closed === false) {
(m.closed !== false) && this.onopen({type: 'open'});
}
},
/** @private */
setMeta: function (meta) {
this.hole.emit(META_EVENT, meta);
},
/** @private */
connect: function () {
var _this = this;
var socket = new WebSocket(_this.url, _this.protocols);
_this.socket = socket;
socket.onopen = function (evt) {
_this.setMeta({
closed: false,
});
};
socket.onclose = function (evt) {
this.setMeta({
closed: {
type: evt.type,
wasClean: evt.wasClean,
code: evt.code,
reason: evt.reason,
},
});
};
socket.onerror = function (evt) {
this.setMeta({
error: {
type: evt.type,
message: evt.message,
},
});
};
socket.onmessage = function (evt) {
_this.hole.emit(MESSAGE_EVENT, {
type: evt.type,
data: evt.data,
});
};
_this.master = true;
_this.onmaster({type: 'master'});
},
onmaster: function (evt) {},
onopen: function (evt) {},
onmessage: function (evt) {},
onclose: function (evt) {},
onerror: function (evt) {},
/**
* Send data to WebSocket
* @param {string} data
*/
send: function (data) {
this.hole.call(SEND_CMD, data);
},
};
// Export
WS['default'] = WS;
return WS;
});