forked from opensourceBIM/BIMserver-JavaScript-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbimserverapiwebsocket.js
More file actions
119 lines (109 loc) · 3.49 KB
/
Copy pathbimserverapiwebsocket.js
File metadata and controls
119 lines (109 loc) · 3.49 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
export default class BimServerApiWebSocket {
constructor(baseUrl, bimServerApi) {
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 (callback != null && 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) : ""));
}
}
_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);
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;
}
}