@momo2555/koppeliajs 0.0.101 → 0.0.103
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -9,6 +9,7 @@ export declare class KoppeliaWebsocket {
|
|
|
9
9
|
onGoingRequests: OnGoingRequest[];
|
|
10
10
|
timeout: number;
|
|
11
11
|
receiveCallbacks: Callback[];
|
|
12
|
+
websocketUrl: string;
|
|
12
13
|
openCallback?: () => void;
|
|
13
14
|
/**
|
|
14
15
|
* Constructor of the Koppelia websocket class
|
|
@@ -33,6 +34,7 @@ export declare class KoppeliaWebsocket {
|
|
|
33
34
|
* @param callback
|
|
34
35
|
*/
|
|
35
36
|
onOpen(callback: () => void): void;
|
|
37
|
+
private _connectWebsocket;
|
|
36
38
|
/**
|
|
37
39
|
* Handle a websocket response
|
|
38
40
|
* - Execute a callback of a reception if a request was send before (by checking ongoing request)
|
|
@@ -5,6 +5,7 @@ export class KoppeliaWebsocket {
|
|
|
5
5
|
onGoingRequests;
|
|
6
6
|
timeout;
|
|
7
7
|
receiveCallbacks;
|
|
8
|
+
websocketUrl = "";
|
|
8
9
|
openCallback;
|
|
9
10
|
/* ------ PUBLIC ------ */
|
|
10
11
|
/**
|
|
@@ -14,7 +15,7 @@ export class KoppeliaWebsocket {
|
|
|
14
15
|
*/
|
|
15
16
|
constructor(websocketUrl, timeout = DEFAULT_WS_TIMEOUT) {
|
|
16
17
|
if (!import.meta.env.SSR) {
|
|
17
|
-
this.
|
|
18
|
+
this._connectWebsocket(websocketUrl);
|
|
18
19
|
console.log("Open new Koppelia websocket connection successfully with url ", websocketUrl);
|
|
19
20
|
this._setupEvents();
|
|
20
21
|
}
|
|
@@ -23,6 +24,7 @@ export class KoppeliaWebsocket {
|
|
|
23
24
|
this.onGoingRequests = [];
|
|
24
25
|
this.timeout = timeout;
|
|
25
26
|
this.receiveCallbacks = [];
|
|
27
|
+
this.websocketUrl = websocketUrl;
|
|
26
28
|
}
|
|
27
29
|
/**
|
|
28
30
|
* Send a new request: Add a request Id to the request and add it to the ongoing requests
|
|
@@ -62,6 +64,9 @@ export class KoppeliaWebsocket {
|
|
|
62
64
|
this.openCallback = callback;
|
|
63
65
|
}
|
|
64
66
|
/* ------ PRIVATE ------ */
|
|
67
|
+
_connectWebsocket(websockerUrl) {
|
|
68
|
+
this.socket = new WebSocket(websockerUrl);
|
|
69
|
+
}
|
|
65
70
|
/**
|
|
66
71
|
* Handle a websocket response
|
|
67
72
|
* - Execute a callback of a reception if a request was send before (by checking ongoing request)
|
|
@@ -109,6 +114,11 @@ export class KoppeliaWebsocket {
|
|
|
109
114
|
let data = JSON.parse(e.data);
|
|
110
115
|
this._handleWebsocketResponse(data);
|
|
111
116
|
});
|
|
117
|
+
// handle connection close (if an error occure)
|
|
118
|
+
this.socket.onclose = (event) => {
|
|
119
|
+
console.log(`Connection closed ${event.reason}, code=${event.code}, retry onnection ...`);
|
|
120
|
+
setTimeout(() => { this._connectWebsocket(this.websocketUrl); }, 1000);
|
|
121
|
+
};
|
|
112
122
|
}
|
|
113
123
|
/**
|
|
114
124
|
* Timeout the request if there is no response
|