我正在创建一个使用 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
我尝试使用我确信可以正常工作的旧存储库,但我遇到了同样的问题。
我尝试在另一台计算机和 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
Server
# -*- coding: utf-8 -*-
import eventlet
import socketio
import numpy as np
import json
import serial
import threading
from scipy.integrate import odeint
sio = socketio.Server()
app = socketio.WSGIApp(sio, static_files={
'/': {'content_type': 'text/html', 'filename': 'index.html'}
})
@sio.on('connect')
def connect(sid, env):
print('conectado ', sid)
@sio.on('disconnect')
def disconnect(sid):
print('desconectado ', sid)
# Other functionalities in the code
#...
if __name__ == '__main__':
print('Inicnando...')
thread = threading.Thread(target=leitura_dados, args=(ser,))
thread.start()
eventlet.wsgi.server(eventlet.listen(('', 2003)), app)
客户端中的连接
Vue.use(new VueSocketIO({
debug: false,
connection: 'http://localhost:2003'
}))
我希望它能像以前一样工作。没有任何 CORS 错误或握手过程中的错误。我不知道为什么它突然停止工作。