This repository has been archived on 2022-03-12. You can view files and clone it, but cannot push or open issues or pull requests.
reValuate/Server/websocketServer.py
Yamozha 91e0557b20 included "database" and primitive balance checking
let's make it so we have a way to integrate it with sql in later stages.
PLEASE FIX the "fstring" in the js and the id to balance sequence
2021-02-03 18:45:10 +02:00

38 lines
1015 B
Python

import asyncio
import websockets
import logging
from ratelimit import limits
import time
from databasefornow import *
logger = logging.getLogger('websockets')
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler())
print("Ready")
# calls in number of requests
# period in seconds
@limits(calls=20, period=5)
async def serveStuff(websocket, path):
requestText = await websocket.recv()
print(requestText)
if requestText == "STATUSONCOIN":
file = open("currency.txt","r")
currencyStatus = file.readline()
file.close()
await websocket.send(currencyStatus)
# bad way to send balance to given user - FIX!!!
elif requestText == "STATUSONUSER":
name, balance = checkBalance("1")
await websocket.send(balance)
else:
await websocket.send("We don't accept DoS")
start_server = websockets.serve(serveStuff, "localhost", 8765)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()