forked from opensourceBIM/BIMserver-JavaScript-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbimserverapiwebsocket.ts
More file actions
159 lines (147 loc) · 4.29 KB
/
Copy pathbimserverapiwebsocket.ts
File metadata and controls
159 lines (147 loc) · 4.29 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
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
import WebSocket from "ws";
class BimServerApiWebSocket {
connected: boolean;
openCallbacks: any[];
endPointId: any;
listener: any;
tosend: any[];
tosendAfterConnect: any[];
messagesReceived = 0;
intervalId: any;
baseUrl: string;
bimServerApi: any;
_ws: any;
endpointid: any;
constructor(baseUrl: string, bimServerApi: any) {
this.connected = false;
this.openCallbacks = [];
this.endPointId = null;
this.listener = null;
this.tosend = [];
this.tosendAfterConnect = [];
this.messagesReceived = 0;
this.intervalId = null;
this.baseUrl = baseUrl;
this.bimServerApi = bimServerApi;
}
connect(callback = null) {
if (this.connected) {
if (callback != null) {
callback();
}
return Promise.resolve();
}
console.info("Connecting websocket");
var promise = new Promise((resolve, reject) => {
this.openCallbacks.push(() => {
resolve();
});
if (callback != null) {
if (typeof callback === "function") {
this.openCallbacks.push(callback);
} else {
console.error("Callback was not a function", callback);
}
}
const location = this.bimServerApi.baseUrl.toString().replace('http://', 'ws://').replace('https://', 'wss://') + "/stream";
try {
this._ws = new WebSocket(location);
this._ws.binaryType = "arraybuffer";
this._ws.onopen = this._onopen.bind(this);
this._ws.onmessage = this._onmessage.bind(this);
this._ws.onclose = this._onclose.bind(this);
this._ws.onerror = this._onerror.bind(this);
} catch (err) {
console.error(err);
this.bimServerApi.notifier.setError("WebSocket error" + (err.message !== undefined ? (": " + err.message) : ""));
}
});
return promise;
}
_onerror(err) {
console.log(err);
this.bimServerApi.notifier.setError("WebSocket error" + (err.message !== undefined ? (": " + err.message) : ""));
}
_onopen() {
this.intervalId = setInterval(() => {
this.send({"hb": true});
}, 30 * 1000); // Send hb every 30 seconds
while (this.tosendAfterConnect.length > 0 && this._ws.readyState == 1) {
const messageArray = this.tosendAfterConnect.splice(0, 1);
this._sendWithoutEndPoint(messageArray[0]);
}
}
_sendWithoutEndPoint(message) {
if (this._ws && this._ws.readyState == 1) {
this._ws.send(message);
} else {
this.tosendAfterConnect.push(message);
}
}
_send(message) {
if (this._ws && this._ws.readyState == 1 && this.endPointId != null) {
this._ws.send(message);
} else {
console.log("Waiting", message);
this.tosend.push(message);
}
}
send(object) {
const str = JSON.stringify(object);
this.bimServerApi.log("Sending", str);
this._send(str);
}
_onmessage(message) {
this.messagesReceived++;
if (this.messagesReceived % 10 === 0) {
// console.log(this.messagesReceived);
}
if (message.data instanceof ArrayBuffer) {
this.listener(message.data);
} else {
const incomingMessage = JSON.parse(message.data);
if (incomingMessage.id != null) {
var id = incomingMessage.id;
if (this.bimServerApi.websocketCalls.has(id)) {
var fn = this.bimServerApi.websocketCalls.get(id);
fn(incomingMessage);
this.bimServerApi.websocketCalls.delete(id);
}
} else {
this.bimServerApi.log("incoming", incomingMessage);
if (incomingMessage.welcome !== undefined) {
this._sendWithoutEndPoint(JSON.stringify({"token": this.bimServerApi.token}));
} else if (incomingMessage.endpointid !== undefined) {
this.endPointId = incomingMessage.endpointid;
this.connected = true;
this.openCallbacks.forEach((callback) => {
callback();
});
while (this.tosend.length > 0 && this._ws.readyState == 1) {
const messageArray = this.tosend.splice(0, 1);
console.log(messageArray[0]);
this._send(messageArray[0]);
}
this.openCallbacks = [];
} else {
if (incomingMessage.request !== undefined) {
this.listener(incomingMessage.request);
} else if (incomingMessage.requests !== undefined) {
incomingMessage.requests.forEach((request) => {
this.listener(request);
});
}
}
}
}
}
_onclose(m) {
console.log("WebSocket closed", m);
clearInterval(this.intervalId);
this._ws = null;
this.connected = false;
this.openCallbacks = [];
this.endpointid = null;
}
}
export { BimServerApiWebSocket };