简体   繁体   English

Socket.io错误-ERR_CONNECTION_TIMED_OUT

[英]Socket.io error - ERR_CONNECTION_TIMED_OUT

I am trying to build a basic chat html program. 我正在尝试建立一个基本的html聊天程序。 I have set up a node server to test it, however, when accessing the html page, I get an socket error. 我已经设置了一个节点服务器对其进行测试,但是,当访问html页面时,出现套接字错误。 It's my first time working with node.js and setting up node server, so I've surely done something wrong. 这是我第一次使用node.js并设置节点服务器,因此我肯定做错了。

Thanks to everyone who's taking a look at this! 感谢所有正在看这个的人!

 var express = require('express'); var app = express(); var server = require('http').createServer(app); var io = require("socket.io").listen(server); var socket = io.listen(1223, "1.2.3.4"); server.listen(process.env.PORT || 3000); console.log('Server is running...'); var people = {}; app.get('/', function(req, res){ res.sendFile(__dirname + '/index.html'); }) // When connecting socket.on("connection", function(client) { client.on("join", function(name){ people[client.id] = name; client.emit("update", "You have connected to the server."); socket.sockets.emit("update", name + " has joined the server."); socket.sockets.emit("update-people", people); }); // When sending client.on("send", function(msg){ socket.sockets.emit("chat", people[client.id], msg); }); // When disconnecting client.on("disconnect", function(){ socket.sockets.emit("update", people[client.id] + " has left the server."); delete people[client.id]; socket.sockets.emit("update-people", people); }); }); 
 <!DOCTYPE html> <html lang="en"> <head> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script> <script src="/socket.io/socket.io.js"></script> </head> <script> $(document).ready(function(){ var socket = io.connect("1.2.3.4:1223"); $("#chat").hide(); $("#name").focus(); $("form").submit(function(event){ event.preventDefault(); }); $("#join").click(function(){ var name = $("#name").val(); if (name != "") { socket.emit("join", name); $("#login").detach(); $("#chat").show(); $("#msg").focus(); ready = true; } }); $("#name").keypress(function(e){ if(e.which == 13) { var name = $("#name").val(); if (name != "") { socket.emit("join", name); ready = true; $("#login").detach(); $("#chat").show(); $("#msg").focus(); } } }); socket.on("update", function(msg) { if(ready) $("#msgs").append("" + msg + ""); }) socket.on("update-people", function(people){ if(ready) { $("#people").empty(); $.each(people, function(clientid, name) { $('#people').append("" + name + ""); }); } }); socket.on("chat", function(who, msg){ if(ready) { $("#msgs").append("" + who + " says: " + msg + ""); } }); socket.on("disconnect", function(){ $("#msgs").append("The server is not available"); $("#msg").attr("disabled", "disabled"); $("#send").attr("disabled", "disabled"); }); // Sending the message (either by button click or enter) $("#send").click(function(){ var msg = $("#msg").val(); socket.emit("send", msg); $("#msg").val(""); }); $("msg").keypress(function(e){ if (e.which == 13) { var msg = $("msg").val(); socket.emit("send", msg); $("#msg").val(""); } }); }); </script> <body> <div class="container"> <div class="row"> <div class="span2"> <ul id="people" class="unstyled"></ul> </div> <div class="span4"> <ul id="msgs" class="unstyled"> </div> </div> <div class="row"> <div class="span5 offset2" id="login"> <form class="form-inline"> <input type="text" class="input-small" placeholder="Your name" id="name"> <input type="button" name="join" id="join" value="Join" class="btn btn-primary"> </form> </div> <div class="span5 offset2" id="chat"> <form id="2" class="form-inline"> <input type="text" class="input" placeholder="Your message" id="msg"> <input type="button" name="send" id="send" value="Send" class="btn btn-success"> </form> </div> </div> </div> </body> </html> 

You mixed up the different listen calls. 您混合了不同的listen呼叫。

var server = require('http').createServer(app);
var io = require("socket.io").listen(server);
var socket = io.listen(1223, "1.2.3.4"); 
server.listen(process.env.PORT || 3000);

The third line has not the effect you expect. 第三行没有您期望的效果。 io is not listening itself. io不听自己。 It uses the http server that is listening on port 3000. 它使用在端口3000上侦听的http服务器。

Rather use this on the server 而是在服务器上使用它

var server = require('http').createServer(app);
var socket = require("socket.io").listen(server);
server.listen(process.env.PORT || 3000);   

And in the client connect to port 3000 (or the port configured via the PORT environment variable. 并在客户端中连接到端口3000(或通过PORT环境变量配置的端口)。

var socket = io.connect("your.domain:3000");

If you want to listen on a dedicated port for socket.io, you have to create a second http server listening on a second port, and bind that one to socket.io. 如果要在socket.io的专用端口上侦听,则必须创建在第二个端口上侦听的第二个http服务器,并将该端口绑定到socket.io。

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

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