[英]socket.io, 'Access-Control-Allow-Origin' error
I have set up a node server with socket io turning and trying to connect to it through another server.我已经设置了一个带有套接字 io 转动的节点服务器,并尝试通过另一台服务器连接到它。 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 . XMLHttpRequest 无法加载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'.
当请求的凭据模式为“包含”时,响应中“Access-Control-Allow-Origin”标头的值不得为通配符“*”。 Origin ' https://www.differentServerDomain.fr ' is therefore not allowed access.
因此,不允许访问 Origin ' https://www.differentServerDomain.fr '。 The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
XMLHttpRequest 发起的请求的凭证模式由 withCredentials 属性控制。
my js config:我的js配置:
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 + "\n");
});
I am using node 8.0 and socket io 2.2, Your help will be greatly appreciated.我正在使用 node 8.0 和 socket io 2.2,您的帮助将不胜感激。
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'] });
这对我有用
var socket = io('http://yourDomain:port', { transports: ['websocket', 'polling', 'flashsocket'] });
For users of Socekt io v3对于Socekt io v3 的用户
You need to use option credentials
in cors
options of server config.您需要在服务器配置的
cors
选项中使用选项credentials
。
Example:示例:
const io = require('socket.io')(strapi.server, {
cors: {
origin: "http://localhost:3000",
credentials: true
}
});
Docs:文档:
https://expressjs.com/en/resources/middleware/cors.html https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin https://expressjs.com/en/resources/middleware/cors.html https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
Related topic:相关主题:
Socket.io + Node.js Cross-Origin Request Blocked Socket.io + Node.js 跨域请求被阻止
Migration guide迁移指南
https://socket.io/docs/v3/migrating-from-2-x-to-3-0/index.html#CORS-handling https://socket.io/docs/v3/migrating-from-2-x-to-3-0/index.html#CORS-handling
In Client code add this, I got error "NO Access-Control-Allow-Origin' header is present".在客户端代码中添加这个,我收到错误“NO Access-Control-Allow-Origin' header is present”。 so added this on client side.
所以在客户端添加了这个。
var connectionOptions = { "force new connection" : true, "reconnectionAttempts": "Infinity", "timeout" : 10000, "transports" : ["websocket"] }; this.socket = io.connect('http://localhost:5000',connectionOptions);
Try this, it worked for me:试试这个,它对我有用:
const io = require("socket.io")(httpServer, {
cors: {
origin: "http://localhost:8080",
methods: ["GET", "POST"]
}
});
I recently faced the same issue and the solution that worked for me was the following:我最近遇到了同样的问题,对我有用的解决方案如下:
//in the React project install socket.io-client and then //在 React 项目中安装 socket.io-client 然后
import io from 'socket.io-client'
var socket = io('http://localhost:<your_port_number>', {transports: ['websocket', 'polling', 'flashsocket']});
Hope this modification will help you.希望这个修改能帮到你。
var port = 3000;
var fs = require('fs');
var https = require('https');
var express = require('express');
var app = express();
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "https://www.differentServerDomain.fr https://www.differentServerDomain.fr");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
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);
// start of server
server.listen(port, function(){
console.log('listening on *: '+ port + "\n");
});
Also please check server-side log having this msg " info - unhandled socket.io url " or not.另请检查服务器端日志是否包含此消息“信息 - 未处理的 socket.io url ”。 if shown, please upgrade your socket.io version to 1.0+ .
如果显示,请将您的socket.io版本升级到1.0+ 。 I solved it by this approach.
我通过这种方法解决了它。
npm install socket.io@1.0 npm 安装 socket.io@1.0
or change package.json like that:或者像这样更改 package.json :
"dependencies": {
"socket.io": "^1.0"
}
Specify the following on the client-side.在客户端指定以下内容。
const socket = io('https://yourDomain:3000', { transports: ['websocket'] });
instead of const socket = io('https://yourDomain:3000');
而不是
const socket = io('https://yourDomain:3000');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.