Gus*_*rio 3 python sockets cors vue.js
我正在创建一个使用 Vue.js(作为客户端)和 Python(作为服务器)的项目。Python用于一些计算,Vue.js用于接口。我正在使用 python-socketio ( https://python-socketio.readthedocs.io/en/latest/ ) 和 Vue-socket.io ( https://github.com/MetinSeylan/Vue-Socket.io )连接它们. 几周前它工作得很好。连接和通信正在成功进行。但是几天前,我再次尝试运行相同的代码并出现此错误:
? Access to XMLHttpRequest at shttp://localhost:2003/socket.io/?EI0.38transport.polling&t=Mom6k2V' from origin 'http://1 :1 ocalhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
? GET http://localhost:2003/socket.io/?EI0=3&transport=polling&t=Mom6k2V net::ERR FAILED vue-socketio.js?5132:8
Run Code Online (Sandbox Code Playgroud)
我尝试使用我确信可以正常工作的旧存储库,但我遇到了同样的问题。
我尝试在另一台计算机和 Raspberry Pi 中运行相同的代码,但遇到了同样的问题。
我尝试使用 --disable-web-security 运行 chrome 以禁用 cors,但出现以下错误:
? WebSocket connection to 'ws://localhost:2003/socket.io/? vue-socketio.js?5132:10 EI0.3&transport=websocket&sid=7111830544fa4dfd98c3424afd25c10e failed: Error during WebSocket handshake: Unexpected response code: 400
Run Code Online (Sandbox Code Playgroud)
服务器
? Access to XMLHttpRequest at shttp://localhost:2003/socket.io/?EI0.38transport.polling&t=Mom6k2V' from origin 'http://1 :1 ocalhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
? GET http://localhost:2003/socket.io/?EI0=3&transport=polling&t=Mom6k2V net::ERR FAILED vue-socketio.js?5132:8
Run Code Online (Sandbox Code Playgroud)
在客户端连接
Vue.use(new VueSocketIO({
debug: false,
connection: 'http://localhost:2003'
}))
Run Code Online (Sandbox Code Playgroud)
我希望它能像以前一样工作。握手期间没有任何 CORS 错误或错误。我不知道为什么它突然停止工作。
Gus*_*rio 18
深入了解文档(https://python-socketio.readthedocs.io/en/latest/api.html?highlight=cors#server-class)我终于找到了答案。代替:
sio = socketio.Server()
Run Code Online (Sandbox Code Playgroud)
用
sio = socketio.Server(cors_allowed_origins='*')
Run Code Online (Sandbox Code Playgroud)
Ahm*_*hab 11
服务器.py
sio = socketio.AsyncServer(cors_allowed_origins=['*']) # * is bad
Run Code Online (Sandbox Code Playgroud)
client.js - 需要额外参数:
let socket = io("http://localhost:8080",
{transports: ['websocket', 'polling', 'flashsocket']}
)
Run Code Online (Sandbox Code Playgroud)
复制了! https://github.com/socketio/socket.io-client/issues/641#issuecomment-38276289