-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
113 lines (106 loc) · 4.36 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
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
<html>
<head>
<script src="/socket.io/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script>
var socket = io.connect("http://localhost:3000");
var username = "";
// on connection to server, ask for user's name with an anonymous callback
socket.on("connect", function() {
// call the server-side function 'adduser' and send one parameter (value of prompt)
while (username === "" || username == null)
username = prompt("Enter a username:");
room =
prompt("Enter a room name (leave blank for 'lobby'):") ||
"lobby";
socket.emit("adduser", username, room);
});
// listener, whenever the server emits 'updatechat', this updates the chat body
socket.on("updatechat", function({ username, data }) {
$("#conversation").append(
"<b>" + username + ":</b> " + data + "<br>"
);
});
// listener, whenever the server emits 'updaterooms', this updates the room the client is in
socket.on("updaterooms", function({ rooms, current_room }) {
$("#rooms").empty();
$("#rooms").append(
"<div>You are currently in:<br /><b>" +
current_room +
"</b><br /><br /></div>"
);
if (rooms.length > 1) {
$("#rooms").append(
"<div>Other available rooms:<br /></div>"
);
}
rooms.forEach(r => {
if (r != current_room) {
$("#rooms").append(
'<div><a href="#" onclick="switchRoom(\'' +
r +
"')\">" +
r +
"</a></div>"
);
}
});
});
socket.on("serverupdate", function(update) {
if (update.type === "userjoined") {
$("#conversation").append(
"<font color='#888'><b>" +
update.username +
"</b> joined the room.<br />"
);
} else if (update.type === "userleft") {
$("#conversation").append(
"<font color='#888'><b>" +
update.username +
"</b> left the room.<br />"
);
}
});
function switchRoom(room) {
socket.emit("switchroom", room);
$("#conversation").empty();
}
// on load of page
$(function() {
// when the client clicks SEND
$("#datasend").click(function() {
var message = $("#data").val();
$("#data").val("");
// tell server to execute 'sendchat' and send along one parameter
if (message !== "") socket.emit("sendchat", message);
});
// when the client hits ENTER on their keyboard
$("#data").keypress(function(e) {
if (e.which == 13) {
$("#datasend").click();
}
});
});
</script>
</head>
<body style="padding:15px">
<h2>LP Socket.IO Server Chat</h2>
<h5><i>on the internet, nobody knows you're a dog...</i></h5>
<br />
<div
style="float:left;width:200px;border-right:1px solid black;height:300px;padding:10px;overflow:scroll-y;"
>
<b>Chat Rooms</b>
<br /><br />
<div id="rooms"></div>
</div>
<div style="float:left;width:300px;height:300px;padding:20px;">
<div
id="conversation"
style="height:275px;overflow-y:scroll;"
></div>
<input id="data" style="width:225px;" />
<input type="button" id="datasend" value="send" />
</div>
</body>
</html>