added getNumViewers, not implemented for twitch yet
parent
63e7cfbce8
commit
2f0aad90de
@ -0,0 +1,100 @@
|
||||
if(typeof process === 'object') {
|
||||
var WebSocket = require('isomorphic-ws');
|
||||
var EventEmitter = require('events');
|
||||
var TrovoApollo = require('./trovo.js/lib/api/TrovoApollo');
|
||||
}
|
||||
|
||||
class TrovoPubSubSocket extends EventEmitter {
|
||||
constructor(channelID, settings) {
|
||||
super();
|
||||
|
||||
this.channelID = channelID;
|
||||
|
||||
settings = settings || {};
|
||||
|
||||
this.wsUrl = settings && settings.wsUrl || 'wss://pubsub.trovo.live/sub';
|
||||
this.verbose = settings.verbose || false;
|
||||
this.reconnectDelay = settings.reconnectDelay || 2000;
|
||||
this.state = 'none';
|
||||
this.trovoApollo = new TrovoApollo();
|
||||
this.nonces = {};
|
||||
this.pingTimeout = 45000;
|
||||
}
|
||||
|
||||
async connect() {
|
||||
if(this.webSocket) {
|
||||
this.webSocket.removeAllListeners('message');
|
||||
this.state = 'none';
|
||||
this.webSocket = null;
|
||||
}
|
||||
|
||||
this.emit('verbose', 'Opening WebSocket ' + this.wsUrl);
|
||||
this.webSocket = new WebSocket(this.wsUrl);
|
||||
|
||||
this.webSocket.once('open', () => {
|
||||
this.emit('verbose', 'websocket open');
|
||||
this.trovoApollo.GetPubSubToken({}).then((token) => {
|
||||
this.send('AUTH', { token: token.getPubSubToken.token }).then((response) => {
|
||||
this.emit('open');
|
||||
this.send('LISTEN', {"topics":["emote-event-notify"]}).then((response) => { console.log('listen emote event notify', response); });
|
||||
this.send('LISTEN', {"topics":["channel-event-by-id."+this.channelID]});
|
||||
this.send('LISTEN', {"topics":["channel-change-notify-by-id."+this.channelID]});
|
||||
this.sendPing();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
this.webSocket.once('error', (err) => {
|
||||
this.emit('verbose', 'websocket error: ' + err.toString());
|
||||
});
|
||||
|
||||
this.webSocket.on('close', (err, message) => {
|
||||
this.emit('verbose', 'websocket close: ' + err + ' ' + message);
|
||||
setTimeout(() => { this.connect(); }, this.reconnectDelay);
|
||||
});
|
||||
|
||||
this.webSocket.on('message', (text) => {
|
||||
var data = JSON.parse(text);
|
||||
if(data.type == 'RESPONSE' && data.nonce) {
|
||||
if(this.nonces && this.nonces[data.nonce]) {
|
||||
this.nonces[data.nonce].resolve(data.data);
|
||||
delete this.nonces[data.nonce];
|
||||
}
|
||||
} else if(data.type == 'PONG') {
|
||||
setTimeout(() => { this.sendPing(); }, this.pingTimeout)
|
||||
}
|
||||
|
||||
this.emit(data.type, data);
|
||||
});
|
||||
}
|
||||
|
||||
sendPing() {
|
||||
this.send('PING');
|
||||
}
|
||||
|
||||
send(type, data) {
|
||||
if(this.webSocket && this.webSocket.readyState == 1) {
|
||||
var nonce = type + '_' + Date.now();
|
||||
var message = {
|
||||
type: type,
|
||||
nonce: nonce,
|
||||
data: data
|
||||
};
|
||||
var str = JSON.stringify(message);
|
||||
this.webSocket.send(str);
|
||||
this.emit('verbose', 'SEND ' + str);
|
||||
return new Promise((resolve, reject) => {
|
||||
this.nonces[nonce] = { resolve: resolve, reject: reject };
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
isConnected() {
|
||||
return this.state === 'connected';
|
||||
}
|
||||
}
|
||||
|
||||
if(typeof process === 'object') {
|
||||
module.exports = TrovoPubSubSocket;
|
||||
}
|
||||
|
Loading…
Reference in New Issue