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

I am trying to connect to a remote server with socket.io, but I am having some problems. I am getting t

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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745588857a4634711.html

相关推荐

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

    I am trying to connect to a remote server with socket.io, but I am having some problems. I am getting t

    9小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信