最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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);
发布评论

评论列表(0)

  1. 暂无评论