Comments (20)
@nonotest Thanks~! You could wrap/overwrite the reconnect
method after you initialize Sockette.
Since the onclose
event is passed to reconnect()
directly, you can assert against/abort the reconnection.
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
};
from sockette.
Hey,
You may need to update? The latest version will gracefully shutdown on ws.close()
. Previous versions attempted a reconnection if a 1000
code was explicitly passed.
from sockette.
hi @lukeed, im using the following version im my project:
"sockette": "^2.0.0",
And reconnection happens when i force close execution, i don't see any special handling for this logic in source.
from sockette.
Im looking the following lines:
https://github.com/lukeed/sockette/blob/master/src/index.js#L45
https://github.com/lukeed/sockette/blob/master/src/index.js#L12
from sockette.
Yes. The WebSocket.close
internally defaults to 1000
, which we accept as graceful. Similarly, servers that terminate the connection without a explicit code come down as 1005
, which is also considered graceful.
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.
from sockette.
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, ... }
from sockette.
Mmm,
im using react native, probably it needs special handling ... i will check if close code is the same, i tell you in few minutes
from sockette.
@lukeed in react native (with iOS) im getting the following event:
{ code: 1001, reason: 'Stream end encountered' }
by the moment im do special handling for close operations
from sockette.
Okay, I would just do ws.close(1000)
. Sockette is made for browsers and so follows the browser specifications.
from sockette.
Thanks @lukeed im try with your code but no luck, i go with websockets for react native π thanks again =)
from sockette.
Okay, sorry I couldn't be of more help! But glad to know that about RN.
from sockette.
Reproduced the problem perfectly. It's definitely something with the library and it's compatibility with React.
from sockette.
React Native you mean?
from sockette.
Maybe when websockets are part of the core nodejs/node#19308
from sockette.
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
from sockette.
If you could post or send me a gist of your React code that'd be great and I'll try to reproduce tonight!
from sockette.
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) -> { code: 1001, reason: 'Stream end encountered' }
then sockette attempts to reconnect at least once.
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.
I can then turn off reconnecting easily.
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!
from sockette.
that's great thanks! btw sorry didn't realise the issue was closed before!
maybe could add a note in the readme for react-native users :)
from sockette.
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.
from sockette.
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 am not on the latest RN version (55.3) so not 100% sure if it's the intended behaviour thus I would not automatically exclude it for browsers.
I will try with RN latest and do some more research later and update you!
thank you.
from sockette.
Related Issues (20)
- Disable reconnect programmatically HOT 15
- Access underlying socket object HOT 3
- SCRIPT5022: SecurityError in IE11 HOT 1
- onerror not returning code HOT 2
- ws.close should clear pending setTimeout HOT 1
- Still in CONNECTING state HOT 1
- Still reconnecting after close() HOT 5
- didunmount
- Bump npm version to include #48 HOT 1
- Able to detect network lost? HOT 1
- Possible to get WebSocket instance? HOT 1
- How to get error code HOT 1
- When I close the connection, it doesn't send queryStringParameters HOT 1
- Socket connection code choices HOT 2
- [Feature] Ability to set url before reconnecting HOT 1
- Automatically split & re-combine messages over a certain size HOT 5
- Increment timeout HOT 2
- Sockette.close can unexpectedly clear other timeouts/intervals
- project status? HOT 2
- Put an option to always reconnect, no matter what
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sockette.