尝试在我的fastapi应用程序中实现websockets,然而,当我从JavaScript端连接到websocket时,它打开了4个连接,我在后端端实现了一个解决方案,检查某个客户是否连接,然而这意味着客户端无法连接到移动的上的websocket,而它在计算机上打开。
class ConnectionManager:
def __init__(self):
self.connections: dict[int, WebSocket] = {}
async def connect(self, websocket, customer_id):
existing_connection = self.connections.get(customer_id)
if existing_connection:
await websocket.close()
raise ExistingConnectionError("Existing connection for customer_id")
await websocket.accept()
websocket.customer_id = customer_id
self.connections[customer_id] = websocket
print(list(self.connections.values()))
async def disconnect(self, websocket):
customer_id = getattr(websocket, 'customer_id', None)
if customer_id in self.connections:
del self.connections[customer_id]
async def broadcast(self, message):
for websocket in self.connections.values():
await websocket.send_json(message)
async def broadcast_to_customer(self, customer_id, message):
matching_connection = self.connections.get(customer_id)
if matching_connection:
await matching_connection.send_json(message)
connection_manager = ConnectionManager()
个字符
JavaScript语言:
let socket;
if (!socket || socket.readyState !== WebSocket.OPEN) {
socket = new WebSocket(`ws://localhost:3002/sock?customer_id=${customer_id}`);
socket.onopen = () => {
console.log('Connected to WebSocket server');
};
let i = 0;
socket.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log('Incoming data:', data);
console.log('i:', i);
i = i + 1;
};
}
型
后端日志:
INFO: connection open
INFO: ('127.0.0.1', 33366) - "WebSocket /sock?customer_id=185" 403
Existing connection detected, rejecting the new connection
INFO: connection rejected (403 Forbidden)
INFO: connection closed
INFO: ('127.0.0.1', 33368) - "WebSocket /sock?customer_id=185" 403
Existing connection detected, rejecting the new connection
INFO: connection rejected (403 Forbidden)
INFO: connection closed
INFO: ('127.0.0.1', 33384) - "WebSocket /sock?customer_id=185" 403
Existing connection detected, rejecting the new connection
INFO: connection rejected (403 Forbidden)
INFO: connection closed
型
前端日志:
Connected to WebSocket server
Firefox can’t establish a connection to the server at ws://localhost:3002/sock?customer_id=185.
Firefox can’t establish a connection to the server at ws://localhost:3002/sock?customer_id=185.
Firefox can’t establish a connection to the server at ws://localhost:3002/sock?customer_id=185.
型
尝试在FastAPI中实现WebSocket,为每个客户端打开多个连接而不是一个连接。
2条答案
按热度按时间7hiiyaii1#
我认为你需要这样的东西:
在每个客户端的每个设备的后端存储连接上:
字符串
当您从前端连接到WebSocket时,添加额外的查询参数
device_hash
,该参数对于每个用户设备都应该是唯一的。您也可以在服务器端从请求头生成此device_hash
(例如,您可以使用user-agent
)5f0d552i2#
此问题可能与您如何检查前端上现有的WebSocket连接有关。
在你的JavaScript代码中,你每次都在创建一个新的WebSocket连接,而没有检查连接是否已经存在。为了确保只建立一个WebSocket连接,你应该以不同的方式处理WebSocket创建逻辑。一种方法是只在WebSocket不存在或不处于打开状态时才创建WebSocket。JavaScript代码的示例修改:
字符串
此修改可确保仅在不存在现有连接或现有连接未处于打开状态时才创建新的WebSocket连接。
您可能希望处理onclose事件以处理WebSocket连接意外关闭的情况。