-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
153 lines (129 loc) · 4.12 KB
/
client.js
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const SET_QUERY_INTERVAL = "/query";
const SET_LEEWAY = "/leeway";
const DEFAULT_QUERY_INTERVAL = 2000;
var socket = io();
var loop;
var leewayTime = 3;
const CHAT_COMMANDS = [
{
text: SET_QUERY_INTERVAL,
action: function(param, sender) {
var ms = parseInt(param);
if (!isNaN(ms) && ms >= 0) {
queryServerForVideoUpdate(ms);
addSystemMessage("Server will now be queried for a video update every {0} ms.".format(ms));
}
}
},
{
text: SET_LEEWAY,
action: function(param, sender) {
var leeway = parseInt(param);
if (!isNaN(leeway) && leeway >= 0) {
leewayTime = leeway;
addSystemMessage(
"Video will now update if time difference with server is greater than {0} seconds."
.format(leewayTime));
}
}
}
];
// Called on window load I think??
$(function() {
queryServerForVideoUpdate(DEFAULT_QUERY_INTERVAL);
makeTextAreaEnterSubmitContent();
document.getElementById("button").onclick = chat;
socket.on('chat message', function(msg) {
addChatMessage(msg);
});
socket.on('system message', function(msg) {
addSystemMessage(msg);
});
socket.on('update username', function(userName, color) {
document.getElementById("username").innerHTML = userName.fontcolor(color);
});
// number of people currently in the chat
socket.on("update population", function(count) {
document.getElementById("count").innerHTML = count;
});
socket.on('set video', function(video) {
setVideo(video);
});
});
function makeTextAreaEnterSubmitContent() {
$('#m').keypress(function(event) {
if ((event.keyCode || event.which) == 13) {
event.preventDefault();
chat();
return false;
}
});
}
// send a message to server
function chat() {
var message = $('#m').val();
$('#m').val('');
// Check if user is inputting any client-side commands
for (var i = 0; i < CHAT_COMMANDS.length; i++) {
if (util.parseCommand(CHAT_COMMANDS[i], { text: message, sender: socket })) {
return false;
}
}
socket.emit('chat message', message);
return false;
}
function setVideo(video) {
if (isPlayerReady && video.time > 0) {
// Update video if not playing correct ID
if (util.getVideoID(player.getVideoUrl()) != video.videoId) {
player.loadVideoById(video);
}
var timeDifferenceWithServer = Math.abs(video.time - player.getCurrentTime());
// Don't update current time if within a leeway amount
if (timeDifferenceWithServer > leewayTime) {
player.seekTo(video.time, true);
}
}
}
// Repeatedly ask server for video update
function queryServerForVideoUpdate(intervalTime) {
if (loop) {
clearInterval(loop);
}
loop = setInterval(
function() {
socket.emit("request update");
},
intervalTime
)
}
// System messages don't have a sender
function addSystemMessage(msg) {
var system = document.createElement("i");
system.innerHTML = msg.fontcolor("grey");
addMessage(system);
}
// Chat messages have a sender
function addChatMessage(msg) {
var user = document.createElement("b");
user.innerHTML = msg.author.fontcolor(msg.color);
var text = document.createTextNode(": " + msg.message);
addMessage(user, text);
}
// Adds message to container and handles autoscrolling
function addMessage() {
var container = $('#container');
// Only scroll down IF scrollbar is already at bottom
var shouldScrollDown =
(container[0].scrollTop + container[0].clientHeight
== container[0].scrollHeight);
var div = document.createElement('div');
div.className = "message";
for (var i = 0; i < arguments.length; i++) {
div.appendChild(arguments[i]);
}
container.append(div);
if (shouldScrollDown) {
container[0].scrollTop = container[0].scrollHeight;
}
}