-
Notifications
You must be signed in to change notification settings - Fork 2
/
geeky_chat.py
107 lines (69 loc) · 1.8 KB
/
geeky_chat.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import socketio
import os
import sys
from socketio.client import Client
import typer
from typer.main import Typer
app = typer.Typer()
sender_ = None
reciever_ = None
stop = False
messages = [
]
sio = socketio.Client()
def print_decoration():
decor = "❄️ "*5
s = typer.style(sender_, fg="green")
r = typer.style(reciever_, fg="red")
typer.echo(f"{decor} Chat {s} with {r} {decor}")
@sio.event
def connect():
print('connection established')
def print_messages(messages):
indent = " "*30
print_decoration()
# s = typer.stule(sender_, fg="green")
# r = typer.stule(reciever_, fg="red")
for m in messages:
if m["sent"]:
n = m["message"]
typer.secho(f"{n}{indent}", fg="green")
else:
n = m["message"]
typer.secho(f"{indent}{n}", fg="red")
@sio.event
def chat(data):
messages.append({"message": data, "sent": False})
os.system('cls||clear')
print_messages(messages)
@sio.event
def disconnect():
pass
def start_chat():
os.system('cls||clear')
print_decoration()
while True:
message = input()
messages.append({"message": message, "sent": True})
os.system('cls||clear')
print_messages(messages)
sio.emit("chat", message)
sys.sxit()
# asyncio.sleep(1)
def start_client():
start_chat()
# await start_chat()
sio.wait()
@app.command()
def start_chatting(sender: str = None, reciever: str = None):
global sender_
global reciever_
sender_ = sender
reciever_ = reciever
typer.echo(f"connecting to {reciever}")
indent = " "*20
sio.connect('http://localhost:8080',
auth={"to": reciever, "from": sender}, headers={"to": reciever, "from": sender})
start_chat()
quit()
app()