-2

Following on from this question, I am trying to set a variable to see if a socket is connected.

However the question's answer doesnt seem to work. If I console.log the socket variable I get this: Console.log of var socket

As you can see in the image, it says connected: true but when I run this:

console.log(socket.connected); I simply get false. The start of my code looks like this:

var socket = io('http://test.domain.net:1234', {reconnection: false}); console.log("Connected:" + socket.connected);

Community
  • 1
  • 1
Chud37
  • 4,907
  • 13
  • 64
  • 116

1 Answers1

1

I think you should put a timer and then do the console due to the async nature of JavaScript.

var socket = io('http://test.domain.net:1234', {reconnection: false});

setTimeout(function(){ 
    console.log("Connected:" + socket.connected);
}, 3000);

`

Umar Waliyullah
  • 529
  • 6
  • 17
  • As I commented above there's an event fired when connected socket.on('connect',function(){console.log('connected')}); – Molda May 17 '16 at 13:26