drukaman
drukaman

Reputation: 372

socket.io reconnect socket.socket.connect doesn't work

sorry for posting this issue again, but most of the posts related don't answer my question. i'm having issues to use multiple connections with the socket.io i don't get the "socket.socket.connect" method to work, yet i get feedbacks from the first connection.

Here's my structure:

var iosocket = null;
var firstconnection = true;
var ip = "http://xxx.xxx.xxx"
var ipPort = 8081

function callSocket() {
    iosocket = null;
    iosocket = io.connect(ip,{port:ipPort,rememberTransport:true, timeout:1500});               

    if (firstconnection) {
    firstconnection= false;                     
    iosocket = io.connect(ip,{port:ipPort,rememberTransport:true, timeout:1500});   
            iosocket.on('connect', function () {console.log("hello socket");}); 
            iosocket.on('message', function(message) {});//end of message io.socket     
            iosocket.on('disconnect', function () {console.log("disconnected");});
    } else {                
     if (iosocket.connected === true) {
        console.log("heyhey still connected");
        iosocket.disconnect();
     }     
     iosocket.socket.connect(ip,{port:ipPort,rememberTransport:true,timeout:1500});
    }  
};

it simply doesn't get any feedback from the second connection

Upvotes: 0

Views: 264

Answers (2)

talktalk11
talktalk11

Reputation: 26

i simply solved that IE8 bug by adding

  <!DOCTYPE html>

at the top of the html

Upvotes: 1

Blubberguy22
Blubberguy22

Reputation: 1332

I think I know why this isn't working. For server-side code, this doesn't seem correct for socket.io. The connect method is used for clients and not servers. I think you are trying to make the server listen on a port. In that case, you should do:

var socket = require('socket.io');
var express = require('express');
var http = require('http');

var app = express();
var server = http.createServer(app);

var io = socket.listen(server);

io.on('connection', function (client) {
    client.on('someEvent', function(someVariables){
        //Do something with someVariables when the client emits 'someEvent'
        io.emit('anEventToClients', someData);
    });
    client.on('anotherEvent', function(someMoreVariables){
        //Do more things with someMoreVariables when the client emits 'anotherEvent'
        io.emit('anotherEventToClients', someMoreData);
    });
});

server.listen(8000);

Upvotes: 0

Related Questions