socket.io用react build部署在节点/express上时不起作用

发布于 2025-02-08 05:40:48 字数 961 浏览 1 评论 0原文

我已经在我的 node/express 后端设置了 socket.io 实例。前端是反应。当我构建 React 应用程序并将其部署在后端时,一切正常。 但是,当我将代码移至生产服务器时,没有任何通信。我相信这是因为我没有正确初始化客户端,但是我尝试了多次迭代,无法得到它上班。

我已经尝试使用服务器名称,IP地址和Localhost,但没有任何作用。我希望下面有效:

//SOCKET.IO CLIENT SETUP
import { io } from "socket.io-client";
const socket = io(); //there is NO separation between client and server in the production instance
export default socket;

在服务器端,设置 socket.io 实例的代码直接从文档中的 Express 示例中获取。

import express from "express";
const app = express();

//SOCKET.IO SERVER SETUP
import { createServer } from "http";
import { Server } from "socket.io";
const httpServer = createServer();
const io = new Server(httpServer, {});
httpServer.listen(8080);

//SOCKET MANAGEMENT
var connections = 0;
io.on("connection", (socket) => {
  connections++;
  console.log(connections);
});

I have setup a Socket.io instance inside my Node/Express backend. The frontend is React. Everything works fine on my device when I build the React app and deploy it in the backend. However when I move the code to my production server, there is no communication whatsoever. I believe this is because I haven't initialised the client correctly, but I have tried multiple iterations and can't get it to work.

I've tried using the server name, IP address, and localhost, but nothing works. I would expect the below to work:

//SOCKET.IO CLIENT SETUP
import { io } from "socket.io-client";
const socket = io(); //there is NO separation between client and server in the production instance
export default socket;

On the server side, the code to setup the socket.io instance is taken straight from the Express example in the docs.

import express from "express";
const app = express();

//SOCKET.IO SERVER SETUP
import { createServer } from "http";
import { Server } from "socket.io";
const httpServer = createServer();
const io = new Server(httpServer, {});
httpServer.listen(8080);

//SOCKET MANAGEMENT
var connections = 0;
io.on("connection", (socket) => {
  connections++;
  console.log(connections);
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

手心的海 2025-02-15 05:40:48

这是工作的工作:

客户端:使用CORS选项

//REPLACE 'DOMAINNAME' WITH YOUR DOMAIN/IP ADDRESS
const socket = io("http://domainname:8080", {
    withCredentials: true,
    extraHeaders: {
      "my-custom-header": "abcd"
    }
});

服务器端:添加

socket.io docs 在这里
(在文档中并不十分清楚,因为该示例已评论)

这意味着服务器代码对来自多个来源的请求更具弹性。

考虑到服务器/客户端全部从同一个地方提供服务,我仍然不明白为什么我必须考虑CORS,因此,如果有人想用答案来启发这一点,我会很乐意将其标记为正确。

Here's what has worked:

CLIENT-SIDE: USED THE CORS OPTIONS

//REPLACE 'DOMAINNAME' WITH YOUR DOMAIN/IP ADDRESS
const socket = io("http://domainname:8080", {
    withCredentials: true,
    extraHeaders: {
      "my-custom-header": "abcd"
    }
});

SERVER-SIDE: ADD AN ARRAY OF ORIGINS

As detailed in the Socket.IO docs here
(It's not very clear in the docs as the example is commented out)

This means the server code is more resilient against requests originating from multiple sources.

I still don't really understand why I've had to account for CORS given that the server/client is all being served from the same place, so if anyone wants to enlighten this with an answer I will happily mark it as correct.

对你再特殊 2025-02-15 05:40:48

在插座服务器创建的nodejs文件中尝试
io.attach(服务器);

try this in the nodejs file where the socket server created
io.attach(server);

流年里的时光 2025-02-15 05:40:48

尝试IO全球导出

`io = module.exports = require("socket.io")(server, { allowEIO3: true /* false by default**/ });`

Once you try io export globally

`io = module.exports = require("socket.io")(server, { allowEIO3: true /* false by default**/ });`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文