Skip to content

Instantly share code, notes, and snippets.

@SirmaXX
Created March 29, 2023 05:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SirmaXX/594b4c35cb2bcbd98e4c37200e7fce04 to your computer and use it in GitHub Desktop.
Save SirmaXX/594b4c35cb2bcbd98e4c37200e7fce04 to your computer and use it in GitHub Desktop.
fastapi websocket and console client
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())
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