Git Product home page Git Product logo

Comments (8)

jondubois avatar jondubois commented on September 23, 2024

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.

smilence86 avatar smilence86 commented on September 23, 2024

@jondubois thanks~

from socketcluster-client.

smilence86 avatar smilence86 commented on September 23, 2024

@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.

jondubois avatar jondubois commented on September 23, 2024

@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.

jondubois avatar jondubois commented on September 23, 2024

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.

smilence86 avatar smilence86 commented on September 23, 2024

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.

jondubois avatar jondubois commented on September 23, 2024

@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.

smilence86 avatar smilence86 commented on September 23, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.