简体   繁体   中英

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

i simply solved that IE8 bug by adding

  <!DOCTYPE html>

at the top of the html

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM