-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo.js
80 lines (72 loc) · 1.77 KB
/
video.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
var imageElement = document.getElementById('image');
// var rect;
var top_p = 0;
var left_p = 0;
var win_wid = window.innerWidth;
var win_hei = window.innerHeight;
function inform_server(top_p, left_p) {
socket.send(JSON.stringify({ top: top_p, left: left_p }));
}
var socket = new WebSocket("ws://" + url);
// register connect event
socket.onopen = function () {
console.log("Connected to server");
imageElement.style.top = (top_p - row * win_hei) + 'px';
imageElement.style.left = (left_p - col * win_wid) + 'px';
if (row != 0 || col != 0) {
imageElement.muted = true;
}
};
// Log errors
socket.onerror = function (error) {
console.log('Websocket error ' + error);
}
// Log messages from server
var status;
var deadlock;
socket.onmessage = function (message) {
status = message.data;
h = JSON.parse(status);
if (h.videoEve == true) {
deadlock = true;
if (h.play == true) {
imageElement.play();
imageElement.currentTime = h.time;
console.log("playing");
}
else if (h.pause == true) {
imageElement.pause();
imageElement.currentTime = h.time;
console.log("paused");
}
else if (h.seek == true) {
imageElement.currentTime = h.time;
console.log("seeking");
}
}
}
// video controls
imageElement.onplay = function (e) {
if (deadlock == true) {
deadlock = false;
}
else {
socket.send(JSON.stringify({ videoEve: true, play: true, time: imageElement.currentTime }));
}
}
imageElement.onpause = function () {
if (deadlock == true) {
deadlock = false;
}
else {
socket.send(JSON.stringify({ videoEve: true, pause: true, time: imageElement.currentTime }));
}
}
imageElement.onseeking = function () {
if (deadlock == true) {
deadlock = false;
}
else {
socket.send(JSON.stringify({ videoEve: true, seek: true, time: imageElement.currentTime }));
}
}