-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws_client_bry.py
37 lines (25 loc) · 896 Bytes
/
ws_client_bry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from browser import document
from browser.websocket import WebSocket
from browser import console
# print ("brython")
socket = WebSocket("ws://localhost:1234")
def onopen(e):
console.log("[open] Connection established")
console.log("Sending to server")
socket.send("My name is danx0r")
socket.onopen = onopen
def onmessage(e):
console.log(f"[message] Data received from server: {e.data}")
document["message"].innerHTML = e.data
socket.onmessage = onmessage
def onclose(e):
if (e.wasClean):
console.log(f"[close] Connection closed cleanly, code={e.code} reason={e.reason}")
else:
# // e.g. server process killed or network down
# // event.code is usually 1006 in this case
console.log('[close] Connection died')
socket.onclose = onclose
def onerror(error):
console.log(f"[error] {error.message}")
socket.onerror = onerror