Last active
December 15, 2019 16:51
-
-
Save rajagurunath/54d626386de599a4bbcb52f81c13389c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| """ | |
| TODO:to fix bug | |
| not able consume complete message from websocker server | |
| stops after receiving first messages | |
| have to handle it properly | |
| """ | |
| import asyncio | |
| import websockets | |
| async def hello(): | |
| uri = "ws://localhost:8000/ws" | |
| async with websockets.connect(uri,ping_interval=100) as websocket: | |
| name = input("Enter the message: ") | |
| await websocket.send(name) | |
| print(f"> {name}") | |
| greeting = await websocket.recv() | |
| print(greeting) | |
| # for g in websocket: | |
| # print(f"< {g}") | |
| # # print(greeting) | |
| # while greeting: | |
| # greeting=await websocket.recv() | |
| # print(f"< {greeting}") | |
| asyncio.get_event_loop().run_until_complete(hello()) |
This file contains hidden or 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
| """ | |
| websockets Example | |
| websockets client: | |
| python -m websockets ws://localhost:8000/ws | |
| """ | |
| from fastapi import FastAPI | |
| from starlette.responses import HTMLResponse | |
| from starlette.websockets import WebSocket | |
| import json | |
| import time | |
| app = FastAPI() | |
| html = """ | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>Chat</title> | |
| </head> | |
| <body> | |
| <h1>WebSocket Chat</h1> | |
| <form action="" onsubmit="sendMessage(event)"> | |
| <input type="text" id="messageText" autocomplete="off"/> | |
| <button>Send</button> | |
| </form> | |
| <ul id='messages'> | |
| </ul> | |
| <script> | |
| var ws = new WebSocket("ws://localhost:8000/ws"); | |
| ws.onmessage = function(event) { | |
| var messages = document.getElementById('messages') | |
| var message = document.createElement('li') | |
| var content = document.createTextNode(event.data) | |
| message.appendChild(content) | |
| messages.appendChild(message) | |
| }; | |
| function sendMessage(event) { | |
| var input = document.getElementById("messageText") | |
| ws.send(input.value) | |
| input.value = '' | |
| event.preventDefault() | |
| } | |
| </script> | |
| </body> | |
| </html> | |
| """ | |
| @app.get("/") | |
| async def get(): | |
| return HTMLResponse(html) | |
| @app.websocket("/ws") | |
| async def websocket_endpoint(websocket: WebSocket): | |
| await websocket.accept() | |
| while True: | |
| resp=await websocket.receive_text() | |
| if resp=='print': | |
| data ={}# await websocket.receive_text() | |
| for i in range(10): | |
| data[i]=f"msg-{i}" | |
| data=json.dumps(data) | |
| print(type(data)) | |
| await websocket.send_text(f"Message text was: {data}") | |
| if resp=="print10": | |
| data ={}# await websocket.receive_text() | |
| for i in range(10): | |
| data[i]=f"msg-{i}" | |
| data_str=json.dumps(data) | |
| await websocket.send_text(f"Message text was: {data_str}") | |
| if resp=="print100": | |
| data ={}# await websocket.receive_text() | |
| for i in range(100): | |
| #time.sleep(2) | |
| data[i]=f"msg-{i}" | |
| #data_str=json.dumps(data) | |
| await websocket.send_json(data) | |
| else: | |
| data=f"GOT RESPONSE -{resp}" | |
| await websocket.send_text(f"Message text was: {data}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment