-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_client.html
More file actions
185 lines (148 loc) · 5.32 KB
/
chat_client.html
File metadata and controls
185 lines (148 loc) · 5.32 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>BinaryRPC Chat</title>
<style>
body {
font-family: sans-serif;
padding: 20px;
max-width: 600px;
margin: auto;
}
#chat {
border: 1px solid #ccc;
padding: 10px;
height: 300px;
overflow-y: scroll;
margin-bottom: 10px;
}
#controls input,
#controls button {
margin: 5px 0;
width: 100%;
padding: 8px;
}
#messageInput {
width: calc(100% - 60px);
display: inline-block;
}
#sendBtn {
width: 50px;
display: inline-block;
}
</style>
</head>
<body>
<h2>BinaryRPC Chat</h2>
<div id="login">
<input id="username" type="text" placeholder="Username">
<input id="room" type="text" placeholder="Room Name">
<button id="joinBtn">Join</button>
</div>
<div id="chatUI" style="display: none;">
<div id="chat"></div>
<div id="controls">
<input id="messageInput" type="text" placeholder="Enter message...">
<button id="sendBtn">Send</button>
<button id="leaveBtn">Leave</button>
<button id="disconnectBtn">Disconnect</button>
</div>
</div>
<script type="module">
import { encode, decode } from 'https://cdn.jsdelivr.net/npm/@msgpack/msgpack@3.1.1/+esm';
window.MessagePack = { encode, decode };
</script>
<script src="binaryrpc.js"></script>
<script>
let rpc;
let username;
let room;
const chatDiv = document.getElementById("chat");
const chatUI = document.getElementById("chatUI");
const LS_KEY = "sessionToken";
const CL_KEY = "clientId";
const DV_KEY = "deviceId";
const clientId = localStorage.getItem(CL_KEY) || crypto.randomUUID();
const deviceId = localStorage.getItem(DV_KEY) || Math.floor(Math.random() * 100000);
const storedToken = localStorage.getItem(LS_KEY);
localStorage.setItem(CL_KEY, clientId);
localStorage.setItem(DV_KEY, deviceId);
function appendMessage(msg) {
const p = document.createElement("p");
p.textContent = msg;
chatDiv.appendChild(p);
chatDiv.scrollTop = chatDiv.scrollHeight;
}
document.getElementById("joinBtn").onclick = async () => {
username = document.getElementById("username").value.trim();
room = document.getElementById("room").value.trim();
if (!username || !room) {
alert("Please enter both username and room name.");
return;
}
rpc = new BinaryRPC("ws://localhost:5555", {
onConnect: async () => {
try {
rpc.call("join", { username, "roomname": room });
rpc.call("get_token");
} catch (e) {
alert("Join failed: " + e.message);
}
},
onError: (e) => {
appendMessage("❌ Connection error.");
}
});
rpc.registerHandler("message", (data) => {
console.log(data);
if (data.code == "JOIN") {
appendMessage(`🔵 Joined room: ${room}`);
chatUI.style.display = "block";
document.getElementById("login").style.display = "none";
} else {
appendMessage(`💬 ${data.username}: ${data.message}`);
}
});
rpc.registerHandler("get_token", (data) => {
localStorage.setItem(LS_KEY, data.token);
})
rpc.connect(clientId, deviceId, storedToken);
};
document.getElementById("sendBtn").onclick = async () => {
const input = document.getElementById("messageInput");
const text = input.value.trim();
if (!text) return;
try {
await rpc.call("say", { message: text });
input.value = "";
} catch (e) {
appendMessage("❌ Failed to send message.");
}
};
document.getElementById("leaveBtn").onclick = async () => {
try {
await rpc.call("leave", { username, room });
appendMessage("🔴 You left the room.");
} catch (e) {
appendMessage("⚠️ Leave failed.");
}
rpc.disconnect();
chatUI.style.display = "none";
document.getElementById("login").style.display = "block";
};
document.getElementById("disconnectBtn").onclick = () => {
if (rpc) {
rpc.disconnect();
rpc = null;
}
chatDiv.innerHTML = "";
document.getElementById("messageInput").value = "";
document.getElementById("username").value = "";
document.getElementById("room").value = "";
chatUI.style.display = "none";
document.getElementById("login").style.display = "block";
};
</script>
</body>
</html>