本文介绍了使用公共IP运行/socket.io/socket.io.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我试图在我的私人服务器上运行我的Node.js。现在,我创建了一个聊天程序,这个程序在我的本地网络上运行时效果很好,并且都很好用。



现在,当我将IP更改为我的公共IP它不会加载 /socket.io/socket.io.js 文件。它说 net :: ERR_CONNECTION_TIMED_OUT



我连接的代码部分是:



Server.js:

  var mongo = require('mongodb')。MongoClient ,
client = require('socket.io')。listen(8080).sockets;
console.log('info - socket.io started');

mongo.connect('mongodb://127.0.0.1/chat',function(err,db){
if(err)throw err;
$ b

Index.html:

 < ; script src =http://94.211.125.196:8080/socket.io/socket.io.js>< / script> 

< script>
//尝试连接
尝试{
var socket = io.connect('http://94.211.125.196:8080');
} catch(e){
// Set status以警告用户
}
< / script>

所需端口端口转发到我的服务器( 8080 & 27017

socket.io.js 文件,你需要从别的地方包含js文件或者通过在节点脚本中启动一个httpServer来托管它。尝试使用socket.io cdn:

 < script src = https://cdn.socket.io/socket.io-1.3.5.js\"></script> 


So, I'm trying to run my Node.js on my private server. Now the thing is, I've created a chat program, which works great and all when I run it on a local network.

Now, when I change the IP's to my public IP it won't load the /socket.io/socket.io.js file. It says net::ERR_CONNECTION_TIMED_OUT

The parts of the code where I'm connecting are:

Server.js:

var mongo = require('mongodb').MongoClient,
    client = require('socket.io').listen(8080).sockets;
    console.log('info - socket.io started');

mongo.connect('mongodb://127.0.0.1/chat', function(err, db) {
    if(err) throw err;

Index.html:

<script src="http://94.211.125.196:8080/socket.io/socket.io.js"></script>

<script>
// Try connection
try {
    var socket = io.connect('http://94.211.125.196:8080');
} catch(e) {
    // Set status to warn user
}
</script>

I do have the required ports port forwarded to my server (8080 & 27017)

解决方案

You're not hosting the socket.io.js file. You need to include the js file from somewhere else or host it by starting a httpServer in your node script. Try using the socket.io cdn:

<script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>

这篇关于使用公共IP运行/socket.io/socket.io.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 13:01