socket.io 未连接

发布于 2025-01-13 05:45:44 字数 1176 浏览 0 评论 0原文

Socket.IO 版本 - 4.4.1

服务器

const express = require("express");
const app = express();
const http = require("http");
const server = http.createServer(app);
const { Server } = require("socket.io");
const cors = require("cors");

const io = new Server(server, {
  cors,
});

const PORT = 3000;

app.use(cors());

io.on("connection", (socket) => {
  console.log("A user connected :)");
  socket.on("msg", console.log);

  socket.on("connect_error", (err) => {
    console.log(`connect_error due to ${err.message}`);
  });
});

server.listen(PORT, () => {
  console.log("listening on *:" + PORT);
});

HTML

身体内部

 <script
      src="https://cdn.socket.io/4.4.1/socket.io.min.js"
    ></script>
    <script>
      // socket.io
      console.log("Connecting..")
      const socket = io("http://localhost:3000")
      socket.on("connect", () => {
        console.log(socket.connected); // true
      });
    </script>

客户端或服务器上都没有错误,但我仍然无法连接到服务器。

Socket.IO version - 4.4.1

Server

const express = require("express");
const app = express();
const http = require("http");
const server = http.createServer(app);
const { Server } = require("socket.io");
const cors = require("cors");

const io = new Server(server, {
  cors,
});

const PORT = 3000;

app.use(cors());

io.on("connection", (socket) => {
  console.log("A user connected :)");
  socket.on("msg", console.log);

  socket.on("connect_error", (err) => {
    console.log(`connect_error due to ${err.message}`);
  });
});

server.listen(PORT, () => {
  console.log("listening on *:" + PORT);
});

HTML

Inside body

 <script
      src="https://cdn.socket.io/4.4.1/socket.io.min.js"
    ></script>
    <script>
      // socket.io
      console.log("Connecting..")
      const socket = io("http://localhost:3000")
      socket.on("connect", () => {
        console.log(socket.connected); // true
      });
    </script>

There is no error on either client or server, but still i am not able to connect to the server.

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

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

发布评论

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

评论(1

莫言歌 2025-01-20 05:45:44

如果您将此:更改

const io = new Server(server, {
   cors,
});

为:

const io = new Server(server);

然后,您的代码开始为我工作。因此,您尝试将 cors 与 socket.io 一起使用的方式有问题。仅供参考,如果您将客户端更改为始终使用 webSocket 作为传输,那么您的 socket.io 连接将不需要 CORS,因为 websocket 连接不受同源限制。

const socket = io("http://localhost:3000", {transports: ["websocket"]});

默认情况下,socket.io 连接以 http 轮询(受同源限制)开始,然后在几个 http 请求后切换到 websocket。

If you change this:

const io = new Server(server, {
   cors,
});

to this:

const io = new Server(server);

Then, your code starts working for me. So, something is wrong with how you're trying to use cors with socket.io. FYI, if you change the client to always use a webSocket as the transport, then you won't need CORS for your socket.io connection because a websocket connection is not subject to same origin restrictions.

const socket = io("http://localhost:3000", {transports: ["websocket"]});

By default a socket.io connection starts with http polling (which is subject to same origin restrictions) and then switching to a websocket after a few http requests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文