-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
83 lines (70 loc) · 2.55 KB
/
app.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
import uvicorn
from starlette.applications import Starlette
from starlette.endpoints import HTTPEndpoint
from starlette.responses import HTMLResponse
from nejma.ext.starlette import WebSocketEndpoint
app = Starlette()
html = """
<!DOCTYPE html>
<html>
<head>
<title>ws</title>
</head>
<body>
<h1>Nejma ⭐ Chat</h1>
<form action="" onsubmit="sendMessage(event)">
<label>username : </label><input type="text" id="username" autocomplete="off"/><br/>
<label>room id : </label><input type="text" id="roomId" autocomplete="off"/><br/>
<label>message : </label><input type="text" id="messageText" autocomplete="off"/><br/>
<button>Send</button>
</form>
<ul id='messages'>
</ul>
<script>
var ws = new WebSocket("ws://localhost:8000/ws");
ws.onmessage = function(event) {
var messages = document.getElementById('messages');
var message = document.createElement('li');
var data = JSON.parse(event.data);
message.innerHTML = `<strong>${data.username} :</strong> ${data.message}`;
messages.appendChild(message);
};
function sendMessage(event) {
var username = document.getElementById("username");
var room = document.getElementById("roomId");
var input = document.getElementById("messageText");
var data = {
"room_id": room.value,
"username": username.value,
"message": input.value,
};
ws.send(JSON.stringify(data));
input.value = '';
event.preventDefault();
}
</script>
</body>
</html>
"""
@app.route("/")
class Homepage(HTTPEndpoint):
async def get(self, request):
return HTMLResponse(html)
@app.websocket_route("/ws")
class Chat(WebSocketEndpoint):
encoding = "json"
async def on_receive(self, websocket, data):
room_id = data['room_id']
message = data['message']
username = data['username']
if message.strip():
group = f"group_{room_id}"
self.channel_layer.add(group, self.channel)
payload = {
"username": username,
"message": message,
"room_id": room_id
}
await self.channel_layer.group_send(group, payload)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)