简体   繁体   English

如何在 Socket.io 中与 Heroku 服务器建立 Socket 连接?

[英]How To Make Socket Connection To Heroku Server In Socket.io?

I'm made a full stack mern application and hosted the nodejs backend in heroku and the frontend is running on netlify.我制作了一个完整的堆栈 mern 应用程序,并将 nodejs 后端托管在 heroku 中,前端在 netlify 上运行。 All.env variables are set up both for heroku and netlify, I set up cors() also.为 heroku 和 netlify 设置了 All.env 变量,我也设置了 cors()。 The website has chat functionality so I use socket.io for that which that used to work nice in development.该网站具有聊天功能,因此我使用 socket.io 来实现曾经在开发中运行良好的功能。

This is link to my heroku server (backend api) https://ural-shop.herokuapp.com/这是我的 heroku 服务器(后端 api) https://ural-shop.herokuapp.com/的链接

In messenger.js file (This is where the socket connections happens)在 messenger.js 文件中(这是发生套接字连接的地方)

import React, { useEffect, useState, useRef } from "react";
import io from "socket.io-client";

const socketRef = useRef();

useEffect(() => {
      socketRef.current = io.connect("https://ural-shop.herokuapp.com/");
},[]);

index.js file (main file of backend) index.js 文件(后端的主文件)

const express = require("express");
const app = express();
const http = require("http");
const server = http.createServer(app);
const socket = require("socket.io");

app.use(cors());

const io = socket(server, {
  cors: { origin: "https://ural-shop.herokuapp.com/" },
});

server.listen(PORT, () => console.log(`Server on port ${PORT}`));

I only put the neccessary parts of the files.我只放了文件的必要部分。

From google developer console I see this.从谷歌开发者控制台我看到了这个。

Developer Console Output开发者控制台 Output

Error From Network Section Of Developer console来自开发者控制台网络部分的错误

Looking at your screenshot it looks like you've not configured cors correctly on your backend.查看您的屏幕截图,您似乎没有在后端正确配置 cors。 Either put * as value so requests from anywhere are allowed, or put the URL of your frontend application instead of the URL of your backend API.要么将 * 作为值,以便允许来自任何地方的请求,要么将前端应用程序的 URL 代替后端 API 的 URL。 ( https://ural-shop.herokuapp.com/ is what you've configured and it should be the URL of your frontend application) https://ural-shop.herokuapp.com/是您配置的,它应该是您的前端应用程序的 URL)

Let me know if it worked.让我知道它是否有效。

I had the same issue with the same tech stack.我在使用相同的技术堆栈时遇到了同样的问题。 And this took me 3 days to find out this was all CORS issue.这花了我 3 天的时间才发现这都是 CORS 问题。 But you need to include your Heroku server in your CORS whitelist.但是您需要将 Heroku 服务器包含在 CORS 白名单中。

According to the official docs:根据官方文档:

Since Socket.IO v3, you need to explicitly enable Cross-Origin Resource Sharing (CORS).由于 Socket.IO v3,您需要显式启用跨域资源共享 (CORS)。

client-site客户站点

So, everything you have to do is to specify the correct URL according to the environment.所以,你要做的就是根据环境指定正确的URL。 If you want to set only with the production URL, it will work both in the local or prod environment.如果您只想设置生产 URL,它将在本地或生产环境中工作。

Mine is dynamic like this:我的动态是这样的:

const IS_PROD = process.env.NODE_ENV === "production";
const URL = IS_PROD ? "yoursite.herokuapp.com" : "http://localhost:5000";
const socket = io(URL);

server-side服务器端

...
const io = socket(server, {
        cors: {
            origin: ["http://localhost:3000", "yoursite.herokuapp.com"],
        },
    });

Very important : You also have to make sure that you development is in sync with your production side.非常重要:您还必须确保您的开发与生产端同步。 Otherwise, you will test it and see the same error, but only because you didn't update your production, too.否则,您将对其进行测试并看到相同的错误,但这仅仅是因为您也没有更新您的产品。 Yep, this kind of silly mistakes can happen...是的,这种愚蠢的错误可能会发生......

Hope that helps someone in the same situation.希望对处于相同情况的人有所帮助。 This solved my issue.这解决了我的问题。

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

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