-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path$ws.js
210 lines (173 loc) · 6.04 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
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
/**
* This WebSocket library allows one to emit and receive WebSocket events/messages.
* Unlike Socket.io and other libraries, sawKit-client (or rather "$ws-client")...
* ...allows for a JSON.parsable message-object to be sent without major coordination...
* ...between front and back-end libraries; this allows for the message to still be sent...
* ...by the client and received by server - it just requires that the server JSON.parses the...
* ...message to look for an "_event" property (which describes the event/emission from client)...
* ...and a "_data" property which contains the data sent from the client. Alternatively, use ...
* ...sawKit-node which already has the on-event-emit functionality already built in.
*
* IMPORTANT!: Pull-Requests will be ignored if it strays from the object's prototypal design.
*
*/
Function.prototype.method = function(name, fn){ // for().method().chaining()
this.prototype[name] = fn;
return this;
};
String.prototype.forEach = Array.prototype.forEach; // polyfill (forEach on chars)
var $ws = window.$ws = (window.$ws || (function(){
function WS(){
var self = this;
return function connect(uri){
self.ws = new WebSocket((uri || 'localhost:9696' || 'ws://e.ws.ndn.ucla.edu:9696'));
return {
ws: self.ws,
ready: self.connected,
send: self.send,
emit: self.emit,
sendBinary: self.sendBinary,
on: self.on,
settings: self.settings
};
};
};
WS.prototype = (function(){
var self, sendCallback = {};
/**
* PRIVATE METHODS
*/
var util = (function(){
var timestamp = (new Date).getTime();
var promise = (function(){
var promises = promises || [];
function mise(promise){ // pro.mise() (promise it)
promises.push(promise);
return promises;
};
function gress(key, callback){ // pro.gress() (progress to)
promises.forEach(function(it, i, a){
(it.event === key) && callback(it); //
});
};
return {
promises: promises,
mise: mise,
gress: gress
};
})();
function messageBridge(creds){
var promise = {}, creds = creds, data = creds.data, event = creds.event;
(creds.action === 'send') && (function(){
(event === 'message') && (function(){
data = JSON.stringify(data);
}())
||
(event !== 'message') && (function(){
data = JSON.stringify({_data: data, _event: event});
}());
}())
||
((creds.action === 'receive') && (function(){
var raw = data
, event = 'message'
, cons = data.data.constructor
, msg = (cons !== ArrayBuffer && cons !== Uint8Array && cons !== Blob) && JSON.parse(raw.data) || data.data;
(msg.constructor === Object) && (function(){
event = ((msg._event) && msg._event) || event;
msg = ((msg._data) && msg._data) || msg;
}());
util.pro.gress(event, function(_promise){
_promise.data = msg, _promise.raw = raw, _promise.event = event; promise = _promise;
});
}()));
return {
data: data, // for emit
promise: promise // for onmessage
};
};
return {
ts: timestamp,
pro: promise,
msgBridge: messageBridge
};
})();
/**
* PUBLIC METHODS
*/
function connected(callback){
self = this; // this changes in event callbacks, preserve now.
this.ws.onopen = function(e){
console.log('WebSocket Connection Made. Event:', e);
callback(self, self.ws);
};
this.ws.onmessage = function(data){
self.on({data: data});
};
return this;
};
function sendBinary(binary, binaryType){
this.settings({binaryType: 'arraybuffer'});
this.ws.send(binary);
};
function send(data, callback){
if(!data) throw new Error('Error: no data exists to send.');
(data && callback) && this.emit('message', data, callback);
(!callback) && this.emit('message', data);
return this;
};
function emit(event, data, callback){
if(!data || data.constructor === Function) throw new Error("Error: data-parameter is absent or is a function.");
var formattedData = (data) && util.msgBridge({action: 'send', data: data,event: event}).data;
this.ws.send(formattedData);
console.log('message sent...');
(callback && callback(true));
return this;
};
function on(event, callback){
var promise, promises, cons = event.constructor;
(cons === String && callback) && (function(){
promises = util.pro.mise({event: event, callback: callback});
}())
||
(cons === Object && !callback) && (function(){
promise = util.msgBridge({action: 'receive', data: event.data}).promise;
(promise.callback) && promise.callback(promise.data, promise.raw, promise);
}())
||
(
(cons === ArrayBuffer || cons === Uint8Array)
&&
!callback
) && (function(){
promise = util.msgBridge({action: 'receive', data: event.data}).promise;
(promise.callback) && promise.callback(promise.data, promise.raw, promise);
}());
return this;
};
function settings(opts){
var changes = [];
for(opt in opts){
this.ws[opt] = opts[opt];
changes.push(opt);
}
console.log('settings changed for each of these:', changes, this.ws);
return this;
};
function gettings(){};
function close(){
console.log('close:', WS);
return this;
};
return {
connected: connected,
send: send,
emit: emit,
sendBinary: sendBinary,
on: on,
settings: settings,
get: gettings
};
})();
return new WS();
})());