简体   繁体   English

socket.io重新连接socket.socket.connect不起作用

[英]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. 我在使用与socket.io的多个连接时遇到问题,但我无法使用“ socket.socket.connect”方法,但我从第一个连接获得了反馈。

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 我只是通过添加解决了IE8错误

  <!DOCTYPE html>

at the top of the html 在html的顶部

I think I know why this isn't working. 我想我知道为什么这行不通。 For server-side code, this doesn't seem correct for socket.io. 对于服务器端代码,这对于socket.io似乎并不正确。 The connect method is used for clients and not servers. connect方法用于客户端而不是服务器。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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