Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling "connection error" #21

Closed
hems opened this issue Apr 8, 2020 · 6 comments
Closed

Handling "connection error" #21

hems opened this issue Apr 8, 2020 · 6 comments
Projects

Comments

@hems
Copy link
Contributor

hems commented Apr 8, 2020

Currently when the webserver is down for maintenance i get the following error on my Browser console.

socket.js:64 WebSocket connection to 'ws://[...]/websocket' failed: Error during WebSocket handshake: Unexpected response code: 503

Is that a way of gracefully handling connection errors so they don't end up in the Browser Console and the application can deal with them programmatically, for instance showing an UI element.

@Gregivy
Copy link
Owner

Gregivy commented Apr 9, 2020

Try to use try {await server.connect()} catch(error) {/*error logic here*/}

@hems
Copy link
Contributor Author

hems commented Apr 10, 2020

Still hangs forever on node.js 12.

It never throws.. You can reproduce this way:

import ws from 'isomorphic-ws'
import simpleDDP from 'simpleddp'

const opts = {
  endpoint: 'wss://google.com',
  SocketConstructor: ws,
  reconnectInterval: 500,
};

DDP = new simpleDDP(opts, [simpleDDPLogin]);

;(async () =>{
  try {
    console.log("will hang")
    await DDP.connect()
    console.log("this never gets logged!")
  } catch(error) {
    console.log("this never happens too", error)
  }

  console.log("executed 2")
})()

On the Chrome browser, I get a console.error that comes from socket.js to the console but it doesn't throw / get caught as well:

image

Does it catch for you?

@hems
Copy link
Contributor Author

hems commented Apr 10, 2020

If i set a "maxTimeout" then i get a Error as you can see on the [your source code here](maxTimeout: 1000) but that error has no message, but i can see the socket.js stays retrying anyway, maybe "socket.close" should also be called when the max timeout is reached?

Does the same happen to you?

image

@Gregivy
Copy link
Owner

Gregivy commented Apr 13, 2020

The error happens earlier because by default simpleDDP will try to connect to the endpoint and you are not putting new simpleDDP(opts, [simpleDDPLogin]) in a try block, but you can prevent the default with autoConnect: false:

const opts = {
  endpoint: 'wss://google.com',
  SocketConstructor: ws,
  reconnectInterval: 500,
  autoConnect: false
};

Have a look here about all the constructor parameters https://gregivy.github.io/simpleddp/simpleDDP.html, there is also autoReconnect.

@Gregivy
Copy link
Owner

Gregivy commented Apr 13, 2020

Concerning the maxTimeout it is only for method calls.

@Gregivy Gregivy added this to In Progress in v 2.3.0 Jun 4, 2020
@Gregivy
Copy link
Owner

Gregivy commented Jun 4, 2020

Also don't forget that you have these events:

server.on('connected', () => {
    // do something
});

server.on('disconnected', () => {
    // for example show alert to user
});

server.on('error', (e) => {
    // global errors from server
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
v 2.3.0
In Progress
Development

No branches or pull requests

2 participants