Skip to content

Commit

Permalink
Reconnect to websocket when claimid changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxfoo committed May 1, 2021
1 parent 6bc034d commit fd11fcc
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions extension/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = function (nodecg) {
const defaultTrigger = nodecg.Replicant('defaultTrigger');
const triggers = nodecg.Replicant('triggers');
const test = nodecg.Replicant('test');
const socket = new WebSocket('wss://comments.lbry.com/api/v2/live-chat/subscribe?subscription_id=' + claim_id.value);
let socket = new WebSocket('wss://comments.lbry.com/api/v2/live-chat/subscribe?subscription_id=' + claim_id.value);
const equals = [];

function activateAlert(alertname, username, amount) {
Expand Down Expand Up @@ -72,33 +72,38 @@ module.exports = function (nodecg) {
}
});

claim_id.on('change', value => {
if (socket.readyState == WebSocket.OPEN) {
socket.close();
socket = new WebSocket('wss://comments.lbry.com/api/v2/live-chat/subscribe?subscription_id=' + claim_id.value);
}
// Connection opened
// Alojz helped with websockets code
socket.addEventListener('open', function (event) {
socket.send('Hello LBRY!');
});
// Listen for messages
socket.addEventListener('message', function (event) {
var comment=JSON.parse(event.data);
console.log(comment.data.comment.comment);
// If comment has support
if(comment.data.comment.support_amount>0) {
console.log("Has tip");
var userName = comment.data.comment.channel_name;
var alertName = defaultTrigger.value;
var amount = comment.data.comment.support_amount

addToTicker(userName, amount);
alertName = checkTriggers(amount, alertName);
activateAlert(alertName, userName, amount);
}
});
});

test.on('change', value => {
var alertName = defaultTrigger.value;
alertName = checkTriggers(value.amount, alertName);
activateAlert(alertName, "Slyver Testallone", value.amount);
});

// Connection opened
// Alojz helped with websockets code
socket.addEventListener('open', function (event) {
socket.send('Hello LBRY!');
});

// Listen for messages
socket.addEventListener('message', function (event) {
var comment=JSON.parse(event.data);
console.log(comment.data.comment.comment);
// If comment has support
if(comment.data.comment.support_amount>0) {
console.log("Has tip");
var userName = comment.data.comment.channel_name;
var alertName = defaultTrigger.value;
var amount = comment.data.comment.support_amount

addToTicker(userName, amount);
alertName = checkTriggers(amount, alertName);
activateAlert(alertName, userName, amount);
}
});

};

0 comments on commit fd11fcc

Please sign in to comment.