简体   繁体   English

在Flask socketio应用上连接时出错

[英]Error while connecting on Flask socketio app

I have a Flask socket.io app (backend) hosted on Heroku. 我在Heroku上托管了Flask socket.io应用程序(后端)。

I have frontend with the JS code to connect to the host at my local machine. 我的JS代码前端可以连接到本地计算机上的主机。 I am getting the following error while trying to connect to the server. 尝试连接到服务器时出现以下错误。

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://deploy-appp.herokuapp.com/socket.io/?

EIO=3&transport=polling&t=MH05dP-. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://deploy-appp.herokuapp.com/socket.io/?

EIO=3&transport=polling&t=MH05f5A. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

Link to the backend application 链接到后端应用程序

JS code for socket connection: 套接字连接的JS代码:

<script type="text/javascript">
var socket = io.connect('https://deploy-appp.herokuapp.com/');
socket.on('connect', function() {
console.log('connected');
});
socket.on('message', function(msg) {
document.getElementById('messages').append('<li>'+msg+'</li>');
console.log('Received message');
});
function send() {

console.log(document.getElementById('myMessage').value)
socket.send(document.getElementById('myMessage').value)

};
</script>

Check out the cors wiki . 查看cors Wiki Usually you'll get an error like this if your front end application is 'running' from a different uri to your backend. 通常,如果您的前端应用程序正在从另一个uri“运行”到后端,则会出现这样的错误。 Say for example, the host of your client side code is myclient.com but your socket.io back end is running on anotherclient.com . 举例来说,您的客户端代码的宿主是myclient.com,但是您的socket.io后端在anotherclient.com上运行。

There are a number of solutions to this, you can explicitly set the origins, see this answer Socket.io + Node.js Cross-Origin Request Blocked . 有很多解决方案,您可以显式设置起源,请参见此答案Socket.io + Node.js跨域请求已阻止 (This is quickest thing if you're running your web application local, but not secure long term) (如果您在本地运行Web应用程序,这是最快的方法,但长期不安全)

Alternatively, have a reverse proxy in front of your client web server and your socketio app, where the proxy would route the client /socketio calls to your socketio server, and the other request to the server hosting your site. 或者,在客户端Web服务器和socketio应用程序之前有一个反向代理,该代理会将客户端/ socketio调用路由到您的socketio服务器,并将另一个请求路由到托管您的站点的服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM