forked from twitchdev/pubsub-javascript-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
executable file
·122 lines (106 loc) · 3.59 KB
/
main.js
File metadata and controls
executable file
·122 lines (106 loc) · 3.59 KB
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
var clientId = '<YOUR CLIENT ID HERE>';
var redirectURI = '<YOUR REDIRECT URL HERE>';
var scope = 'user_read+chat_login';
var ws;
function parseFragment(hash) {
var hashMatch = function(expr) {
var match = hash.match(expr);
return match ? match[1] : null;
};
var state = hashMatch(/state=(\w+)/);
if (sessionStorage.twitchOAuthState == state)
sessionStorage.twitchOAuthToken = hashMatch(/access_token=(\w+)/);
return
};
function authUrl() {
sessionStorage.twitchOAuthState = nonce(15);
var url = 'https://api.twitch.tv/kraken/oauth2/authorize' +
'?response_type=token' +
'&client_id=' + clientId +
'&redirect_uri=' + redirectURI +
'&state=' + sessionStorage.twitchOAuthState +
'&scope=' + scope;
return url
}
// Source: https://www.thepolyglotdeveloper.com/2015/03/create-a-random-nonce-string-using-javascript/
function nonce(length) {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < length; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
function heartbeat() {
message = {
type: 'PING'
};
$('.ws-output').append('SENT: ' + JSON.stringify(message) + '\n');
ws.send(JSON.stringify(message));
}
function listen(topic) {
message = {
type: 'LISTEN',
nonce: nonce(15),
data: {
topics: [topic],
auth_token: sessionStorage.twitchOAuthToken
}
};
$('.ws-output').append('SENT: ' + JSON.stringify(message) + '\n');
ws.send(JSON.stringify(message));
}
function connect() {
var heartbeatInterval = 1000 * 60; //ms between PING's
var reconnectInterval = 1000 * 3; //ms to wait before reconnect
var heartbeatHandle;
ws = new WebSocket('wss://pubsub-edge.twitch.tv');
ws.onopen = function(event) {
$('.ws-output').append('INFO: Socket Opened\n');
heartbeat();
heartbeatHandle = setInterval(heartbeat, heartbeatInterval);
};
ws.onerror = function(error) {
$('.ws-output').append('ERR: ' + JSON.stringify(error) + '\n');
};
ws.onmessage = function(event) {
message = JSON.parse(event.data);
$('.ws-output').append('RECV: ' + JSON.stringify(message) + '\n');
if (message.type == 'RECONNECT') {
$('.ws-output').append('INFO: Reconnecting...\n');
setTimeout(connect, reconnectInterval);
}
};
ws.onclose = function() {
$('.ws-output').append('INFO: Socket Closed\n');
clearInterval(heartbeatHandle);
$('.ws-output').append('INFO: Reconnecting...\n');
setTimeout(connect, reconnectInterval);
};
}
$(function() {
if (document.location.hash.match(/access_token=(\w+)/))
parseFragment(document.location.hash);
if (sessionStorage.twitchOAuthToken) {
connect();
$('.socket').show()
$.ajax({
url: "https://api.twitch.tv/kraken/user",
method: "GET",
headers: {
"Client-ID": clientId,
"Authorization": "OAuth " + sessionStorage.twitchOAuthToken
}})
.done(function(user) {
$('#topic-label').text("Enter a topic to listen to. For example, to listen to whispers enter topic 'whispers."+user._id+"'");
});
} else {
var url = authUrl()
$('#auth-link').attr("href", url);
$('.auth').show()
}
});
$('#topic-form').submit(function() {
listen($('#topic-text').val());
event.preventDefault();
});