Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 将socket.io置于反向代理之后?_Node.js_Socket.io_Reverse Proxy - Fatal编程技术网

Node.js 将socket.io置于反向代理之后?

Node.js 将socket.io置于反向代理之后?,node.js,socket.io,reverse-proxy,Node.js,Socket.io,Reverse Proxy,我最近决定学习socket.io,以实现实时性。我在网站的“入门”页面上写了一些东西,并在本地进行了测试,直到它正常工作 我把它上传到我的服务器上,使用的过程和其他任何东西一样。我在端口8002上运行了它,并将它添加到我的反向代理(使用)下的/pong/*。然后,在端口8002工作之前,我将其代理为/socket.io/*。然而,在使用Firefox进行检查之后,我注意到socket.io只是使用轮询作为一种传输方法,而不是WebSocket,经过进一步思考后,我决定在将来的其他项目中使用soc

我最近决定学习socket.io,以实现实时性。我在网站的“入门”页面上写了一些东西,并在本地进行了测试,直到它正常工作

我把它上传到我的服务器上,使用的过程和其他任何东西一样。我在端口8002上运行了它,并将它添加到我的反向代理(使用)下的
/pong/*
。然后,在端口8002工作之前,我将其代理为
/socket.io/*
。然而,在使用Firefox进行检查之后,我注意到socket.io只是使用轮询作为一种传输方法,而不是WebSocket,经过进一步思考后,我决定在将来的其他项目中使用socket.io时,将
/socket.io/*
发送到8002是不好的

所以我问,如何让多个socket.io程序在反向代理后运行,使用WebSocket作为传输工具


proxy.js
const express=require(“express”)
常数fs=要求('fs');
const http=require('http');
常量https=require('https');
constproxy=require('http-proxy-middleware');
const privateKey=fs.readFileSync('/etc/[path to-letsencrypt]/privkey.pem','utf8');
const certificate=fs.readFileSync('/etc/[path to letsencrypt]/cert.pem','utf8');
const ca=fs.readFileSync('/[path to letsencrypt]/chain.pem','utf8');
var凭证={key:privateKey,cert:certificate,ca:ca};
var-app=express();
应用程序使用(功能(请求、恢复、下一步){
console.log(请求url)
下一个()
})
app.use(“/pong/*”,代理({target:http://localhost:8002,路径重写:{“^/pong”:“},ws:true,changeOrigin:true})
app.use(“/pnw/war/*”,代理({target:http://localhost:8000" }))
app.use(“/pnw/nation/*”,代理({target:http://localhost:8001" }))
应用程序使用(快速静态(“/静态”))
https.createServer(凭据,应用程序).listen(443);
//将所有HTTP通信重定向到HTTPS
http.createServer(函数(req,res){
res.writeHead(301,{“位置”:“https://”+req.headers['host']+req.url});
res.end();
}).听(80);

pong.js
var-app=require('express')();
var http=require('http')。服务器(应用程序);
var io=require('socket.io')(http{
路径:“/pong/”
});
app.get('/',函数(req,res){
res.sendFile(uu dirname+'/index.html');
});
http.listen(8002,函数(){
console.log('监听*:8002');
});

index.html

变量套接字=io({
//传输:['websocket',升级:false,(用于测试)
路径:“/pong”
}) 
// ...

我现在所得到的是对这个问题的以下回答:

但是,在firefox控制台中,我收到一条警告,内容如下:
为具有源的加载失败”https://curlip.xyz/pong/socket.io.js“
,后跟一个错误
未定义io
。在网络选项卡中,get socket.io.js显示一个404


因此,我认为正在发生的事情是,由于express正在捕获对/的请求,socket.io无法(出于某种原因)服务器socket.io.js。然而,当我将/更改为
/index.html
并加载时,没有任何更改。

因此我做了更多的研究并找到了解决方案。我打开了EC2上的8002端口,以便四处寻找
socket.io.js

基本上,我发现的是
socket.io.js
位于
/pong/pong/socket.io.js
,因为我将pong.js中的路径设置为“pong”,事后看来,代理添加了一个“pong”,而socket.io本身正在捕获“/pong”

知道了这一点,我删除了pong.js中的path选项,因此socket.io.js可以在
/pong/socket.io/socket.io.js
中找到。然后,我通过更改index.html中的脚本标记和路径选项,使客户机指向这一点


pong.js
var-app=require('express')();
var http=require('http')。服务器(应用程序);
var io=require('socket.io')(http);
app.get('/',函数(req,res){
res.sendFile(uu dirname+'/index.html');
});
http.listen(8002,函数(){
console.log('监听*:8002');
});

index.html

变量套接字=io({
路径:“/pong/socket.io/”
})