Git Product home page Git Product logo

Comments (26)

elghazal-a avatar elghazal-a commented on May 3, 2024 14

the socket.disconnect(); doesn't seems to work correctly, certainly it triggers the disconnect event, but it doesn't close literally the connection. Is there a bug or this behavior is expected ?

from socket.io.

nh2 avatar nh2 commented on May 3, 2024 2

I just discovered this code in client/socket.js:

/**
 * Kicks client
 *
 * @api public
 */

Socket.prototype.disconnect = function () {
  if (!this.disconnected) {
    this.log.info('booting client');

    if ('' === this.namespace.name) {
      if (this.manager.transports[this.id] && this.manager.transports[this.id].open) {
        this.manager.transports[this.id].onForcedDisconnect();
      } else {
        this.manager.onClientDisconnect(this.id);
        this.manager.store.publish('disconnect:' + this.id);
      }
    } else {
      this.packet({type: 'disconnect'});
      this.manager.onLeave(this.id, this.namespace.name);
      this.$emit('disconnect', 'booted');
    }

  }

  return this;
};

I tried it (socket.disconnect()) and it works fine. It looks like this code has been around since mid-2011 and nobody told us.

See also issue #795.

from socket.io.

onedayitwillmake avatar onedayitwillmake commented on May 3, 2024 1

Is this implemented now, or should I use the - method

socket.clients[kickedClientId].send({ event: 'disconnect' });
socket.clients[kickedClientId].connection.end();

from socket.io.

matbee-eth avatar matbee-eth commented on May 3, 2024 1

Try,
socket.manager.onClientDisconnect(socket.id);

from socket.io.

rauchg avatar rauchg commented on May 3, 2024

It might be interesting to discuss whether it's worth implementing (although it'd be low priority). However, IRC kick/ban is an example of where I wouldn't use such functionality. In those scenarios it's desired to keep the client connected, and just prevent broadcasting of messages of a specific channel.

from socket.io.

jdub avatar jdub commented on May 3, 2024

Found this when pondering a similar method.

I'd like a way to explicitly and cleanly tell a client it is being disconnected for policy reasons, not transport reasons. For instance, authorisation failure. This way, the client knows the connection was established but was disconnected at the host's behest, and need not try fallback transports.

Thanks!

from socket.io.

nh2 avatar nh2 commented on May 3, 2024

This would indeed be very useful.

from socket.io.

nh2 avatar nh2 commented on May 3, 2024

Is client.connection.destroy() a legitimate way to disconnect a client?

from socket.io.

SeanColombo avatar SeanColombo commented on May 3, 2024

plus1. Would love to have a way to cleanly disconnect a client. :)

from socket.io.

SeanColombo avatar SeanColombo commented on May 3, 2024

client._onDisconnect(); or
socket.clients[kickedUserSocketId]._onDisconnect();

seem to do the trick.

NOTE: There is an underscore before the "onDisconnect", it just doesn't render in this markdown.

from socket.io.

SeanColombo avatar SeanColombo commented on May 3, 2024

Also saw someone do it like this (haven't tested it myself though):
socket.clients[kickedClientId].send({ event: 'disconnect' });
socket.clients[kickedClientId].connection.end();

from socket.io.

bminer avatar bminer commented on May 3, 2024

What is the "official" way to do this????

from socket.io.

nh2 avatar nh2 commented on May 3, 2024

Please re-open such that we find out what the official way is.

from socket.io.

nh2 avatar nh2 commented on May 3, 2024

There is still no solution to this, please reopen to track progress.

from socket.io.

volpino avatar volpino commented on May 3, 2024

@geniousphp I'm having the same issue

from socket.io.

destan avatar destan commented on May 3, 2024

So why is the issue closed, is it solved? if so how? What is the conclusion?

from socket.io.

elghazal-a avatar elghazal-a commented on May 3, 2024

I don't know why it's closed, it doesn't works for me and I still use the ugly way to force disconnection.

from socket.io.

Jackcu avatar Jackcu commented on May 3, 2024

I think that's what I want. awesome.

from socket.io.

timk95 avatar timk95 commented on May 3, 2024

Unfortunately @acidhax his method does not work any more since the 1.0 release

from socket.io.

Jackcu avatar Jackcu commented on May 3, 2024

Is there anyway that upgrade 0.9.16 to 1.X.X smoothly,guys?

from socket.io.

nicohvi avatar nicohvi commented on May 3, 2024

+1

from socket.io.

agconti avatar agconti commented on May 3, 2024

Same boat here. This is really important for testing as you want to isolate tests from each other.

from socket.io.

SiddharthArtoon avatar SiddharthArtoon commented on May 3, 2024
    try {
        IO.Options opts = new IO.Options();
        opts.forceNew = true;
        opts.timeout = -1;
        socket1 = IO.socket(c.ip, opts);
        socket1.on("res", getdataListener);
        socket1.connect();
                socket1.on(com.github.nkzawa.socketio.client.Socket.EVENT_CONNECT,
                new Emitter.Listener() {

                    @Override
                    public void call(Object... args) {

                        Logger.print("CONNECTED...............");

                        Message msg = new Message();
                        msg.what = ResponseCodes.CreateQueueResp;
                        Login.handler.sendMessage(msg);

                        activity.runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                // TODO Auto-generated method stub

                                startChecking();
                                startIdleChecking();

                            }

                        }); 
                    }

                })
                .on(com.github.nkzawa.socketio.client.Socket.EVENT_RECONNECT,
                        new Emitter.Listener() {

                            @Override
                            public void call(Object... arg0) {


                                System.out
                                        .println("............... EVENT_RECONNECT");

                                // TODO Auto-generated method stub

                            }
                        })
                .on(com.github.nkzawa.socketio.client.Socket.EVENT_CONNECT_ERROR,
                        new Emitter.Listener() {

                            @Override
                            public void call(Object... args) {

                                System.out
                                        .println("............... EVENT_CONNECT_ERROR");

                            }

                        })
                .on(com.github.nkzawa.socketio.client.Socket.EVENT_CONNECT_TIMEOUT,
                        new Emitter.Listener() {

                            @Override
                            public void call(Object... args) {

                                System.out
                                        .println("............... EVENT_CONNECT_TIMEOUT");

                            }

                            // this is the emit from the server
                        })

                .on(Socket.EVENT_ERROR, new Emitter.Listener() {

                    @Override
                    public void call(Object... args) {

                        System.out
                                .println("......................... EVENT_ERROR");

                    }

                    // this is the emit from the server

                })
                .on(com.github.nkzawa.socketio.client.Socket.EVENT_DISCONNECT,
                        new Emitter.Listener() {

                            @Override
                            public void call(Object... args) {

                                Logger.print(".............EVENT_DISCONNECT");


                                if (!open) {

                                    //server_message = getResources().getString(R.string.It_seems_that);


                                    server_message="It seems that connection to server have been Lost.Please reconnect!"; 

                                    ServerError error = new ServerError(activity, server_message);


                                }

                            }

                        });

    } catch (Exception e) {

        e.printStackTrace();

    }

from socket.io.

DurgaPrasadGavara avatar DurgaPrasadGavara commented on May 3, 2024

My function app triggered repeatedly when device gets the message. Every time my function app socket connection opened.How to close the opened connection forcibly.
Here is the my logic:
'use strict';
var _test = require('socket.io-client');
var socket = _test.connect('http://XXXXXXXXXX.net/');
exports.handler = function (event,context) {
var ResultMessage = JSON.stringify(event.message,null,2);
if(!socket.connected)
{
socket.emit('add user', '1542');
}
socket.emit('new message',"[" + ResultMessage + "]");

};
Thanks,
In advance.

from socket.io.

thusinh1969 avatar thusinh1969 commented on May 3, 2024

11 years...passed :) This is till an issue, can not kick client out or simply tell them to disconnect !

Steve

from socket.io.

darrachequesne avatar darrachequesne commented on May 3, 2024

@thusinh1969 you can use socket.disconnect()

Reference: https://socket.io/docs/v4/server-api/#socketdisconnectclose

from socket.io.

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.