在部署快速 api 项目后,我遇到了 websockets 问题 Deta Space 我的测试 websocket 路由不起作用。然而,在我的本地主机上,这条路线运行良好,很抱歉我的英语不好
开始吧,这是我的测试 websocket 路线
app = FastAPI(
title='websocket test',
description="The description",
version='062023.1'
)
allow_all = ['*']
app.add_middleware(
CORSMiddleware,
allow_origins=allow_all,
allow_credentials=True,
allow_methods=allow_all,
allow_headers=allow_all
)
@app.websocket('/ws')
async def websocket_echo(websocket: WebSocket):
await websocket.accept()
try:
while True:
data = await websocket.receive_text()
await websocket.send_text(f'You send: {data}')
except WebSocketDisconnect:
print('user disconnected')
如此简单的代码,从 fast api websocket 教程中获取,可能有点不同 另外,我还有 Vue 组件文本字段,可以在单击发送按钮后将消息发送到此路由
// CREATE CONNECTION
let socket = new WebSocket(`wss://fastapitouch-1-l4518065.deta.app/ws`)
// SEND MESSAGE
socket.send(this.message)
// ON MESSAGE FROM SERVER LOG IT
socket.onmessage = function(event) {
console.log(`Message from server: ${event.data}`)
}
单击后,我的详细空间生成器日志中收到一条错误消息,表明找不到此路线 - #404 找不到路线()
在我的控制台中我收到此消息 - 与“wss://fastapitouch-1-l4518065.deta.app/webs”的 WebSocket 连接失败 ()
我试图找到有关它的信息,但没有人没有部署类似的东西并记下它或制作视频
看起来 Deta Space 不支持 websockets(见第 10 点)。
刚刚看到这个 - 这里有一些有用的信息:
micros:
- name: your-name
public_routes:
- "/test/*"
...
Important Notes for Micros
...
10. Websockets and long-running processes do not work on Micros. (examples: socket.io or Discord bots won’t work).