I may be doing something wrong here but I can't get my head around this.
The following does not work:
client
$("#disconnectButton").click(function() {
socket.emit("disconnect");
});
server
socket.on("disconnect", function() {
console.log("this never gets called");
});
Can't I manually call the disconnect event from the client side? Because the following works:
client
$("#disconnectButton").click(function() {
socket.emit("whatever");
});
server
socket.on("whatever", function() {
socket.disconnect();
console.log("this gets called and disconnects the client");
});
'disconnect' event is Socket.io event,if you use emit to call the 'disconnect',may be cause other problom
so:
$("#disconnectButton").click(function() {
socket.emit("disconnect");
});
replace:
$("#disconnectButton").click(function() {
socket.disconnect();
});
For now, socket.emit('disconnect') or socket.disconnect() doesn't work.
The correct syntax to disconnect to the socket server is
socket.close()
Related
I am on a websocket (socket.io) and I want to be able to force disconnect on a given user. I use this on my server file:
// kickout
socket.on('kickout', (sckid) => { //sckid is the socket.id of the kicked-out user
io.to(sckid).emit('kicked');
});
});
// kicked out
socket.on('kicked', () => {
socket.disconnect();
});
Then I do socket.emit('kickout', 'someusersocketid'); on my frontend but it won't work for some reasons. Seems like the server listens to "kickout" alright but so to "kicked". Why is that?
Try this one:
var clients = {}
sockets.on('connection', function(socket) {
clients[socket.id] = socket;
socket.on('disconnect', function() {
delete clients[socket.id];
});
});
currently I am using the following code in node.js with socket.io to connect to my server, which is working fine. But if my node.js server is not running, the client is trying to connect to it again and again in intervals - but I would like to stop it and close the socket on the client side if the server is not reachable. I have tried using connect_failed, but unfortunately this is never being called. how can this be done?
function findOpponent(gamemode)
{
console.log('Registering myself on nodejs Server and waiting for 2nd player');
socket = io.connect("http://gladiator.localhost:3000" , {
'query': '&uuid='+uuid+'&authkey='+auth_key+'&gamemode='+gamemode
});
socket.on('connect_failed', function() {
// --> this is never being called
console.log("Sorry, there seems to be an issue with the connection!");
});
socket.on('user join',function(msg){
alert('USER JOINED');
});
socket.on('user leave',function(msg){
console.log(msg);
alert('USER LEFT');
});
socket.on('message',function(msg){
alert(msg);
});
socket.on('gamestart',function(data){
console.log('gamestart');
alert(data.msg);
});
}
try autoConnect and reconnection options for socket io client
{ autoConnect: false, reconnection: false}
EDIT:
and you can listen on connect_error event for catch connection error
socket.on("connect_error", callback)
I am new to node and i have been playing while developing an app and so far so good. I am using socket.io with http server. All work as expected but.... i have a button that emit to the socket and after 10 clicks i get a warning on the logs about the event memory leak detected and to setMaxListeners. So far i saw on other questions, i must remove the listeners after emit instead of take the shortcut of increase the Max Listeners, but i have been trying without success.
My client side code looks like:
$(document).ready(function () {
$("#validate").click(function () {
socket.emit('stop', "validate button");
});
});
On the server side:
io.sockets.on('connection', function (socket) {
// WE RECEIVE stop
socket.on('stop', function (msg) {
console.log(msg);
clearInterval(refresh);
});
});
How can i remove the listener after the event?
Any lights will be appreciated. Thanks in advance
To only fire the client-side event once, since you are using jQuery, you can use the one() function, like this:
$(document).ready(function () {
$("#validate").one('click', function () {
socket.emit('stop', "validate button");
});
});
On the server-side, try this:
io.sockets.on('connection', function (socket) {
// WE RECEIVE stop
socket.on('stop', stopHandler);
function stopHandler (msg) {
console.log(msg);
clearInterval(refresh);
socket.removeListener('stop', stopHandler);
}
});
I'm trying to send news to my client. On connect, it works great, but for some reason on broadcast2 it wont get any response client sided, even know its the same piece of code, and even that broadcast2's console.log is working.
Q: How can i make sure broadcast2 emit will work?
This works:
socket.on('message', function (data) {
console.log('message gotten');
socket.emit('news', { message: 'xxxx' });
});
this wont work:
socket.on('broadcast2', function (data) {
console.log("broadcast revieced");
socket.emit('news', { message: 'xxxx' });
});
this is node.js response:
total code in node.js
socket.on('message', function (data) {
console.log('message gotten');
});
socket.on('another-message', function (data) {
socket.emit('not-news', { hello: 'world' });
});
socket.on('broadcast2', function (data) {
console.log("broadcast revieced");
socket.emit('news', { message: 'xxxx' });
});
and this on the client side:
var socket = io.connect('mysite:8080');
function sender() {
console.log('sending tester');
socket.emit('sendertester', 2);
}
socket.on('connect',function(){
});
socket.on('tester', function(msg){
console.log("callback");
});
socket.on('news', function(message) {
console.log("INCOMMING NEWS");
console.log(message);
});
UPDATE 1:
The broadcast2 socket, sent by PHP:
function broadcast($message,$broadcast = 'broadcast2') {
$client = new Client(new Version1X('myurlhidden:8080'));
$client->initialize();
$client->emit($broadcast, ['message' => $message]);
$client->close();
}
UPDATE 2:
**Question two: Cause my broadcast2 is sent before the client sided is loaded, and then the client connects to the node, could that be the cause?
But in the same time, im already preloading the class that holds the broadcast2 emitter.
Using codeigniter framework.**
UPDATE 3
I was trying to check my theory on update 2, by having two users logged in, while user one trying to perform the trigger. user two gets no output, so i suppose that theory is busted.
The server cannot send a message to any socket before it is connected. You have to wait until you have something listening to receive what you are sending.
I wouldn't call close after the emit either, as you may close the connection before the client has received the message, emit doesn't wait for the client to receive the data before returning its asynchronous.
Instead let the clients close the connections when they terminate.
Good day,
I was playing with the example found here on SO: PHP Socket Server vs node.js: Web Chat.
Using gMUD (a telnet-like client) to connect to my server, I can't seem to be able to detect when the client disconnect from the server.
Here is the two events I'm using:
socket.on('end', function() {
console.log('end');
});
socket.on('error', function() {
console.log('error');
});
To be honest, I thought the error event could do it. Is there any other events I'm actually missing?
I found it. It was abvious:
socket.on('close', function() {
//Do something
});