socket.io, #39;Access-Control-Allow-Origin#39; error(socket.io,“访问控制允许来源错误)
问题描述
I have set up a node server with socket io turning and trying to connect to it through another server. However some browsers on different computers give me this error and makes it reconnect the whole time:
XMLHttpRequest cannot load https://serverDomain.net:3000/socket.io/?EIO=3&transport=polling&t=Lo_SdiU. The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'https://www.differentServerDomain.fr' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
my js config:
var port = 3000;
var fs = require('fs');
var https = require('https');
var express = require('express');
var app = express();
var options = {
key: fs.readFileSync('/etc/letsencrypt/live/devpeter.net/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/devpeter.net/fullchain.pem')
};
var server = https.createServer(options, app);
var io = require('socket.io')(server);
io.origins('https://www.differentServerDomain.fr:* https://www.differentServerDomain.fr/wp-admin/index.php:*');
// start of server
server.listen(port, function(){
console.log('listening on *: '+ port + "
");
});
I am using node 8.0 and socket io 2.2, Your help will be greatly appreciated.
EDIT: here is the client code:
<script src="https://serverDomain.net:3000/socket.io/socket.io.js"></script>
<script>
var socket = io('https://serverDomain.net:3000');
</script>
I have found a solution. for some reason the default transportation method is not always allowed by all servers.
So i specified a neutral transportation method at the client side, like this:
var socket = io('https://yourDomain:3000', { transports : ['websocket'] });
这篇关于socket.io,“访问控制允许来源"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:socket.io,“访问控制允许来源"错误
基础教程推荐
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01