forked from webber0928/socketDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
88 lines (83 loc) · 2.94 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
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
iframe {width: 90%;height: 90vh;padding:0px 100px;}
h1 {padding: 0px 100px;}
</style>
</head>
<body>
<h1 id='tutorial'>請用手機開啟 139.162.22.47:3000/mobi 頁面</h1>
<script>
var randomValue = new String(Math.floor(Math.random() * Math.pow(36, 5)).toString(36)).toUpperCase();
document.write("<h1>在手機上輸入配對碼: "+randomValue+"</h1>")
</script>
<div id="aaa"></div>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket = io();
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 配對成功,回傳確認訊息
socket.on('ask_channel', function(msg){
if(msg==randomValue){
socket.emit('answer_channel',msg);
$('h1').text("");
$('#tutorial').text('將手機橫拿->撥放影片 ; 翻轉->切換影片');
}
});
var player;
socket.on(randomValue, function(msg){
var message = msg;
if (message.play == 'start'){
player.loadVideoById({videoId:'VcoRKfAztYk',
startSeconds:player.getCurrentTime(),
suggestedQuality:'large'});
}
else{
player.loadVideoById({videoId:'BJQb50sIuFE',
startSeconds:player.getCurrentTime(),
suggestedQuality:'large'});
}
});
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
function onYouTubeIframeAPIReady() {
console.log(player)
player = new YT.Player('aaa', {
height: '390',
width: '640',
videoId: 'UtF6Jej8yb4',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
stopVideo();
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
</script>
</body>
</html>