-
-
Notifications
You must be signed in to change notification settings - Fork 81
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
Force websocket close without reconnection how? #17
Comments
Hey, You may need to update? The latest version will gracefully shutdown on |
hi @lukeed, im using the following version im my project:
And reconnection happens when i force close execution, i don't see any special handling for this logic in source. |
Im looking the following lines: https://github.com/lukeed/sockette/blob/master/src/index.js#L45 |
Yes. The This probably depends on your usage and/or your browser? Although I'm not aware of any browsers that don't comply to this spec. |
Here's quick little test you can paste in an empty tab: function noop() {}
function Sockette(url, opts) {
opts = opts || {};
let k, ws, num, $={}, self=this;
let ms=opts.timeout || 1e3, max=opts.maxAttempts || Infinity;
$.onmessage = opts.onmessage || noop;
$.onclose = e => {
(e.code !== 1e3 && e.code !== 1005) && self.reconnect(e);
(opts.onclose || noop)(e);
};
$.onerror = e => {
(e && e.code==='ECONNREFUSED') ? self.reconnect(e) : (opts.onerror || noop)(e);
};
$.onopen = e => {
num=0; (opts.onopen || noop)(e);
};
self.open = () => {
ws = new WebSocket(url, opts.protocols);
for (k in $) ws[k] = $[k];
};
self.reconnect = e => {
(num++ < max) ? setTimeout(_ => {
(opts.onreconnect || noop)(e);
self.open();
}, ms) : (opts.onmaximum || noop)(e);
};
self.json = x => {
ws.send(JSON.stringify(x));
};
self.send = x => {
ws.send(x);
};
self.close = (x, y) => {
ws.close(x, y);
};
self.open(); // init
return self;
}
ws = new Sockette('wss://echo.websocket.org', {
onopen: e => console.log('OPENED', e),
onconnect: e => console.log('connected', e),
onreconnect: e => console.log('reconnecting...', e),
onmessage: e => console.log('HELLO', e.data),
onclose: e => console.log('closed!', e),
onerror: e => console.log('error', e),
});
//=> OPENED {...}
ws.close();
//=> closed! {isTrusted: true, wasClean: true, code: 1005, ... } |
Mmm, |
@lukeed in react native (with iOS) im getting the following event:
by the moment im do special handling for close operations |
Okay, I would just do |
Thanks @lukeed im try with your code but no luck, i go with websockets for react native 👍 thanks again =) |
Okay, sorry I couldn't be of more help! But glad to know that about RN. |
Reproduced the problem perfectly. It's definitely something with the library and it's compatibility with React. |
React Native you mean? |
Maybe when websockets are part of the core nodejs/node#19308 |
I'm thinking it's react as a whole right now. I reproduced it using React Web. I also noticed that it did not respect the maximum retries, and continued to try until I killed it |
If you could post or send me a gist of your React code that'd be great and I'll try to reproduce tonight! |
hello, I encountered same problem with react native (note that react works fine for me - which makes sense since its the normal javascript websocket lib) . ws.close(1000) -> The only workaround I could think of is using the close code argument passed in the self.close handler instead of relying on just the ws event code. hacky but i no better idea at the moment. I could do a PR but I doubt that's something you would want in your lib! |
@nonotest Thanks~! You could wrap/overwrite the Since the It sounds like RN is doing something funky – and tbh, I've never used or plan to use RN so I can't really help in that particular context. let foo = new Sockette({ ... });
// save old function
let _reconnect = foo.reconnect.bind(foo);
// overwrite it
foo.reconnect = e => {
if (e && e.code === 1001) return; // abort
_reconnect(e); // invoke old fn
}; |
that's great thanks! btw sorry didn't realise the issue was closed before! |
Sure, will do. Do you happen to know why it's closing with 1001 code? Maybe can point me to some relevant documentation? Because if that is the standard behavior for RN (and not specific to your app) then I think that is a code I can exclude for browser users too. |
Hello, When using a browser and connecting to the same server, I get the correct close code (using sockette as well), so I would definitely say there is something happening on the RN side. I have found a few people talking about it https://github.com/facebook/react-native/issues/12678 but the issue was closed without much discussion.. I will try with RN latest and do some more research later and update you! thank you. |
Hi pal,
How do you intentional close the websocket without reconnection execution, its necessary to pass event code to close method?
when manually invoke close method I think that the reconnection should not be done
thanks
The text was updated successfully, but these errors were encountered: