Comments (8)
You don't need to write the reconnect behaviour yourself - SC does that automatically by default (though you can turn it off by setting the autoReconnect option here to false: http://socketcluster.io/#!/docs/api-socketcluster-client - But I wouldn't recommend it :p).
Note that in SC, when the socket becomes disconnect, all state is maintained so when you connect again, it will restore everything exactly as it was. In this case the watcher function is never removed, so it will stay bound between disconnect/reconnect.
If you want to get rid of a watcher you need to call channel.unwatch(handlerFn)
. See http://socketcluster.io/#!/docs/api-scchannel-client. Or you can call it on the socket too: socket.unwatch('channelName', handlerFn)
.
from socketcluster-client.
@jondubois thanks~
from socketcluster-client.
@jondubois Sorry I don't solve it really, you said: In this case the watcher function is never removed, so it will stay bound between disconnect/reconnect.
I set autoReconnect to true with your recommend, The watch event triggered again by page refreshed, I try to unwatching when socket becomes disconnected both client & server sides. It seems not work.
from socketcluster-client.
@smilence86 It works for me. Maybe try using the latest version of SocketCluster client and server and check that you're passing the correct arguments to functions. http://socketcluster.io/#!/docs/api-scsocket-client
from socketcluster-client.
When you call channel.unwatch(myFunction)
, make sure that myFunction is exactly the same reference as what you provided to channel.watch(myFunction)
.
from socketcluster-client.
yes, I confirm that SC is newest, this is the code below:
$(document).ready(function(){
var channel = {};
var options = {
port: 8123
};
var socket = socketCluster.connect(options);
socket.on('connect', function(status){
console.log(status);
console.log('CONNECTED');
channel = bindChannel(socket);
});
socket.on('error', function(err){
console.error('Socket error - ' + err);
});
socket.on('disconnect', function(err){
console.error('Socket disconnect - ' + err);
channel.unwatch(watcher);
// socket.unwatch('channel_demo', function(){
// console.log(arguments);
// });
});
$('#btn').click(function(){
socket.emit('event', {message: 'This is an object with a message property'});
});
socket.on('event', function(data){
$('#result').append(JSON.stringify(data) + '<br/>');
});
var watcher = function(data){
$('#result').html(data + '<br/>');
}
function bindChannel(socket){
var channel = socket.subscribe('channel_demo');
channel.on('subscribeFail', function(err){
console.log('Failed to subscribe the channel due to error: ' + err);
});
channel.watch(watcher);
//console.log(channel.watchers());
return channel;
}
});
from socketcluster-client.
@smilence86 This is expected since you are calling channel.watch(watcher)
every time the 'connect' event triggers. Note that the 'connect' event will trigger on reconnect too.
So what you're doing now is you're unwatching it on disconnect, but then as soon as it reconnects, you are setting up the watcher again.
from socketcluster-client.
@jondubois I leave my code, please help fix it. There are 3 workers, channel.watch will trigger twice when page refresh 4 times and more. But It looks well after I set only one worker, No matter page refreshed how many times.
client:
$(document).ready(function(){
var channel = {};
var options = {
port: 8123
};
var socket = socketCluster.connect(options);
socket.on('connect', function(status){
console.log(status);
console.log('CONNECTED');
channel = bindChannel(socket);
});
socket.on('error', function(err){
console.error('Socket error - ' + err);
});
socket.on('disconnect', function(err){
console.error('Socket disconnect - ' + err);
});
var watcher = function(data){
$('#result').html(data + '<br/>');
}
function bindChannel(socket){
var channel = socket.subscribe('channel_demo');
channel.on('subscribeFail', function(err){
console.log('Failed to subscribe the channel due to error: ' + err);
});
channel.watch(watcher);
return channel;
}
});
server:
var socket = {};
scServer.on('connection', function (sock){
log('socket_id: ' + sock.id);
socket = sock;
socket.on('disconnect', function () {
console.error('User disconnected');
});
});
setInterval(function(){
if(socket && socket.exchange){
socket.exchange.channel('channel_demo').publish('push data to channel:' + utils.random(1, 100), function(){
console.log(arguments);
});
}
}, 1000);
from socketcluster-client.
Related Issues (20)
- Increases temperature on ios devices HOT 2
- TypeError: cb is not a function HOT 8
- Update release Page HOT 1
- Not able to run the client on the office-js in excel desktop HOT 2
- Subscribe method causes error in IE11 HOT 9
- TypeError: _iterator[Symbol.iterator] is not a function || For await of loop not working on Mobile (React Native) HOT 2
- Please bump version of remote socketcluster-client
- Remove gulp, and babel dependencies
- Can this client be used in non browser environment ?
- [docs] Add description about `autoReconnectOptions` option
- Unable to resolve as a es module
- Need help for implemeting a real-time chat HOT 1
- Incorrect disconnect code HOT 1
- socketcluster-client with protocol v1 HOT 1
- How to use "subscribeWildcard" instead of "subscribe" ? HOT 1
- How we add headers in socketclusterclient HOT 5
- I got TypeError: Object is not async iterable error while trying to listen to message event in React Native app
- Docs & readme incorrect on "rejectUnauthorized" HOT 3
- eventObject timeout has a timeout set when it's not expecting a response
- SyntaxError: Identifier 'global' has already been declared HOT 1
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 socketcluster-client.