gpt4 book ai didi

azure - 通过 Azure API 管理连接时,WebSocket 服务在收到客户端消息后始终关闭连接

转载 作者:行者123 更新时间:2023-12-03 04:54:58 26 4
gpt4 key购买 nike

我在 Azure Kubernetes 上部署了 WebSocket 服务。如果直接连接到服务,效果很好,但是当我尝试通过 API 管理连接我的服务时。它总是在收到客户端的消息后关闭连接。

服务代码

 from fastapi import FastAPI, Request, Header, Response
from fastapi import WebSocket, WebSocketDisconnect, Query
from fastapi.middleware.cors import CORSMiddleware

from pathlib import Path

import os
import time
import websockets
import logging

from custom_logging import CustomizeLogger

app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)

# logger = logging.getLogger(__name__)
config_path=Path(__file__).with_name("log_config.json")
logger = CustomizeLogger.make_logger(config_path)

@app.websocket("/ws")
async def websoc(websocket: WebSocket):
await websocket.accept()
try:
while True:
msg = await websocket.receive_text()
logger.info('recieve:'+ msg)

await websocket.send_text(msg)

except Exception as e:
logger.info(e)

客户端代码

 import websockets
import asyncio
import time

async def send_receive():
async with websockets.connect(
'wss://xxxx.azure-api.net/testwebsocket?subscription-key=xxx',
ping_interval=5,
ping_timeout=20) as websocket:
for i in range(10):
await websocket.send(str(i))
await asyncio.sleep(0.1)
msg = await websocket.recv()
print(msg)

asyncio.run(send_receive())

通过 API 管理调用时我的自定义登录服务的结果 enter image description here

直接调用我的自定义登录服务时的结果 enter image description here

<小时/>

来自后端的网络跟踪。

红色是API管理。 azure 是后端。 enter image description here

来 self 本地计算机的网络跟踪。

红色是API管理。绿色是本地机器。

enter image description here

<小时/>

来自 APIM 的日志 enter image description here enter image description here

最佳答案

APIM 不支持自定义 Sec-WebSocket-Extensions。您的客户端 websocket 库可能会发送一些。您可以在 api 策略中添加一组 Sec-WebSocket-Extensions header 为空并确认。请参阅https://learn.microsoft.com/en-us/azure/api-management/api-management-transformation-policies#SetHTTPheader

关于azure - 通过 Azure API 管理连接时,WebSocket 服务在收到客户端消息后始终关闭连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71308328/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com