English 中文(简体)
页: 1 IO与部署在Vercel的Hendjs没有连接。
原标题:SocketIO with Nextjs deployed to Vercel, socket is not connecting

我正试图制作下一个数据,以便实时与背书进行沟通,为此,我正在使用<代码>。 它是在当地工作的(即使是在建的),但当被部署到十字架时,我会发现<代码”类型的错误。 WebSocket connection to <URL>ail: 在建立链接前关闭网页。

First I created a custom Nextjs server:

import { createServer } from "http";
import { parse } from "url";
import { Server as SocketIOServer, Socket } from "socket.io";
import next from "next";

const port = parseInt(process.env.PORT || "3000", 10);
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();

console.log("hello");

app.prepare().then(() => {
  const server = createServer((req, res) => {
    const parsedUrl = parse(req.url!, true);
    handle(req, res, parsedUrl);
  }).listen(port);

  // tslint:disable-next-line:no-console
  console.log(
    `> Server listening at http://localhost:${port} as ${
      dev ? "development" : process.env.NODE_ENV
    }`
  );

  const socketIO = new SocketIOServer(server, { transports: ["websocket"] });

  socketIO.on("connection", (socket: Socket) => {
    console.log("connected", socket.id);
  });
});

随后,在前线:

import { io } from "socket.io-client";

useEffect(() => {
  const socket = io(window?.location?.hostname || "http://localhost:3000", {
    reconnectionDelay: 1000,
    reconnection: true,
    reconnectionAttempts: 10,
    transports: ["websocket"],
    agent: false,
    upgrade: false,
    rejectUnauthorized: false
  });

  socket.on("connect", () => {
    console.log("someone connected: ", socket?.id);
  });
}, []);

该法典在当地运作得很广,在<代码>dev<>>> /code> 模式和build &&start上没有任何问题,这两方面都只是罚款。

问题一经部署就开始。

如果我没有在<条码>上添加<>:3000”至<条码>。 我发现错误:

WebSocket connection to wss://xxxxxx.vercel.app/socket.io/?EIO=4&transport=websocket failed: doOpen

如果我补充说,它就变成:

最佳回答

答案是:

是不可能的。 Vercel不支持下游的定制服务器功能。 Shame.

The problem is not in the socket itself, the problem is in Vercel and it doesnt work for the above reason.

无论谁也存在同样的问题,我发现的最容易的工作是简单地迁到Heroku,他们很容易建立起来(我向DO、GCP、AWS等机构领导,但对于不是DevOps guy的人来说,他们都花了更多的时间来建立和更加复杂。 因此,我与Heroku和海关服务器一道开台并运行几分钟。

问题回答

I am also getting this error while deploying this application on the vercel after a lot of searching i got some vercel articles that are tellinf that :: socket is working fine but the issue is generated by vercel because vercel does not support the socket or custom server connection on it , i just want to give u the overview read it on the article and if u really want to chat using nextjs and vercel with socket.io then vercel allows one third party api that can do this type of socket connection that is Pusher ok





相关问题
correct socket.io implementation

I m trying out socket io for my project to show online friends, and I was wondering (actually, it looks kinda strange to me) that whenever I try to rerender the page (it doesn t matter if a user ...

缩略语

我可以把我们的网页连接起来,使用轮椅。

WebSocket stress testing

I would like to do some stress testing to a WebSocket based application. Anyone knows a tool that may help me in this task? Update: I forgot to mention, but I m favoring open source or free tools, ...

Web Sockets - server load

I m trying to learn new technology called Web Sockets. I ve got the setup (pywebsocket as Apache2 module) working and I m playing with examples. http://code.google.com/p/websocket-sample/wiki/samples?...

Web sockets server side processing model

To implement a server supporting clients using web sockets, do servers keep an open HTTP connection with each client? How can this scale? What are the "programming models" when implementing this ...

Will html5 websockets be crippled by firewalls?

I m extremely excited about html5 s websockets spec but I have a concern. These days everyone is operating off of some network, with routers (wired/wireless) that have built in firewalls, windows has ...

ColdFusion Socket Gateway

What is the performance like on a Socket Gateway for CF? EDIT : I meant to ask, given the way its built is it suitable for large scale applications or just demo purposes? I.e 2000+ users being ...

热门标签