mirror of
https://github.com/umap-project/umap.git
synced 2025-04-28 19:42:36 +02:00
feat(sync): Check for websocket connectivity and retry if not connected.
Because the WebSockets do not offer a way to know when they failed to connect, use a delay and check the state of the connection.
This commit is contained in:
parent
b7ca961912
commit
4ba5bbc542
4 changed files with 17 additions and 3 deletions
|
@ -70,6 +70,7 @@ export class SyncEngine {
|
|||
* Authenticate with the server and start the transport layer.
|
||||
*/
|
||||
async authenticate() {
|
||||
console.log("authenticating")
|
||||
const [response, _, error] = await this._server.get(this._websocketTokenURI)
|
||||
if (!error) {
|
||||
this.start(response.token)
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
const PONG_TIMEOUT = 5000;
|
||||
const PING_INTERVAL = 30000;
|
||||
const FIRST_CONNECTION_TIMEOUT = 2000;
|
||||
|
||||
export class WebSocketTransport {
|
||||
constructor(webSocketURI, authToken, messagesReceiver) {
|
||||
|
@ -21,6 +22,13 @@ export class WebSocketTransport {
|
|||
}
|
||||
}
|
||||
|
||||
this.ensureOpen = setInterval(() => {
|
||||
if (this.websocket.readyState !== WebSocket.OPEN) {
|
||||
this.websocket.close()
|
||||
clearInterval(this.ensureOpen)
|
||||
}
|
||||
}, FIRST_CONNECTION_TIMEOUT)
|
||||
|
||||
// To ensure the connection is still alive, we send ping and expect pong back.
|
||||
// Websocket provides a `ping` method to keep the connection alive, but it's
|
||||
// unfortunately not possible to access it from the WebSocket object.
|
||||
|
|
|
@ -236,8 +236,10 @@ U.Map = L.Map.extend({
|
|||
},
|
||||
|
||||
render: function (fields) {
|
||||
console.log("sync.websocketConnected", this.sync.websocketConnected)
|
||||
console.log("options.syncEnabled", this.options.syncEnabled)
|
||||
if (this.options.syncEnabled === true) {
|
||||
if (!this.sync.websocketConnected) {
|
||||
if (this.sync.websocketConnected !== true) {
|
||||
const template = `
|
||||
<h3><i class="icon icon-16"></i><span>${L._('Disconnected')}</span></h3>
|
||||
<p>
|
||||
|
|
|
@ -196,4 +196,7 @@ def run(host: str, port: int):
|
|||
logging.debug(f"Waiting for connections on {host}:{port}")
|
||||
await asyncio.Future() # run forever
|
||||
|
||||
try:
|
||||
asyncio.run(_serve())
|
||||
except KeyboardInterrupt:
|
||||
print("Closing WebSocket server")
|
||||
|
|
Loading…
Reference in a new issue