-
Notifications
You must be signed in to change notification settings - Fork 0
/
example01.html
58 lines (45 loc) · 1.43 KB
/
example01.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WebSocketTest</title>
<link rel="stylesheet" href="css/styles.css">
<script src="js/vendor/socket.io.js"></script>
<script>
const url = 'http://localhost:3000';
let socket = io.connect(url);
// サーバ接続時のイベント
socket.on("connect", function () {
console.log("Connected " + url);
});
// サーバ切断時のイベント
socket.on("disconnect", function () {
console.log("Disconnected " + url);
});
// サーバからメッセージ受信時のイベント
socket.on("S_to_C_message", function (msg) {
console.log(msg.value);
});
// サーバにメッセージを送信するメソッド
function sendMessage() {
let msg = document.getElementById("msg").value;
// 空文字チェック
if (msg !== "") {
// 送信実行
socket.emit("C_to_S_message", {value: msg});
// 入力欄を初期化
document.getElementById("msg").value = "";
}
}
</script>
</head>
<body>
<div id="app">
<h1>チャット Example01</h1>
</div>
<form action="" method="post" onsubmit="return false;">
<input id="msg" type="text">
<input type="submit" value="Send" onclick="sendMessage();">
</form>
</body>
</html>