/
app.js
97 lines (77 loc) · 2.26 KB
/
app.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
sp = getSpotifyApi(1);
var models = sp.require('sp://import/scripts/api/models');
var player = models.player;
var currentArtistURI;
exports.init = init;
function init() {
setSessionEventListener(new SessionEventListener());
updatePageWithTrackDetails();
player.observe(models.EVENT.CHANGE, function (e) {
// Only update the page if the track changed
if (e.data.curtrack == true) {
updatePageWithTrackDetails();
}
});
}
function updatePageWithTrackDetails() {
var header = document.getElementById('artist');
var isPlayingTrack = player.track != null;
if (isPlayingTrack) {
var track = getTrack();
var artist = track.artists[0];
if (currentArtistURI != artist.uri){
currentArtistURI = artist.uri;
joinRoomForArtistURI(artist.uri);
}
header.innerHTML = "People listening to " + artist.name;
} else {
header.innerText = "Start playing to see other people liste ning to the same artist as you!";
}
}
function getTrack() {
return player.track.data;
}
var currentArtistURI;
function joinRoomForArtistURI(artistURI){
debug("joining new room");
var sessionResolver = new AsyncSessionResolver(sessionResolverHandler);
sessionResolver.sessionIDAndTokenWithArtistURI(artistURI);
}
function sessionResolverHandler(sessionID, token, artistURI){
if (currentArtistURI === artistURI) {
doJoinRoom(sessionID, token);
}
}
function doJoinRoom(sessionID, token) {
leaveCurrentRoom();
joinANewRoom(sessionID, token);
}
function leaveCurrentRoom() {
disconnectCurrentSession();
}
function joinANewRoom(sessionID, token) {
connectWithSessionAndToken(sessionID, token);
}
function didJoinANewRoom(session) {
startChat(session.sessionId,'Haxor');
startTrackingTracks(session.sessionId, session.connection.connectionId);
}
function didLeaveRoom(session) {
endTrackingTracks(session.sessionId, session.connection.connectionId);
}
function TrackServiceHandler() {
this.didGetTrack = function (responseText) {
console.log("didGetTrack with response: " + responseText);
}
this.didPutTrack = function (responseText) {
console.log("didPutTrack with response: " + responseText);
}
}
function SessionEventListener() {
this.didStartSession = function (session) {
didJoinANewRoom(session);
}
this.didEndSession = function (session) {
didLeaveRoom(session);
}
}