-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.html
191 lines (175 loc) · 6.21 KB
/
test.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<!DOCTYPE html>
<html>
<head>
<title>foo_catnap test</title>
<!--<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>-->
<!--<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>-->
<!--<script src="test.js"></script>-->
<style type="text/css">
#playlists {
}
#playlists li {
background-color: #fff;
color: #000;
}
#playlists li:nth-child(2) {
background-color: #eee;
}
</style>
</head>
<body>
<ol>
<li>Sending request... <span id="send-status"></span></li>
<li>Waiting for response... <span id="receive-status"></span><pre id="receive-message"></pre></li>
<li>Parsing response... <span id="parse-status"></span><pre id="parse-message"></pre></li>
</ol>
<p>Playlists</p>
<ol id="playlists">
</ol>
<p>Playback</p>
<p id="playback-state">
Connecting to server...
</p>
<p id="playback-info">
</p>
<p>Events</p>
<ol id="events">
</ol>
<script type="text/javascript">
(function() {
function $(id) {
return document.getElementById(id);
}
function log(message) {
if (console && console.log) {
console.log(message);
}
}
var url = 'http://localhost:20402/playlists';
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState >= 2) {
log('readyState=' + httpRequest.readyState + ", status=" + httpRequest.status);
} else {
log('readyState=' + httpRequest.readyState);
}
if (httpRequest.readyState == 4) {
if (httpRequest.status == 0) {
$('receive-status').textContent = 'failed';
$('parse-status').textContent = 'skipped';
} else if (httpRequest.status == 200) {
$('receive-status').textContent = 'passed';
$('receive-message').textContent = httpRequest.responseText;
var data = JSON.parse(httpRequest.responseText);
$('parse-status').textContent = 'passed';
$('parse-message').textContent = JSON.stringify(data);
var playlists = data.playlists;
var parent = document.getElementById('playlists');
for (n = 0; n < playlists.length; ++n) {
var child = document.createElement('li');
var playlist = playlists[n];
child.textContent = playlist.name + (playlist.active ? ' [active]' : '') + (playlist.playing ? ' [playing]' : '');
parent.appendChild(child);
}
}
}
};
httpRequest.open('GET', url, true);
httpRequest.setRequestHeader('ContentType', 'application/json; charset=utf-8');
httpRequest.setRequestHeader('Accept', 'application/json; charset=utf-8');
httpRequest.send();
$('send-status').textContent = 'passed';
if (true && typeof(EventSource) !== 'undefined') {
var eventSource = new EventSource('http://localhost:20402/playlists/events');
var eventCount = 0;
eventSource.onopen = function(event) {
var item = document.createElement('li');
item.innerHTML = '[open]';
$('events').appendChild(item);
eventCount++;
if (eventCount > 10) {
eventSource.close();
}
}
eventSource.onmessage = function(event) {
var item = document.createElement('li');
item.innerHTML = '[message] ' + event.type + ':' + event.data;
$('events').appendChild(item);
eventCount++;
if (eventCount > 10) {
eventSource.close();
}
};
eventSource.addEventListener('test', function(event) {
var item = document.createElement('li');
item.innerHTML = '[test] type = ' + JSON.stringify(event.type) + ', data = ' + JSON.stringify(event.data) + ', lastEventId = ' + event.lastEventId;
$('events').appendChild(item);
eventCount++;
if (eventCount > 10) {
eventSource.close();
}
});
eventSource.onerror = function(event) {
var item = document.createElement('li');
var readyState = eventSource.readyState;
item.innerHTML = '[error] ' + ((readyState == 0) ? 'CONNECTING' : ((readyState == 1) ? 'OPEN' : ((readyState == 2) ? 'CLOSED' : readyState)));
$('events').appendChild(item);
eventCount++;
if (eventCount > 10) {
eventSource.close();
}
}
}
//$('#responseParagraph').append("<p>Requesting data...</p>");
//$.getJSON('http://localhost:20402/',
// function(data) {
// $('#responseParagraph').append("<p>"+data.responseMessage+"</p>");
// });
})();
(function() {
function $(id) {
return document.getElementById(id);
}
function log(message) {
if (console && console.log) {
console.log(message);
}
}
if (true && typeof(EventSource) !== 'undefined') {
var eventSource = new EventSource('http://localhost:20402/playback/events');
eventSource.onopen = function(event) {
log('onopen');
$('playback-state').textContent = 'Waiting for server...';
}
eventSource.onmessage = function(event) {
log('onmessage');
$('playback-state').textContent = ''+event.data;
$('playback-info').innerHTML = '';
var data = JSON.parse(event.data);
var span = document.createElement('span');
if (data.track != null) {
var artists = data.track.artist;
for (var index = 0; index < artists.length; ++index) {
var artist = artists[index];
if (index > 0) {
var fillerSpan = document.createElement('span');
fillerSpan.textContent = ', ';
span.appendChild(fillerSpan);
}
var artistSpan = document.createElement('a');
artistSpan.href = "";
artistSpan.textContent = artist;
span.appendChild(artistSpan);
}
}
$('playback-info').appendChild(span);
}
eventSource.onerror = function(event) {
log('onerror');
$('playback-state').textContent = 'Connecting to server...';
}
}
})();
</script>
</body>
</html>