-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathwebsocket.js
125 lines (106 loc) · 3.48 KB
/
websocket.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
/**
* Wrapper class for { WebSocket }.
*
* @param {string} url
* @param {string} binaryType -> 'blob' or 'arraybuffer'
*/
function Websocket(url, binaryType="blob"){
this.url = url;
this.listeners = {};
this.socketEvents = ['open', 'close', 'error', 'message'];
this.socket = new WebSocket(url);
this.readyState = this.socket.readyState;
this.protocol = this.socket.protocol;
this.extensions = this.socket.extensions;
this.bufferedAmount = this.socket.bufferedAmount;
this.binaryType = binaryType;
this.socket.binaryType = this.binaryType;
var self = this;
var reconnect = (u) =>{
var socket = new WebSocket(u);
socket.binaryType = this.socket.binaryType;
for (var e in this.socketEvents){
for(var cb in this.listeners[e]){
socket.addEventListener(e, cb);
}
}
this.socket.addEventListener('close', closeEvent);
this.socket = socket;
this.dispatchEvent('reconnect', this.socket);
}
function closeEvent (e){
if(e.code == 1011){
console.log(`Could not connect to websocket. reason=${e.reason}`);
}else{
reconnect(self.url);
}
}
this.socket.addEventListener('close', closeEvent);
}
Websocket.prototype.addEventListener = function(event, callback){
if(this.socketEvents.includes(event)){
this.socket.addEventListener(event, callback);
}
if (!( event in this.listeners )){
this.listeners[event] = []
}
this.listeners[event].push(callback);
}
Websocket.prototype.removeEventListener = function(event, callback){
if (!( event in this.listeners )){
return;
}
for (var i = 0, l = this.listeners[event].length; i < l; i++) {
if (this.listeners[event][i] === callback){
this.listeners[event].splice(i, 1);
return;
}
}
}
Websocket.prototype.dispatchEvent = function(event, data){
if (!( event in this.listeners )){
return;
}
for(var i=0; i < this.listeners[event].length; i++){
this.listeners[event][i].call(this, data);
}
}
Websocket.prototype.send = function(data){
this.socket.send(data);
}
function PTTWebsocket(url, binaryType="arraybuffer"){
Websocket.call(this, url, binaryType);
this.appEvents = ['started', 'stopped']
var messageEvent = (e) =>{
if (typeof e.data === "string"){
if(e.data == 'ping'){ // keep-alive
this.socket.send('pong');
}else if(this.appEvents.includes(e.data)){
this.dispatchEvent(e.data, e);
}else{
try{
var o = JSON.parse(e.data);
if('meta' in o){
this.dispatchEvent('metadata', o.meta);
}else{
// do nothing for now!
}
}catch(e){
// do nothing!
}
}
}else{
var base64String = btoa(
new Uint8Array(e.data)
.reduce((onData, byte) => onData + String.fromCharCode(byte), ''));
this.dispatchEvent('binary', base64String);
}
}
this.addEventListener('message', messageEvent);
}
PTTWebsocket.prototype = Object.create(Websocket.prototype);
Object.defineProperty(PTTWebsocket.prototype, 'constructor', {
value: PTTWebsocket,
enumerable: false,
writable: true
});