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 badges2 Answers
Reset to default 3i 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
评论列表(0条)