socket.io leave current room and join after button is clicked
up vote
-1
down vote
favorite
my question is about socket rooms and leave and joining in rooms .my problem is that : i am creating chatroom and i want whenever user (socket) clicks "next person" button (in my case link) i want socket to disconnect (leave) its current room and join another one where another socket is waiting for second socket. like in Omegle
Heading
so i tried this code
client-side:
N.addEventListener('click', function(e)
socket.emit('next')
);
socket.on('next',function()
socket.disconnect()
document.GetElementById('divd').style.display = "inline";
socket.connect()
);
server-side:
socket.on('next', function()
socket.leave(socket.current_room)
chnm.in(socket.current_room).emit('next');
socket.join(room)
);
but whenever i click "next" it only shows both socket disconnect div like in disconnect event but does not lets socket which triggered that event to join other room
rooms in my case is like that
var room = "room" + numb;
socket.current_room = room;
but what i want is to show in room (where socket disconnected) that socket disconnected and socket which triggered that event to be joined in other room .
(example: in room 1 socket triggered "next" link he/she disconnects from the room 1 and joins to room 2 and in room 1 appears disconnect div, i think it will appear anyway if i use socket.disconnect() because i already created disconnect event . thanks guys for help <3
node.js sockets express socket.io chatroom
add a comment |
up vote
-1
down vote
favorite
my question is about socket rooms and leave and joining in rooms .my problem is that : i am creating chatroom and i want whenever user (socket) clicks "next person" button (in my case link) i want socket to disconnect (leave) its current room and join another one where another socket is waiting for second socket. like in Omegle
Heading
so i tried this code
client-side:
N.addEventListener('click', function(e)
socket.emit('next')
);
socket.on('next',function()
socket.disconnect()
document.GetElementById('divd').style.display = "inline";
socket.connect()
);
server-side:
socket.on('next', function()
socket.leave(socket.current_room)
chnm.in(socket.current_room).emit('next');
socket.join(room)
);
but whenever i click "next" it only shows both socket disconnect div like in disconnect event but does not lets socket which triggered that event to join other room
rooms in my case is like that
var room = "room" + numb;
socket.current_room = room;
but what i want is to show in room (where socket disconnected) that socket disconnected and socket which triggered that event to be joined in other room .
(example: in room 1 socket triggered "next" link he/she disconnects from the room 1 and joins to room 2 and in room 1 appears disconnect div, i think it will appear anyway if i use socket.disconnect() because i already created disconnect event . thanks guys for help <3
node.js sockets express socket.io chatroom
The usual way you would leave one room and join another is to send your server a message asking it to do asocket.leave(oldRoom)
andsocket.join(newRoom)
on your behalf. There is no need to drop the connection and reconnect just to change rooms.
– jfriend00
Nov 8 at 3:30
can u show me an example i get a bit confused , because i think i am doing what u are saying
– codR
Nov 8 at 11:47
2
Please do not vandalize your posts. If you believe your question is not useful or is no longer useful, it should be deleted instead of editing out all of the data that actually makes it a question. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC BY-SA 3.0 license). By SE policy, any vandalism will be reverted.
– Glorfindel
2 days ago
i tried to edit but it was impossible
– codR
2 days ago
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
my question is about socket rooms and leave and joining in rooms .my problem is that : i am creating chatroom and i want whenever user (socket) clicks "next person" button (in my case link) i want socket to disconnect (leave) its current room and join another one where another socket is waiting for second socket. like in Omegle
Heading
so i tried this code
client-side:
N.addEventListener('click', function(e)
socket.emit('next')
);
socket.on('next',function()
socket.disconnect()
document.GetElementById('divd').style.display = "inline";
socket.connect()
);
server-side:
socket.on('next', function()
socket.leave(socket.current_room)
chnm.in(socket.current_room).emit('next');
socket.join(room)
);
but whenever i click "next" it only shows both socket disconnect div like in disconnect event but does not lets socket which triggered that event to join other room
rooms in my case is like that
var room = "room" + numb;
socket.current_room = room;
but what i want is to show in room (where socket disconnected) that socket disconnected and socket which triggered that event to be joined in other room .
(example: in room 1 socket triggered "next" link he/she disconnects from the room 1 and joins to room 2 and in room 1 appears disconnect div, i think it will appear anyway if i use socket.disconnect() because i already created disconnect event . thanks guys for help <3
node.js sockets express socket.io chatroom
my question is about socket rooms and leave and joining in rooms .my problem is that : i am creating chatroom and i want whenever user (socket) clicks "next person" button (in my case link) i want socket to disconnect (leave) its current room and join another one where another socket is waiting for second socket. like in Omegle
Heading
so i tried this code
client-side:
N.addEventListener('click', function(e)
socket.emit('next')
);
socket.on('next',function()
socket.disconnect()
document.GetElementById('divd').style.display = "inline";
socket.connect()
);
server-side:
socket.on('next', function()
socket.leave(socket.current_room)
chnm.in(socket.current_room).emit('next');
socket.join(room)
);
but whenever i click "next" it only shows both socket disconnect div like in disconnect event but does not lets socket which triggered that event to join other room
rooms in my case is like that
var room = "room" + numb;
socket.current_room = room;
but what i want is to show in room (where socket disconnected) that socket disconnected and socket which triggered that event to be joined in other room .
(example: in room 1 socket triggered "next" link he/she disconnects from the room 1 and joins to room 2 and in room 1 appears disconnect div, i think it will appear anyway if i use socket.disconnect() because i already created disconnect event . thanks guys for help <3
node.js sockets express socket.io chatroom
node.js sockets express socket.io chatroom
edited 2 days ago
Glorfindel
16.3k114769
16.3k114769
asked Nov 7 at 19:47
codR
85
85
The usual way you would leave one room and join another is to send your server a message asking it to do asocket.leave(oldRoom)
andsocket.join(newRoom)
on your behalf. There is no need to drop the connection and reconnect just to change rooms.
– jfriend00
Nov 8 at 3:30
can u show me an example i get a bit confused , because i think i am doing what u are saying
– codR
Nov 8 at 11:47
2
Please do not vandalize your posts. If you believe your question is not useful or is no longer useful, it should be deleted instead of editing out all of the data that actually makes it a question. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC BY-SA 3.0 license). By SE policy, any vandalism will be reverted.
– Glorfindel
2 days ago
i tried to edit but it was impossible
– codR
2 days ago
add a comment |
The usual way you would leave one room and join another is to send your server a message asking it to do asocket.leave(oldRoom)
andsocket.join(newRoom)
on your behalf. There is no need to drop the connection and reconnect just to change rooms.
– jfriend00
Nov 8 at 3:30
can u show me an example i get a bit confused , because i think i am doing what u are saying
– codR
Nov 8 at 11:47
2
Please do not vandalize your posts. If you believe your question is not useful or is no longer useful, it should be deleted instead of editing out all of the data that actually makes it a question. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC BY-SA 3.0 license). By SE policy, any vandalism will be reverted.
– Glorfindel
2 days ago
i tried to edit but it was impossible
– codR
2 days ago
The usual way you would leave one room and join another is to send your server a message asking it to do a
socket.leave(oldRoom)
and socket.join(newRoom)
on your behalf. There is no need to drop the connection and reconnect just to change rooms.– jfriend00
Nov 8 at 3:30
The usual way you would leave one room and join another is to send your server a message asking it to do a
socket.leave(oldRoom)
and socket.join(newRoom)
on your behalf. There is no need to drop the connection and reconnect just to change rooms.– jfriend00
Nov 8 at 3:30
can u show me an example i get a bit confused , because i think i am doing what u are saying
– codR
Nov 8 at 11:47
can u show me an example i get a bit confused , because i think i am doing what u are saying
– codR
Nov 8 at 11:47
2
2
Please do not vandalize your posts. If you believe your question is not useful or is no longer useful, it should be deleted instead of editing out all of the data that actually makes it a question. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC BY-SA 3.0 license). By SE policy, any vandalism will be reverted.
– Glorfindel
2 days ago
Please do not vandalize your posts. If you believe your question is not useful or is no longer useful, it should be deleted instead of editing out all of the data that actually makes it a question. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC BY-SA 3.0 license). By SE policy, any vandalism will be reverted.
– Glorfindel
2 days ago
i tried to edit but it was impossible
– codR
2 days ago
i tried to edit but it was impossible
– codR
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
There's still not enough code shown or enough of the overall design described that you're trying to achieve, but here's what I can see from what you've shown so far.
Here's your sequence of events:
- User clicks button
- Client code does
socket.emit('next')
- Server receives
next
message - Server calls
socket.leave(socket.current_room)
to leave the current room - Server does a
.emit('next')
to everyone still in that room - Server calls
socket.join(room)
(I have no idea where theroom
variable comes from) - Clients receive
next
message and that causes them tosocket.disconnect() and then
socket.connect()` (no idea why it's doing that).
The things that seem odd to me about this sequence of events are:
The
socket.disconnect()
followed by thesocket.connect()
is completely unnecessary and probably causing problems. First off, these events are asynchronous. If you really wanted to do one followed by the other, you need to wait for the disconnect to finish before trying to do the reconnect. But, mostly, there should be no reason to disconnect and then reconnect. Just update the state of the connection you already have.I don't follow why when you assign someone to a new room, you then disconnect everyone else who was in that room.
Please don't use the same message name
next
to mean one thing when the client receives it and something completely different when the server receives it. This isn't a programming error per se, but it really makes your code hard to understand. Give each its own descriptive name.
check my ANSWER there is the code because i can not post it in coms
– codR
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
There's still not enough code shown or enough of the overall design described that you're trying to achieve, but here's what I can see from what you've shown so far.
Here's your sequence of events:
- User clicks button
- Client code does
socket.emit('next')
- Server receives
next
message - Server calls
socket.leave(socket.current_room)
to leave the current room - Server does a
.emit('next')
to everyone still in that room - Server calls
socket.join(room)
(I have no idea where theroom
variable comes from) - Clients receive
next
message and that causes them tosocket.disconnect() and then
socket.connect()` (no idea why it's doing that).
The things that seem odd to me about this sequence of events are:
The
socket.disconnect()
followed by thesocket.connect()
is completely unnecessary and probably causing problems. First off, these events are asynchronous. If you really wanted to do one followed by the other, you need to wait for the disconnect to finish before trying to do the reconnect. But, mostly, there should be no reason to disconnect and then reconnect. Just update the state of the connection you already have.I don't follow why when you assign someone to a new room, you then disconnect everyone else who was in that room.
Please don't use the same message name
next
to mean one thing when the client receives it and something completely different when the server receives it. This isn't a programming error per se, but it really makes your code hard to understand. Give each its own descriptive name.
check my ANSWER there is the code because i can not post it in coms
– codR
2 days ago
add a comment |
up vote
1
down vote
accepted
There's still not enough code shown or enough of the overall design described that you're trying to achieve, but here's what I can see from what you've shown so far.
Here's your sequence of events:
- User clicks button
- Client code does
socket.emit('next')
- Server receives
next
message - Server calls
socket.leave(socket.current_room)
to leave the current room - Server does a
.emit('next')
to everyone still in that room - Server calls
socket.join(room)
(I have no idea where theroom
variable comes from) - Clients receive
next
message and that causes them tosocket.disconnect() and then
socket.connect()` (no idea why it's doing that).
The things that seem odd to me about this sequence of events are:
The
socket.disconnect()
followed by thesocket.connect()
is completely unnecessary and probably causing problems. First off, these events are asynchronous. If you really wanted to do one followed by the other, you need to wait for the disconnect to finish before trying to do the reconnect. But, mostly, there should be no reason to disconnect and then reconnect. Just update the state of the connection you already have.I don't follow why when you assign someone to a new room, you then disconnect everyone else who was in that room.
Please don't use the same message name
next
to mean one thing when the client receives it and something completely different when the server receives it. This isn't a programming error per se, but it really makes your code hard to understand. Give each its own descriptive name.
check my ANSWER there is the code because i can not post it in coms
– codR
2 days ago
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
There's still not enough code shown or enough of the overall design described that you're trying to achieve, but here's what I can see from what you've shown so far.
Here's your sequence of events:
- User clicks button
- Client code does
socket.emit('next')
- Server receives
next
message - Server calls
socket.leave(socket.current_room)
to leave the current room - Server does a
.emit('next')
to everyone still in that room - Server calls
socket.join(room)
(I have no idea where theroom
variable comes from) - Clients receive
next
message and that causes them tosocket.disconnect() and then
socket.connect()` (no idea why it's doing that).
The things that seem odd to me about this sequence of events are:
The
socket.disconnect()
followed by thesocket.connect()
is completely unnecessary and probably causing problems. First off, these events are asynchronous. If you really wanted to do one followed by the other, you need to wait for the disconnect to finish before trying to do the reconnect. But, mostly, there should be no reason to disconnect and then reconnect. Just update the state of the connection you already have.I don't follow why when you assign someone to a new room, you then disconnect everyone else who was in that room.
Please don't use the same message name
next
to mean one thing when the client receives it and something completely different when the server receives it. This isn't a programming error per se, but it really makes your code hard to understand. Give each its own descriptive name.
There's still not enough code shown or enough of the overall design described that you're trying to achieve, but here's what I can see from what you've shown so far.
Here's your sequence of events:
- User clicks button
- Client code does
socket.emit('next')
- Server receives
next
message - Server calls
socket.leave(socket.current_room)
to leave the current room - Server does a
.emit('next')
to everyone still in that room - Server calls
socket.join(room)
(I have no idea where theroom
variable comes from) - Clients receive
next
message and that causes them tosocket.disconnect() and then
socket.connect()` (no idea why it's doing that).
The things that seem odd to me about this sequence of events are:
The
socket.disconnect()
followed by thesocket.connect()
is completely unnecessary and probably causing problems. First off, these events are asynchronous. If you really wanted to do one followed by the other, you need to wait for the disconnect to finish before trying to do the reconnect. But, mostly, there should be no reason to disconnect and then reconnect. Just update the state of the connection you already have.I don't follow why when you assign someone to a new room, you then disconnect everyone else who was in that room.
Please don't use the same message name
next
to mean one thing when the client receives it and something completely different when the server receives it. This isn't a programming error per se, but it really makes your code hard to understand. Give each its own descriptive name.
answered Nov 8 at 21:58
jfriend00
420k46531577
420k46531577
check my ANSWER there is the code because i can not post it in coms
– codR
2 days ago
add a comment |
check my ANSWER there is the code because i can not post it in coms
– codR
2 days ago
check my ANSWER there is the code because i can not post it in coms
– codR
2 days ago
check my ANSWER there is the code because i can not post it in coms
– codR
2 days ago
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53196749%2fsocket-io-leave-current-room-and-join-after-button-is-clicked%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
The usual way you would leave one room and join another is to send your server a message asking it to do a
socket.leave(oldRoom)
andsocket.join(newRoom)
on your behalf. There is no need to drop the connection and reconnect just to change rooms.– jfriend00
Nov 8 at 3:30
can u show me an example i get a bit confused , because i think i am doing what u are saying
– codR
Nov 8 at 11:47
2
Please do not vandalize your posts. If you believe your question is not useful or is no longer useful, it should be deleted instead of editing out all of the data that actually makes it a question. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC BY-SA 3.0 license). By SE policy, any vandalism will be reverted.
– Glorfindel
2 days ago
i tried to edit but it was impossible
– codR
2 days ago