-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsse-server.py
executable file
·106 lines (77 loc) · 2.34 KB
/
sse-server.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
from flask import Flask, Response, render_template, request
from mongoengine import *
import uuid
from time import sleep
import datetime
app = Flask(__name__)
app.secret_key = 'any random string'
connect('tanishly')
# MODELS
class User(Document):
id = uuid.uuid4().hex
username = StringField()
password = StringField()
contacts = ListField(StringField())
# def __init__(self, username, password):
# self.username = username
# self.password = password
def append_friend(self,p):
self.contacts.append(str(p.id))
class Call(Document):
caller = ReferenceField(User)
callee = ReferenceField(User)
class StatusLog(Document):
user = ReferenceField(User)
sdp = StringField()
status = StringField(choices=['connect', 'disconnect'])
time = DateTimeField(default=datetime.datetime.utcnow)
@app.route("/home")
def home():
u = User("mohsen1","pass1")
u2 = User("mohsen2", "pass2")
u2.save()
u.append_friend(u2)
u.save()
return render_template("index.html")
@app.route("/signup", methods=['POST'])
def signup():
try:
u = User(request.form['username'],
request.form['password'])
u.save()
except e:
return str(e)
return "OK"
@app.route("/login", methods=['POST'])
def login():
u = User.objects(username=request.form['username'],
password=request.form['password']).first()
if u:
session['username'] = u.username
return "OK"
return "FALSE"
@app.route("/online")
def online_me():
u = User.objects(username=session['username']).first()
@app.route("/stream")
def stream_sse():
def eventStream():
while True:
sleep(2)
yield "data: {}\n\n".format("hello")
# Poll data from the database
# and see if there's a new message
messages="aaa"
previous_messages = "a"
if len(messages) > len(previous_messages):
yield "data: {}\n\n".format(messages[len(messages)-1])
return Response(eventStream(), mimetype="text/event-stream")
@app.route("/upload", methods=['POST'])
def xhr_server():
print(request.data)
return "ok"
@app.route("/p2p")
def p2p():
return render_template("indexp2p.html")
if __name__=="__main__":
app.run(debug=True, host="0.0.0.0", port=80)