Comments (26)
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.
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.
Is this implemented now, or should I use the - method
socket.clients[kickedClientId].send({ event: 'disconnect' });
socket.clients[kickedClientId].connection.end();
from socket.io.
Try,
socket.manager.onClientDisconnect(socket.id);
from socket.io.
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.
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.
This would indeed be very useful.
from socket.io.
Is client.connection.destroy() a legitimate way to disconnect a client?
from socket.io.
plus1. Would love to have a way to cleanly disconnect a client. :)
from socket.io.
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.
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.
What is the "official" way to do this????
from socket.io.
Please re-open such that we find out what the official way is.
from socket.io.
There is still no solution to this, please reopen to track progress.
from socket.io.
@geniousphp I'm having the same issue
from socket.io.
So why is the issue closed, is it solved? if so how? What is the conclusion?
from socket.io.
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.
I think that's what I want. awesome.
from socket.io.
Unfortunately @acidhax his method does not work any more since the 1.0 release
from socket.io.
Is there anyway that upgrade 0.9.16 to 1.X.X smoothly,guys?
from socket.io.
+1
from socket.io.
Same boat here. This is really important for testing as you want to isolate tests from each other.
from socket.io.
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.
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.
11 years...passed :) This is till an issue, can not kick client out or simply tell them to disconnect !
Steve
from socket.io.
@thusinh1969 you can use socket.disconnect()
Reference: https://socket.io/docs/v4/server-api/#socketdisconnectclose
from socket.io.
Related Issues (20)
- Can't emit namespace events without arguments in TS HOT 3
- Sending one message more than one time after network disconnections and reconnects. HOT 2
- Issue with Socket.io Client using Next.js - Memory leak and JavaScript heap out of memory HOT 1
- Including Protocol Parameter in Socket Connection Creation in iOS HOT 1
- Type error when using socket.timeout.emitWithAck HOT 3
- If the server's upstream bandwidth is used up, it will generate a large number of pingTimeoutTimers, which in turn will cause memory leaks HOT 1
- Not able to pass prop socket HOT 2
- 🐛 Error in serving socket.io.min.js HOT 1
- Uncaught ReferenceError: Buffer is not defined HOT 2
- Page Refresh issue with socket HOT 2
- maxHttpBufferSize option don't work with bun HOT 1
- emitWithAck does not throw on disconnect HOT 3
- Make `emitWithAck` for a room say which socket(s) failed to ack HOT 1
- Connection recovery doesn't work HOT 2
- Allow to bind socket.io to bun built in server. HOT 2
- authToken = null in springboot despite set by the client HOT 1
- Replace `XMLHttpRequest` with `fetch` HOT 1
- "emitWithAck" function leaks memory when acknowledgement timeout HOT 1
- this.children is undefined for ParentBroadcastAdapter broadcast HOT 3
- How to get error details in client when server side with io.engine.use middleware 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 socket.io.