Socket.io: Client.disconnect() to force a client to disconnect

Created on 8 Sep 2010  路  24Comments  路  Source: socketio/socket.io

It would be great to have a disconnect() method on Client instances. This is useful to implement mechanisms such as IRC's kick/ban. Basically there needs to be a way to get rid of abusers.

Most helpful comment

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 ?

All 24 comments

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.

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!

This would indeed be very useful.

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

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

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.

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

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

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

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

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

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

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.

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 ?

@geniousphp I'm having the same issue

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

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

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

I think that's what I want. awesome.

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

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

+1

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

    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();

    }

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dmuth picture dmuth  路  3Comments

stnwk picture stnwk  路  4Comments

shashuec picture shashuec  路  4Comments

doughsay picture doughsay  路  4Comments

adammw picture adammw  路  4Comments