Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client.disconnect() to force a client to disconnect #47

Closed
steadicat opened this issue Sep 8, 2010 · 26 comments
Closed

Client.disconnect() to force a client to disconnect #47

steadicat opened this issue Sep 8, 2010 · 26 comments

Comments

@steadicat
Copy link

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.

@rauchg
Copy link
Contributor

rauchg commented Sep 8, 2010

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.

@jdub
Copy link

jdub commented Feb 6, 2011

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!

@nh2
Copy link

nh2 commented Apr 6, 2011

This would indeed be very useful.

@nh2
Copy link

nh2 commented Apr 6, 2011

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

@SeanColombo
Copy link

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

@SeanColombo
Copy link

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.

@SeanColombo
Copy link

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

@onedayitwillmake
Copy link

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

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

@bminer
Copy link

bminer commented Nov 22, 2011

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

@nh2
Copy link

nh2 commented Mar 9, 2012

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

@nh2
Copy link

nh2 commented Jun 16, 2012

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

@nh2
Copy link

nh2 commented Jun 16, 2012

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.

@elghazal-a
Copy link

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 ?

@volpino
Copy link

volpino commented May 5, 2013

@geniousphp I'm having the same issue

@destan
Copy link

destan commented May 15, 2013

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

@elghazal-a
Copy link

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

@matbee-eth
Copy link

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

@Jackcu
Copy link

Jackcu commented May 27, 2014

I think that's what I want. awesome.

@timk95
Copy link

timk95 commented May 29, 2014

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

@Jackcu
Copy link

Jackcu commented May 30, 2014

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

@nicohvi
Copy link

nicohvi commented Aug 8, 2014

+1

@agconti
Copy link

agconti commented Dec 11, 2014

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

@SiddharthArtoon
Copy link

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

    }

@DurgaPrasadGavara
Copy link

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.

@thusinh1969
Copy link

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

Steve

@darrachequesne
Copy link
Member

@thusinh1969 you can use socket.disconnect()

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

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests