-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws_chatroom_sample.html
119 lines (111 loc) · 4.43 KB
/
ws_chatroom_sample.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Gin Hello</title>
<style>
#chat-room {
border: 1px solid;
border-radius: 12px;
width: 720px;
height: 240px;
overflow-y: auto;
padding: 12px;
margin: 8px 0;
}
</style>
</head>
<body>
<label for="accountUUID">AccountUUID:</label>
<input type="text" id="accountUUID" name="accountUUID">
<button id="connect-btn">Connect</button>
<br />
<div style="margin: 16px 0"></div>
<label for="receiver">Receiver:</label>
<input type="text" id="receiver" name="receiver">
<div id="chat-room"></div>
<label for="message">Message:</label>
<input type="text" id="message" name="message">
<button id="send-message-btn">Submit</button>
<button id="get-caht-room-btn-info">Get Chat Room Info</button>
<button id="get-caht-message-btn">Get Chat Room Message</button>
<script>
var ws
function connect() {
const accountUUID = document.getElementById('accountUUID').value
ws = new WebSocket(`ws://localhost:8080/api/chat/ws?accountUuid=${accountUUID}`);
// onOpen被觸發時, 去嘗試連線
ws.onopen = function(evt) {
console.log("Connection open ...");
ws.send(JSON.stringify({
seq: Date.now(),
cmd: "ping"
}));
};
// onMessage被觸發時, 來接收ws server傳來的訊息
ws.onmessage = function(evt) {
const data = JSON.parse(evt.data)
const element = document.createElement("div")
const chatContainerEle = document.getElementById('chat-room')
if (data.cmd == "chat_message") {
element.textContent = `User ${data.payload.from}: ${data.payload.text}`
element.style.textAlign = "left"
element.style.padding = "6px"
chatContainerEle.appendChild(element)
}
};
// 由ws server發出的onClose事件
ws.onclose = function(evt) {
console.log("Connection closed.");
};
// var heartbeatTimeInterval = setInterval(() => ws.send(JSON.stringify({
// seq: Date.now(),
// cmd: "ping"
// })), 100)
}
const connBtnEle = document.getElementById('connect-btn');
const sendMsgBtnEle = document.getElementById('send-message-btn');
const getChatRoomInfoBtn = document.getElementById('get-caht-room-btn-info');
const getChatMessageBtn = document.getElementById('get-caht-message-btn');
getChatMessageBtn.addEventListener("click", () => {
ws.send(JSON.stringify({
seq: Date.now(),
cmd: "get_chat_message",
payload: {
chatRoomId: parseInt(document.getElementById('receiver').value)
}
}))
})
getChatRoomInfoBtn.addEventListener("click", () => {
ws.send(JSON.stringify({
seq: Date.now(),
cmd: "get_all_chat_room",
payload: {}
}))
})
connBtnEle.addEventListener("click", connect);
sendMsgBtnEle.addEventListener("click", () => {
const message = document.getElementById('message').value
const chatContainerEle = document.getElementById('chat-room')
const element = document.createElement("div")
ws.send(JSON.stringify({
seq: Date.now(),
cmd: "send_chat_message",
payload: {
from: document.getElementById('accountUUID').value,
to: parseInt(document.getElementById('receiver').value),
type: "text",
content: message,
}
}));
element.textContent = `You: ${message}`
element.style.textAlign = "right"
element.style.padding = "6px"
chatContainerEle.appendChild(element)
});
</script>
</body>
</html>