最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to fix this CORS issue in socket.io? - Stack Overflow

matteradmin4PV0评论

I am trying to connect to a remote server with socket.io, but I am having some problems. I am getting this error: The value of the Access-Control-Allow-Credentials header in the response is ' ' which must be 'true' when the request's credentials mode is 'include'

OK, so here is the code:

Server

var server = require('http').createServer();

const io = require("socket.io")(server, {
  cors: {
    origin: "my URL",
    methods: ["GET", "POST"],
    credentials: false
  }
});


io.sockets.on('connection', function(socket) {
    console.log("Client has connected!");
});

console.log ('Server started.');
server.listen(3000);

Here is the client code:

var socket = io.connect(";, {
  withCredentials: false
});

How can I solve this and get it to connect?

Thanks for any help!

I am trying to connect to a remote server with socket.io, but I am having some problems. I am getting this error: The value of the Access-Control-Allow-Credentials header in the response is ' ' which must be 'true' when the request's credentials mode is 'include'

OK, so here is the code:

Server

var server = require('http').createServer();

const io = require("socket.io")(server, {
  cors: {
    origin: "my URL",
    methods: ["GET", "POST"],
    credentials: false
  }
});


io.sockets.on('connection', function(socket) {
    console.log("Client has connected!");
});

console.log ('Server started.');
server.listen(3000);

Here is the client code:

var socket = io.connect("https://persistent-gentle-banon.glitch.me", {
  withCredentials: false
});

How can I solve this and get it to connect?

Thanks for any help!

Share Improve this question edited Jan 4, 2021 at 17:28 Andrzej Sydor 1,4096 gold badges14 silver badges32 bronze badges asked Jan 4, 2021 at 16:46 AlenAlen 352 silver badges9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

i had the same problem, i was using express and i was able to solve it by

var cors = require("cors");
const corsOptions = {
  origin: "*",
  optionsSuccessStatus: 200
};
const io = require("socket.io")(server, {
  cors: {
    origin: "*",
    methods: ["PUT", "GET", "POST", "DELETE", "OPTIONS"],
    credentials: false
  }
  // transports: ['websocket']
});

app.use(cors(corsOptions));

According to this link https://socket.io/docs/v4/server-instance/#serverengine , you should use io.engine initial_headers and headers events to set cors headers simply, this should work with socket.io v4+.

import { createServer } from "http";
import {Server} from  'socket.io';

const server = createServer(app);
const io = new Server(server);

io.engine.on("initial_headers", (headers, req) => {
  headers["Access-Control-Allow-Origin"] = "http://localhost:4200";
});

io.engine.on("headers", (headers, req) => {
  headers["Access-Control-Allow-Origin"] = "http://localhost:4200"; // url to all
});

server.listen(3000);
Post a comment

comment list (0)

  1. No comments so far