如何得到结点。在我的服务器上工作的js ?

时间:2022-06-22 03:21:11

I am new to node.js and have got it working fine on localhost.

我是node的新手。并且在本地主机上运行良好。

For localhost I am using xampp, then have a .bat file which runs node app.js (runs the server).

对于本地主机,我正在使用xampp,然后有一个.bat文件,它运行node app.js(运行服务器)。

It works perfectly and runs the game as you can see below with both characters appearing on each screen and the positions being communicated over the server.

它可以完美地运行游戏,如下所示,两个字符出现在每个屏幕上,并且位置通过服务器进行通信。

如何得到结点。在我的服务器上工作的js ?

Then when I try and run it on the server after using ssh to run "node app.js" it outputs info - socket.io started like it does when I run it on local host, but then when I run the app I get the following errors (I had these errors previously when trying to get it working on local host - they appear when the server doesn't run properly..):

然后,当我使用ssh运行“node app.js”并尝试在服务器上运行时,它会输出info - socket。io开始时和我在本地主机上运行时一样,但是当我运行这个应用时,我得到了以下错误(我之前在尝试让它在本地主机上运行时遇到过这些错误——它们在服务器运行不正常时出现):

如何得到结点。在我的服务器上工作的js ?

Any help would be great, Thanks!

任何帮助都可以,谢谢!

on the client side I have this code in the html file: <script src="http://localhost:8080/socket.io/socket.io.js"></script> and var socket = io.connect('http://localhost:8080');

在客户端,我在html文件中有这段代码:

which is then used to run all the functions. e.g.

然后用来运行所有的函数。如。

socket.on('message', function (data) {
    var player = ig.game.getEntitiesByType( EntityPlayer )[0];
    if(player)
    {
        player.messagebox = player.messagebox + '\n' + data + ' disconnected';
    }
});  `

on the server side I have the code: var app = require('http').createServer(handler), io = require('socket.io').listen(app), fs = require('fs') app.listen(8080);

在服务器端我有代码:var app = require('http').createServer(handler), io = require('socket.io').listen(app), fs = require('fs') app.listen(8080);

2 个解决方案

#1


2  

Change var socket = client.connect('http://stuartjones.me:8080'); to var socket = io.connect('http://stuartjones.me:8080');.

更改var socket = client.connect('http://stuartjones.me:8080');连接到var socket = io.connect('http://stuartjones.me:8080');

Using http://localhost as a connection will not work. You must use your server IP address or domain.

使用http://localhostas连接将不起作用。您必须使用您的服务器IP地址或域。

#2


1  

You must connect to your server and not to localhost. Try this var socket = io.connect('http://255.255.255.255:8080'); where you replace 255.255.255.255 with the IP address of your server. Change the address of your script too: <script src="http://255.255.255.255:8080/socket.io/socket.io.js"></script>

您必须连接到您的服务器,而不是本地主机。尝试var socket = io.connect(' http://255.255.255:8080 ');使用服务器的IP地址替换255.255.255.255。更改脚本的地址:。

Are you sure that your node.js server listens to the 8080 port, that this port is opened (not blocked by a firewall), and that the socket.io module is installed (via npm) on your server?

你确定你的节点。js服务器监听8080端口,该端口被打开(没有被防火墙阻止),以及该套接字。io模块已安装在您的服务器上(通过npm) ?

You can see these posts for more information:

你可以看到更多的信息:

Socket.IO - require is not defined

套接字。IO -需要没有定义。

socket.io - ReferenceError: io is not defined

套接字。io - ReferenceError: io没有定义

#1


2  

Change var socket = client.connect('http://stuartjones.me:8080'); to var socket = io.connect('http://stuartjones.me:8080');.

更改var socket = client.connect('http://stuartjones.me:8080');连接到var socket = io.connect('http://stuartjones.me:8080');

Using http://localhost as a connection will not work. You must use your server IP address or domain.

使用http://localhostas连接将不起作用。您必须使用您的服务器IP地址或域。

#2


1  

You must connect to your server and not to localhost. Try this var socket = io.connect('http://255.255.255.255:8080'); where you replace 255.255.255.255 with the IP address of your server. Change the address of your script too: <script src="http://255.255.255.255:8080/socket.io/socket.io.js"></script>

您必须连接到您的服务器,而不是本地主机。尝试var socket = io.connect(' http://255.255.255:8080 ');使用服务器的IP地址替换255.255.255.255。更改脚本的地址:。

Are you sure that your node.js server listens to the 8080 port, that this port is opened (not blocked by a firewall), and that the socket.io module is installed (via npm) on your server?

你确定你的节点。js服务器监听8080端口,该端口被打开(没有被防火墙阻止),以及该套接字。io模块已安装在您的服务器上(通过npm) ?

You can see these posts for more information:

你可以看到更多的信息:

Socket.IO - require is not defined

套接字。IO -需要没有定义。

socket.io - ReferenceError: io is not defined

套接字。io - ReferenceError: io没有定义