简体   繁体   English

尝试通过socket.io连接到服务器时出现CORS错误

[英]CORS error when trying to connect to the server via socket.io

I'm getting 我越来越

Access to XMLHttpRequest at 'http://localhost:4000/socket.io/?EIO=3&transport=polling&t=Moz5j40' from origin 'http://localhost:3000' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value 'http://localhost:3000/' that is not equal to the supplied origin.

error when trying to access a websocket. 尝试访问WebSocket时发生错误。

I've tried setting up the cors package and setting up 我尝试设置cors软件包并进行设置

io.origins("*:*");

backend: 后端:

import express from "express";
import cors from "cors";
import _http from "http";
import _io from "socket.io";

const app = express();
const http = _http.createServer(app);
const io = _io(http);
io.origins("*:*");

app.use(
  cors({
    credentials: true,
    origin: "http://localhost:3000/"
  })
);

io.on("connection", socket => {
  console.log("a user connected");
});

app.listen(4000, () => {
  console.log("Listening on port 4000!");
});

frontend: 前端:

import React, { useState } from "react";
import openSocket from "socket.io-client";

const socket = openSocket(process.env.REACT_APP_API_URL as string);
function subscribeToTimer(
  cb: (err: Error | null, timestamp: number) => void
): void {
  socket.on("timer", (timestamp: number) => cb(null, timestamp));
  socket.emit("subscribeToTimer", 1000);
}
export { subscribeToTimer };

const App: React.FC = () => {
  const [timestamp, setTimestamp] = useState(-1);

  subscribeToTimer((_err, tp) => setTimestamp(tp));

  return <div className="App">Current timestamp {timestamp}</div>;
};

export default App;

I expect to see "a user connected" message in the terminal. 我希望在终端中看到“用户已连接”消息。

添加一个将CORS标头添加到* ...的中间件,不仅是源,而且是方法

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

相关问题 套接字 io CORS 错误无法通过 Socket.io 将客户端连接到 Nodejs 服务器 - Socket io CORS error Can't Connect Client To Nodejs Server Through Socket.io 尝试将 swift 客户端连接到 socket.io 服务器时收到 SSL 错误 - Receiving SSL error when trying to connect swift client to socket.io server Node.js Socket.io 尝试将客户端连接到服务器时出错 - Node.js Socket.io Error when trying to connect client to server socket.io cors 错误。 我尝试在 laravel vuejs 中连接 socket.io 但出现 cors 错误 - socket.io cors error . i try to connect socket.io in laravel vuejs but cors error getting 当我想连接到socket.io(Node.js)时,我在socket.io(Node.js)中有CORS错误 - i have CORS Error in socket.io (Node.js) when i want ot connect to that 通过nodejs socket.io连接cmd线套接字服务器 - connect cmd line socket server via nodejs socket.io 将 socket.io(^3.0.0) 与 Strapi Server(&quot;3.2.5&quot;) CORS 错误集成 - Integrating socket.io(^3.0.0) with Strapi Server("3.2.5") CORS Error Socket.io Express Http-Server 后端,CORS 错误 - Socket.io Express Http-Server Backend, CORS error 使用NodeJS / Express和HTTPS时socket.io中的CORS错误 - CORS Error in socket.io when using NodeJS / Express and HTTPS Socket.io-通过https从客户端连接到服务器 - Socket.io - Connect from client to server via https
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM