USERNAMES

This commit is contained in:
Dominic Zimmer 2020-04-17 02:48:40 +02:00
parent 364d155f9d
commit 8fe6fa4884
5 changed files with 22 additions and 11 deletions

View File

@ -20,9 +20,9 @@ this is the base site
<script>
document.getElementById("btn_dunno").onclick = async function (e) {
let text = document.getElementById("some_input").value;
let data = {"sessionid": text};
let data = {"username": text};
await fetch('api/join_session', {
await fetch('/newclient', {
method: 'POST',
body: JSON.stringify(data),
})

15
main.py
View File

@ -95,12 +95,19 @@ async def handler(request):
return aiohttp.web.FileResponse('index.html')
@routes.get('/newclient')
@routes.post('/newclient')
async def handler(request):
model = request.app['model']
client = model.create_client()
raise aiohttp.web.HTTPFound(f"/{client}/")
data = await request.json()
username = data.get("username")
print(f"{username=}")
if username:
client = model.create_client(username)
print(f"werked?")
raise aiohttp.web.HTTPFound(f"/{client}/")
print(f"werked?")
else:
raise aiohttp.web.HTTPFound(f"/newclient")
if __name__ == '__main__':

View File

@ -77,7 +77,8 @@ class Model(object):
if "session" in self.model["clients"][clientid]:
session = self.model["clients"][clientid]["session"]
allsessions = [ name for name in self.model["sessions"] ]
data = {"currentsession": session, "allsessions": allsessions}
username = self.model["clients"][clientid]["name"] if "name" in self.model["clients"][clientid] else "Joe"
data = {"currentsession": session, "allsessions": allsessions, "username": username}
for socket in self.sockets[clientid]:
await socket.send_json(data)
@ -96,9 +97,9 @@ class Model(object):
def exists_client(self, clientid: str) -> bool:
return clientid in self.model["clients"]
def create_client(self) -> str:
def create_client(self, name="Joe") -> str:
clientname = generate_random_id()
newclient = {"id": clientname}
newclient = {"id": clientname, "name": name}
self.model["clients"][clientname] = newclient
return clientname

View File

@ -10,6 +10,9 @@ ws.onmessage = function(event) {
if (msg.hasOwnProperty('currentsession')) {
document.getElementById('current_session').innerText = msg.currentsession;
}
if (msg.hasOwnProperty('username')) {
document.getElementById('greeting').innerText = "Hello, " + msg.username + "!";
}
if (msg.hasOwnProperty('allsessions')) {
const all_sessions = document.getElementById('all_sessions');
@ -25,4 +28,4 @@ ws.onmessage = function(event) {
all_sessions.appendChild(button);
}
}
};
};

View File

@ -7,7 +7,7 @@
</head>
<body>
this is the UI
<span id="greeting"></span><br>
<br>
<span id="current_session">You are not in any session.</span><br>
<div id="all_sessions"></div><br>