简体   繁体   English

Socket.io 生产连接 URL

[英]Socket.io connection URL for production

When in development env, this works as expected, Server Code... In development, I only specify the url in the client as const socket = io("http://localhost:3001");在开发环境中,这按预期工作,服务器代码...在开发中,我仅在客户端中将 url 指定为 const socket = io("http://localhost:3001"); Below is the server code.下面是服务器代码。

const httpServer = createServer(app);
export const io = new Server(httpServer, {
  cors: {
    origin: "*",
  },
  path: "/socket.io/",
});
io.on("connection", (socket) => {
  // make function calls
  postOrderDeliveryStatus(socket);
  // getOrders(socket);
  // console.log("Socket Connected");

  // close socket
  socket.on("disconnect", () => {
    socket.disconnect(true);
  });
});

httpServer.listen(3001);

//mongoose
// console.log(process.env.CONNECTION_URL)
mongoose
  .connect(process.env.CONNECTION_URL, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useCreateIndex: true,
  })
  .then(() =>
    app.listen(PORT, () =>
      winston.info(
        `Server Running in ${process.env.NODE_ENV} mode on Port: ${PORT}`
      )
    )
  )
  .catch((error) => console.log(`${error} did not connect`));
mongoose.set("useFindAndModify", false);

Client...客户...

const socket = io("http://localhost:3001");
  
  const dispatchOrderHandler = () => {
    const delivery_status = "DISPATCHED";
    socket.emit("update-order", order._id, delivery_status);
    // dispatch(getOrderDetailsToProcess(paramsOrderId)); //This dispatch  alone can suffice
    socket.on("updated-order", (updated_order) => {
      dispatch(
        getOrderDetailsToProcess(paramsOrderId, (order) => [
          ...order,
          updated_order,
        ])
      );
    });
  };

The error i get when sockets run is.net::ERR_CONNECTION_REFUSED. sockets 运行时出现的错误是.net::ERR_CONNECTION_REFUSED。 I also tried const socket = io();我也试过 const socket = io(); without specifying the url since both the client and the server run on the same domain but that didn't either?没有指定 url 因为客户端和服务器都在同一个域上运行但两者都没有? What should the connection URL be in production since specifying the domain name hasn't worked?由于指定域名无效,连接 URL 应该在生产中是什么? Am using heroku!我正在使用 heroku!

When using a hosting service, the service will provide the port to the client.使用托管服务时,该服务将向客户端提供端口。 You can access that port through process.env.PORT .您可以通过process.env.PORT访问该端口。 To make the experience a bit more friendly to yourself as a developer, you can listen to the port using httpServer.listen(process.env.PORT || 3001);作为开发人员,为了让体验更加友好,您可以使用httpServer.listen(process.env.PORT || 3001);

As for the client, I believe you are right on using const socket = io();至于客户端,我相信你使用const socket = io();是正确的。 , although I haven't used socket.io in a while. ,虽然我已经有一段时间没有使用 socket.io 了。

Use const socket = io("serverurl") to connect to a specific server.使用const socket = io("serverurl")连接到特定服务器。

I believe you can specify a port there as well.我相信您也可以在那里指定一个端口。

So like for my website, I would do: const socket = io("us2.swordbattle.io:3000")所以就像我的网站一样,我会这样做: const socket = io("us2.swordbattle.io:3000")

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

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