[英]Socket.io with client on Firebase Hosting and Server on Heroku
I've created an app that uses Angular for the front-end and node.js for the backend.我创建了一个应用程序,它使用 Angular 作为前端,使用 node.js 作为后端。 I'm hosting the front-end on Firebase Hosting and the server on Heroku (because I read that Firebase Functions doesn't support socket.io).我在 Firebase 主机上托管前端,在 Heroku 上托管服务器(因为我读到 Firebase 功能不支持套接字。)
In my socket.service.ts
file, I connect to the socket using this.socket = io(environment.socket_url);
在我的socket.service.ts
文件中,我使用this.socket = io(environment.socket_url);
连接到套接字( socket_url
is http://localhost:3000/
for the development environment and https://PROJECT_NAME_HERE.herokuapp.com:3000
for production). ( socket_url
是http://localhost:3000/
用于开发环境和https://PROJECT_NAME_HERE.herokuapp.com:3000
生产):
The socket connection in my server.ts
file is:我的server.ts
文件中的套接字连接是:
const port = process.env.PORT || 3000;
const server = new http.Server(app);
const io = new Server(server, {
cors: {
origin: socketUrl,
methods: ["GET", "POST"]
}
});
where socketUrl is http://localhost:4200
when in development and https://PROJECT_NAME_HERE.web.app/
in production.其中 socketUrl 是http://localhost:4200
在开发中和https://PROJECT_NAME_HERE.web.app/
在生产中。
This setup works in development, but doesn't work in production.此设置在开发中有效,但在生产中无效。 How do I get this to work?我怎样才能让它工作?
EDIT: This is the error I get: Access to XMLHttpRequest at 'https://PROJECT_NAME_HERE.herokuapp.com/socket.io/?EIO=4&transport=polling&t=NPu1t39' from origin 'https://PROJECT_NAME_HERE.web.app' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value 'https://PROJECT_NAME_HERE.web.app/' that is not equal to the supplied origin.
编辑:这是我得到的错误: Access to XMLHttpRequest at 'https://PROJECT_NAME_HERE.herokuapp.com/socket.io/?EIO=4&transport=polling&t=NPu1t39' from origin 'https://PROJECT_NAME_HERE.web.app' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value 'https://PROJECT_NAME_HERE.web.app/' that is not equal to the supplied origin.
I am also using app.use(cors({origin: true}));
我也在使用app.use(cors({origin: true}));
Trouble isolation steps:故障隔离步骤:
origin
in the CORS configuration with *
and see if you receive the exact same error.将 CORS 配置中的origin
替换为*
并查看是否收到完全相同的错误。 cors: {
origin: "*",
methods: ["GET", "POST"]
}
cors: {
origin: "https://PROJECT_NAME_HERE.web.app",
methods: ["GET", "POST"]
}
If either of these produce the exact same error, you've missed a configuration parameter (probably credentials
).如果其中任何一个产生完全相同的错误,则您错过了一个配置参数(可能是credentials
)。 You'll need to determine which parameter you'll need to add from here .您需要确定需要从此处添加的参数。
Otherwise, you've not included enough details about additional or custom headers, pre-flight requirements, authentication (credentials/cookies), etc.否则,您没有包含有关附加或自定义标头、飞行前要求、身份验证(凭据/cookie)等的足够详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.