我正在尝试像这样在我的React客户端和SocketIO连接之间建立连接。
socket = SocketIO(app, cors_allowed_origins="*")
@socket.on('test')
def test(data):
print("test")
print(data)
和客户端连接
//wrapped in a async function
const socket = openSocket('http://localhost:5000');
export const sendMessage = (user, content, attachments) =>{
const data = {
sender: user,
content: content,
attachments: attachments,
timeStamp: Date.now()
}
try{
const res = socket.emit('test', data)
return stringify(res)
}catch(exception){
return exception
}
}
在服务器端,我会收到200响应,就像连接设置正确一样,但是在客户端,我会收到此错误
886313e1-3b8a-5372-9b90-0c9aee199e5d:1 Access to
XMLHttpRequest at 'http://localhost:5000/socket.io/?EIO=3&transport=polling&t=MvQD3QE' from origin 'http://localhost:3000' has been blocked by CORS policy: 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'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
如果我删除cors_allowed_origins="*"
,则在服务器端会得到400,但是在客户端总是会出现错误。