简体   繁体   English

如何移植一个socket.io应用

[英]How to port forward a socket.io app

I've made myself a chat application with node.js and socket.io and it works however I don't want to run it from localhost:80 anymore, I want it to be online. 我已经使用node.js和socket.io使自己成为一个聊天应用程序,它可以工作,但是我不想再从localhost:80运行它,我希望它可以在线。 How can I port forward my web app? 如何移植我的Web应用程序? I've tried to portforward a HTTP port but that didn't work. 我尝试过转发一个HTTP端口,但是没有用。 Here's my sever side code: 这是我的服务器端代码:

var express = require("express"),
    app = express(),
    server = require("http").createServer(app),
    io = require("socket.io").listen(server);
server.listen(80);

app.get("/", function(req, res){
    res.sendfile(__dirname + "/index.html");
});

So yeah, all I want to do is instead of having to use "localhost:80" I want to be able to use my IP address to connect. 是的,我要做的就是不必使用“ localhost:80”,我希望能够使用我的IP地址进行连接。

Normal port forwarding from your home router should work just fine. 从家用路由器正常端口转发应该可以正常工作。 A socket.io connection is a webSocket connection underneath and a webSocket connection starts life as an HTTP request and the same original socket is then "upgraded" to the webSocket protocol. 一个socket.io连接是下面的一个webSocket连接,一个webSocket连接作为HTTP请求开始运行,然后将相同的原始套接字“升级”到webSocket协议。 So, client makes HTTP request to server to start the whole process and establish the original connection. 因此,客户端向服务器发出HTTP请求以启动整个过程并建立原始连接。

So, any port forwarding configuration that will work for HTTP should work for a socket.io connection. 因此,任何适用于HTTP的端口转发配置都应适用于socket.io连接。

Socket.io connections are long lived connections (unlike TCP connections) so you also have to make sure there is no infrastructure that won't allow a long lived connection (proxies often need to be specially configured for long lived webSocket connections), but this should apply to port forwarding. Socket.io连接是长期连接(与TCP连接不同),因此您还必须确保不存在不允许长期连接的基础结构(代理通常需要为长期webSocket连接进行特殊配置),但这应该适用于端口转发。


FYI, you don't need .listen() for the socket.io connection. 仅供参考,socket.io连接不需要.listen() You can change this: 您可以更改此:

io = require("socket.io").listen(server);

to this: 对此:

io = require("socket.io")(server);

In case this is not clear to your with port forwrarding... 万一您的端口不清晰,以防万一。

When you are doing port forwarding on your home router, you direct incoming requests on a certain port to a specific computer and port on your home network. 在家庭路由器上进行端口转发时,可以将特定端口上的传入请求定向到家庭计算机上的特定计算机和端口。 Then, when you want to connect from the internet, you connect to your router's public IP address and the port that you have configured for port forwarding. 然后,当您要从Internet连接时,您将连接到路由器的公用IP地址和为端口转发配置的端口。 The router then uses the port forwarding rule to reroute any incoming request on the desired port to the specific server on your local network. 然后,路由器使用端口转发规则将所需端口上的任何传入请求重新路由到本地网络上的特定服务器。 You have to be very careful about this because if you make mistakes or don't have your app properly secured, this could open up a point of attack where people on the internet might be able to attack the internals of your home network. 您必须对此非常小心,因为如果您犯了错误或没有适当地保护您的应用程序,这可能会打开一个攻击点,Internet上的人们可能会攻击您家庭网络的内部。

Usually, this is not a long term scheme and it's better to pay a hosting company to run your app. 通常,这不是一个长期计划,最好向托管公司付款以运行您的应用程序。 Then, you can also get a permanent IP address and can set up a domain name to correspond to that IP address. 然后,您还可以获得一个永久IP地址,并可以设置一个与该IP地址相对应的域名。

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

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