Roderic Day 5 anni fa
parent
commit
eaca1c85b7
3 ha cambiato i file con 41 aggiunte e 1 eliminazioni
  1. +11
    -0
      pico.js
  2. +5
    -1
      pico.py
  3. +25
    -0
      test.py

+ 11
- 0
pico.js Vedi File

@@ -3,6 +3,12 @@ const State = {
posts: [],
users: [],
}

/*
*
* GUI
*
*/
const autoFocus = (vnode) => {
vnode.dom.focus()
}
@@ -68,6 +74,11 @@ const Main = {
}
m.mount(document.body, Main)

/*
*
* WEBSOCKETS
*
*/
const connect = (username) => {
const wsUrl = location.href.replace('http', 'ws')


+ 5
- 1
pico.py Vedi File

@@ -93,7 +93,11 @@ async def core(ws, path, server_name):
text = data['text']
if not text:
continue
await broadcast(kind='post', source=ws.username, text=text)
elif 'target' in data:
targets = {ss for ss in sockets if ss.username in {ws.username, data['target']}}
await send_json_many(ts=ts, targets=targets, kind='post', source=ws.username, text=text)
else:
await broadcast(kind='post', source=ws.username, text=text)

elif data['action'] == 'logout':
sockets.discard(ws)

+ 25
- 0
test.py Vedi File

@@ -158,6 +158,31 @@ def test_rooms():
return _test(client1, client2)


def test_private_message():
client1 = _make_client('ws://localhost:8642/', 0.10, Script()
+ {'action': 'login', 'username': 'Norman'}
- {'kind': 'update', 'users': ['Norman'], 'info': 'Welcome to /', 'username': 'Norman'}
- {'kind': 'update', 'users': ['Norman', 'Ray'], 'info': 'Ray joined'}
- {'kind': 'update', 'users': ['Emma', 'Norman', 'Ray'], 'info': 'Emma joined'}
+ {'action': 'post', 'target': 'Emma', 'text': 'なに?'}
- {'kind': 'post', 'source': 'Norman', 'text': 'なに?'}
- {'kind': 'update', 'users': ['Norman', 'Ray'], 'info': 'Emma left'}
)
client2 = _make_client('ws://localhost:8642/', 0.11, Script()
+ {'action': 'login', 'username': 'Ray'}
- {'kind': 'update', 'users': ['Norman', 'Ray'], 'info': 'Welcome to /', 'username': 'Ray'}
- {'kind': 'update', 'users': ['Emma', 'Norman', 'Ray'], 'info': 'Emma joined'}
- {'kind': 'update', 'users': ['Norman', 'Ray'], 'info': 'Emma left'}
- {'kind': 'update', 'users': ['Ray'], 'info': 'Norman left'}
)
client3 = _make_client('ws://localhost:8642/', 0.12, Script()
+ {'action': 'login', 'username': 'Emma'}
- {'kind': 'update', 'users': ['Emma', 'Norman', 'Ray'], 'info': 'Welcome to /', 'username': 'Emma'}
- {'kind': 'post', 'source': 'Norman', 'text': 'なに?'}
)
return _test(client1, client2, client3)


if __name__ == '__main__':
loop = asyncio.get_event_loop()
for fn_name, fn in list(locals().items()):

Loading…
Annulla
Salva