USERNAMES
This commit is contained in:
parent
364d155f9d
commit
8fe6fa4884
@ -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
15
main.py
@ -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__':
|
||||
|
7
model.py
7
model.py
@ -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
|
||||
|
||||
|
@ -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');
|
||||
|
Loading…
Reference in New Issue
Block a user