我正试图制作下一个数据,以便实时与背书进行沟通,为此,我正在使用<代码>。 它是在当地工作的(即使是在建的),但当被部署到十字架时,我会发现<代码”类型的错误。 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
如果我补充说,它就变成: