forked from pubnub/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket.js
More file actions
111 lines (99 loc) · 3.72 KB
/
websocket.js
File metadata and controls
111 lines (99 loc) · 3.72 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
(function(){
// ---------------------------------------------------------------------------
// WEBSOCKET INTERFACE
// ---------------------------------------------------------------------------
var WS = PUBNUB['ws'] = function( url, protocols ) {
if (!(this instanceof WS)) return new WS( url, protocols );
var self = this
, url = self.url = url || ''
, protocol = self.protocol = protocols || 'Sec-WebSocket-Protocol'
, bits = url.split('/')
, setup = {
'ssl' : bits[0] === 'wss:'
,'origin' : bits[2]
,'publish_key' : bits[3]
,'subscribe_key' : bits[4]
,'channel' : bits[5]
};
// READY STATES
self['CONNECTING'] = 0; // The connection is not yet open.
self['OPEN'] = 1; // The connection is open and ready to communicate.
self['CLOSING'] = 2; // The connection is in the process of closing.
self['CLOSED'] = 3; // The connection is closed or couldn't be opened.
// CLOSE STATES
self['CLOSE_NORMAL'] = 1000; // Normal Intended Close; completed.
self['CLOSE_GOING_AWAY'] = 1001; // Closed Unexpecttedly.
self['CLOSE_PROTOCOL_ERROR'] = 1002; // Server: Not Supported.
self['CLOSE_UNSUPPORTED'] = 1003; // Server: Unsupported Protocol.
self['CLOSE_TOO_LARGE'] = 1004; // Server: Too Much Data.
self['CLOSE_NO_STATUS'] = 1005; // Server: No reason.
self['CLOSE_ABNORMAL'] = 1006; // Abnormal Disconnect.
// Events Default
self['onclose'] = self['onerror'] =
self['onmessage'] = self['onopen'] =
self['onsend'] = function(){};
// Attributes
self['binaryType'] = '';
self['extensions'] = '';
self['bufferedAmount'] = 0;
self['trasnmitting'] = false;
self['buffer'] = [];
self['readyState'] = self['CONNECTING'];
// Close if no setup.
if (!url) {
self['readyState'] = self['CLOSED'];
self['onclose']({
'code' : self['CLOSE_ABNORMAL'],
'reason' : 'Missing URL',
'wasClean' : true
});
return self;
}
// PubNub WebSocket Emulation
self.pubnub = PUBNUB['init'](setup);
self.pubnub.setup = setup;
self.setup = setup;
self.pubnub['subscribe']({
'restore' : false,
'channel' : setup['channel'],
'disconnect' : self['onerror'],
'reconnect' : self['onopen'],
'error' : function() {
self['onclose']({
'code' : self['CLOSE_ABNORMAL'],
'reason' : 'Missing URL',
'wasClean' : false
});
},
'callback' : function(message) {
self['onmessage']({ 'data' : message });
},
'connect' : function() {
self['readyState'] = self['OPEN'];
self['onopen']();
}
});
};
// ---------------------------------------------------------------------------
// WEBSOCKET SEND
// ---------------------------------------------------------------------------
WS.prototype.send = function(data) {
var self = this;
self.pubnub['publish']({
'channel' : self.pubnub.setup['channel'],
'message' : data,
'callback' : function(response) {
self['onsend']({ 'data' : response });
}
});
};
// ---------------------------------------------------------------------------
// WEBSOCKET CLOSE
// ---------------------------------------------------------------------------
WS.prototype.close = function() {
var self = this;
self.pubnub['unsubscribe']({ 'channel' : self.pubnub.setup['channel'] });
self['readyState'] = self['CLOSED'];
self['onclose']({});
};
})();