Created
March 29, 2023 05:00
-
-
Save SirmaXX/594b4c35cb2bcbd98e4c37200e7fce04 to your computer and use it in GitHub Desktop.
fastapi websocket and console client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import websockets | |
import json | |
async def hello(): | |
async with websockets.connect('ws://localhost:8001/ws') as websocket: | |
message = {"user_id_foreginkey": 0, "message": "sson123", "game_room_foreignkey": 0, "game_date": "2023-03-29"} | |
await websocket.send(json.dumps(message)) | |
response = await websocket.recv() | |
print(response) | |
asyncio.run(hello()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fastapi import FastAPI, WebSocket | |
app = FastAPI() | |
@app.websocket("/ws") | |
async def websocket_endpoint(websocket: WebSocket): | |
await websocket.accept() | |
while True: | |
data = await websocket.receive_text() | |
await websocket.send_text(f" {data}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment