Answer the question
In order to leave comments, you need to log in
Why is redirect only after the second click?
Client code:
$( document ).ready(function() {
var socket = io();
$('#create').click(function(){
socket.emit('Create room',(socket.id).toString());
});
$('#join').click(function(){
socket.emit('join',$('#roomID').val());
});
socket.on('joined',function(){
window.location.replace("/room");
});
});
io.on('connection', function(socket){
socket.on('Create room', function(msg){
socket.emit('joined');
room_id = ( Math.random() * 100000 ) | 0;
listOfRooms.push(room_id);
socket.join(room_id);
console.log('Socket '+socket.id.toString()+' was connected to room '+room_id.toString());
Conn[socket.id.toString()]=room_id;
});
});
Answer the question
In order to leave comments, you need to log in
You don't wait for the connect event. Here's an example for you:
var socket = io(); // не стоит ждать загрузки всего документа, сокет должен успеть "открыться" к событию "document ready"
socket.on('joined',function(){
window.location.replace("/room");
});
socket.on('connect', function () { // Вот теперь emit будет работать сразу
$( document ).ready(function() {
$('#create').click(function(){
socket.emit('Create room',(socket.id).toString());
});
$('#join').click(function(){
socket.emit('join',$('#roomID').val());
});
});
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question