-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
51 lines (48 loc) · 1.55 KB
/
index.html
File metadata and controls
51 lines (48 loc) · 1.55 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
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf8">
<style>
#logDiv {
overflow: scroll;
resize: vertical;
height: 400px;
border: solid 1px;
padding: 4;
border-radius: 4px;
}
</style>
</head>
<body>
<form id="chatForm">
<input name="name" size="8" type="text" value="匿名">
<input name="message" size="30" type="text" placeholder="送信するメッセージ。enterで送信。">
<button type="submit">送信</button>
</form>
<div id="logDiv">
</div>
<script src="https://fungo.kcg.edu/madoi-20231023/js/madoi.js"></script>
<script>
window.addEventListener("load", ()=>{
// Madoiを作成しサーバに接続する。引数はルームのID。アプリケーション毎に異なるIDを使用する。
const m = new madoi.Madoi("chat-ldfngslkkeg");
const chatForm = document.getElementById("chatForm");
const logDiv = document.getElementById("logDiv");
chatForm.addEventListener("submit", e=>{
// フォームのsubmit時の処理
e.preventDefault();
const name = chatForm.name;
const message = chatForm.message;
// メッセージを送る。第一引数はメッセージタイプ、第二引数はメッセージの内容。
m.send("chat", `${name.value}: ${message.value}`);
message.value = "";
});
// "chat"タイプのメッセージを受信した際の処理を登録する。
m.setHandler("chat", message=>{
logDiv.innerHTML += message + "<br>";
logDiv.scrollTop = logDiv.scrollHeight;
});
});
</script>
</body>
</html>