简体   繁体   English

Socket.io,代码始终返回错误 net::ERR_CONNECTION_TIMED_OUT

[英]Socket.io, code returns error net::ERR_CONNECTION_TIMED_OUT consistently

This is a rewrited question from a previous one about a more specific issue, however, I believe I've narrowed it down to a single error code that I get with my reformatted code.这是前一个关于更具体问题的重写问题,但是,我相信我已经将其缩小到我重新格式化的代码中得到的单个错误代码。 While I've done a lot of research on discovering the up-to-date syntax for both the client-side and server-side scripts, it still returns the same error which leads me to believe it might have something to do with my server set-up in some way.虽然我已经做了很多关于发现客户端和服务器端脚本的最新语法的研究,但它仍然返回相同的错误,这让我相信它可能与我的服务器有关以某种方式设置。 While I hope this is not the case, I figured posting a more specific question might help narrow down the issue for any potential solvers.虽然我希望情况并非如此,但我认为发布一个更具体的问题可能有助于缩小任何潜在求解者的问题范围。

Server-side code has changed, and I believe it has no issues in syntax at all as (at least the relevant part for opening up a socket io pathway) is copied straight from a different person who provided current good format for the server-side script.服务器端代码已更改,我相信它在语法上根本没有问题,因为(至少是打开套接字 io 路径的相关部分)是直接从为服务器端提供当前良好格式的其他人那里复制的脚本。 It is posted below它发布在下面

const app = require('express')();
const server = require('http').createServer(app);
const io = require('socket.io')(server);

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.php');
});

io.on('connection', function(socket){
    console.log('Client connected');
});

io.on('disconnect', function(data){
    console.log('Client disconnected');  
    });

server.listen(8002, function(){
  console.log('listening on *:8002');
});

module.exports = app;

For anyone wondering, port 8002 is the port that I opened specifically for nodejs and doesn't interfere with any other functionality, AFAIK.对于任何想知道的人,端口 8002 是我专门为 nodejs 打开的端口,不会干扰任何其他功能,AFAIK。

Now onto the client-side code, which is where I postulate the problem is located within.现在到客户端代码,这是我假设问题所在的地方。 It has seen several changes to try and conform to recent solutions to others issues of similar types, however, it still does not work.它已经看到了一些变化,试图符合最近对类似类型的其他问题的解决方案,但是,它仍然不起作用。

        <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js"></script>
        <script>
            const socket = io('http://mywebsite.com:8002');
            socket.on("connect",function(){
                console.log("socket connected");
            });
            socket.on("disconnect",function(){
                console.log("socket disconnected");
            });
        </script>

When navigating to the site, nothing is logged to either the server-side or the client-side console, besides the "listening on *:8002" which obviously only indicates that the server-side script had no issues loading.导航到该站点时,除了“listening on *:8002”之外,服务器端或客户端控制台都没有记录任何内容,这显然仅表明服务器端脚本加载没有问题。 This obviously indicates that no connection between the server is created from the client, but as the port open is 8002 (the port the client points to) I am unsure as to why it doesn't create a connection.这显然表明没有从客户端创建服务器之间的连接,但是由于打开的端口是 8002(客户端指向的端口),我不确定它为什么不创建连接。 After a few seconds on the website, multiple GET http://mywebsite.com:8002/socket.io/?EIO=3&transport=polling&t=Mys3YWR net::ERR_CONNECTION_TIMED_OUT errors appear sourcing from index.js:83.在网站上几秒钟后,出现多个来自 index.js:83 的GET http://mywebsite.com:8002/socket.io/?EIO=3&transport=polling&t=Mys3YWR net::ERR_CONNECTION_TIMED_OUT错误。

This was supposed to be the end of my question, but the plot thickens;这应该是我的问题的结尾,但情节变厚了; I just got back from going to the store and noticed that when I turned my computer off, it connected then disconnected, logging both a "client connected" on the server terminal and a "socket connected" on the website.我刚从商店回来,注意到当我关闭计算机时,它连接然后断开连接,在服务器终端上记录了“客户端已连接”和网站上的“套接字已连接”。 Why it would create the socket when I turned my computer off, I have no clue, which is where I'm hoping someone could give me a clue.为什么当我关闭计算机时它会创建套接字,我不知道,这是我希望有人能给我一个线索的地方。 Any help is greatly appreciated.任何帮助是极大的赞赏。

Found my own solution, and it was almost so simple I feel like an idiot.找到了我自己的解决方案,它几乎是如此简单,我觉得自己像个白痴。 I'm sure some of the problems I had were due to bad code in my previous question about a similar topic, but in this case (with the code I listed which is verified working in 2019 with socket.io 2.3.0) my problem was I specified the io connection as mywebsite.com:8002, replacing it with the websites IP:8002 made the code work.我确定我遇到的一些问题是由于我之前关于类似主题的问题中的错误代码造成的,但在这种情况下(我列出的代码已在 2019 年使用 socket.io 2.3.0 验证)我的问题我是否将 io 连接指定为 mywebsite.com:8002,用网站 IP:8002 替换它使代码工作。 Why this made all the difference, I have some guesses but nothing solid, figuring that since the website has two nameservers it might have been relayed onto the other one which doesn't have any of the node dependencies in it.为什么这一切都不同,我有一些猜测,但没有什么可靠的,我认为由于该网站有两个名称服务器,它可能已经中继到另一个没有任何节点依赖项的服务器上。

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

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