diff --git a/Server/__pycache__/databasefornow.cpython-36.pyc b/Server/__pycache__/databasefornow.cpython-36.pyc new file mode 100644 index 00000000..88fdde97 Binary files /dev/null and b/Server/__pycache__/databasefornow.cpython-36.pyc differ diff --git a/Server/databasefornow.py b/Server/databasefornow.py new file mode 100644 index 00000000..910686fe --- /dev/null +++ b/Server/databasefornow.py @@ -0,0 +1,7 @@ +userNames = {"1":"boyan", "2":"nikola"} +usersCoins = {"1":"200", "2":"100"} + +def checkBalance(ID): + userName = userNames[f"{ID}"] + userCoins = usersCoins[f"{ID}"] + return userName, userCoins diff --git a/Server/websocketServer.py b/Server/websocketServer.py index c2868feb..95d22d6d 100644 --- a/Server/websocketServer.py +++ b/Server/websocketServer.py @@ -1,12 +1,19 @@ 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) @@ -15,6 +22,11 @@ async def serveStuff(websocket, path): 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") diff --git a/Website/index.html b/Website/index.html index 416899c3..03705454 100644 --- a/Website/index.html +++ b/Website/index.html @@ -3,12 +3,14 @@ + Currency site -

This is where you'll see the thing

+

Your balance is:

+ diff --git a/Website/scripts/Cookies.js b/Website/scripts/Cookies.js new file mode 100644 index 00000000..1983dbd1 --- /dev/null +++ b/Website/scripts/Cookies.js @@ -0,0 +1 @@ +document.cookie = "id=1"; diff --git a/Website/scripts/getCurrentCoins.js b/Website/scripts/getCurrentCoins.js index 589157b9..4664e088 100644 --- a/Website/scripts/getCurrentCoins.js +++ b/Website/scripts/getCurrentCoins.js @@ -1,5 +1,9 @@ +socketAdress="ws://localhost:8765" +// Lacking protocol for ids lmao +document.cookie = "id=1"; + function currencyTicker(){ - var ws = new WebSocket("ws://localhost:8765/"); + var ws = new WebSocket(socketAdress); ws.onopen = function(){ console.log("Connection is Established"); @@ -12,5 +16,20 @@ function currencyTicker(){ }; } -currencyTicker() + +function userBalance(){ + var ws = new WebSocket(socketAdress); + ws.onopen = function(){ + ws.send("STATUSONUSER"); + }; + + ws.onmessage = function(evt){ + var balanceStatus = evt.data; + // this doesnt work, fix pls + var websiteReplace = "Your balance is:${balanceStatus}" + w = document.getElementById("balance").innerHTML = websiteReplace + } + +} var t=setInterval(currencyTicker,5000); +var y=setInterval(userBalance,10000);