net :: ERR_CONNECTION_REFUSED socket.io

时间:2017-07-31 10:42:45

标签: node.js sockets socket.io

我试图在我的服务器上运行套接字

这是我的app.js文件

 console.log('app.js says hi');
 var express = require('express');  
 var app = express();  
 var server = require('http').createServer(app);  
 var io = require('socket.io')(server);
app.use(express.static(__dirname + '/bower_components'));  
app.get('/', function(req, res,next) {  
res.sendFile(__dirname + '/index.html');
});
server.listen(4200);  

和我的index.html文件

     <!doctype html>  
     <html lang="en">  
     <head>

    </head>
    <body>
    <h1>Hello World!</h1>
    <div id="future"></div>
    <form id="form" id="chat_form">
        <input id="chat_input" type="text">
        <input type="submit" value="Send">
    </form>
     <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
    <script src="socket.io.js"></script>    
    <script>  
 var socket = io.connect('http://localhost:4200');
socket.on('connect', function(data) {
    socket.emit('join', 'Hello World from client');
 });
</script>        
</body>
</html>  

并在控制台日志中出现此错误 获取http://localhost:4200/socket.io/?EIO=3&transport=polling&t=LsOF2yq net :: ERR_CONNECTION_REFUSED

1 个答案:

答案 0 :(得分:0)

我找到了

替换此行

var socket = io.connect(&#39; http://localhost:4200&#39;);

var socket = io.connect(&#39; http://yourdomain.com:4200&#39;);