-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
32 lines (30 loc) · 1.07 KB
/
index.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
<textarea id="txtShow" disabled></textarea>
<input id="txtInput" type="text">
<button id="btnSend">送出</button>
<script>
document.addEventListener("DOMContentLoaded", event => {
let keyinDom = document.querySelector('#txtInput')
let showDom = document.querySelector('#txtShow')
document.querySelector("#btnSend").addEventListener('click', () => {
let txt = keyinDom.value;
ws.send(txt);
})
// 建立 WebSocket (本例 server 端於本地運行)
let url = 'ws://localhost:3000'
var ws = new WebSocket(url)
// 監聽連線狀態
ws.onopen = () => {
console.log('open connection')
}
ws.onclose = () => {
console.log('close connection');
}
//接收 Server 發送的訊息
ws.onmessage = event => {
let txt = event.data
if (!showDom.value) showDom.value = txt
else showDom.value = showDom.value + "\n" + txt
keyinDom.value = ""
}
});
</script>