A
A
Alino4ka2016-09-01 02:42:57
Socket.io
Alino4ka, 2016-09-01 02:42:57

Socket.io doesn't want to emit connections right away, is this a bug?

I want to do this: when connecting, the user sends an emit with certain data.
Everything seems to be correct, BUT the first emit is simply not sent to frames empty, the second one works fine.
Is this a socket.io bug or what?

<script>
    $(document).ready(function(){
      var socket = io.connect('localhost:81');

      socket.on('connect',function(){
          console.log('Подключение к серверу успешно!');
          socket.emit('test1','data1');
          socket.emit('test2','data2');
      });

    });
</script>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SagePtr, 2016-09-01
@Alino4ka

Have you made sure on the server side that it does not come?
The fact is that socket.io tries several "transports" - first polling, then websocket. Since polling usually works, the first packet is transmitted through it, the subsequent ones are transmitted through the web socket, because the first one will not appear there, it was sent earlier.
You can disable transports other than the websocket with the second parameter:

var socket = io.connect('localhost:81', {transports: ['websocket'], upgrade: false});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question