Skip to content

Commit 70bbe81

Browse files
committed
Improvements and reductions in querying youtube
1 parent 8ac2b2a commit 70bbe81

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

app.js

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ const wss = new WebSocketServer({
3737

3838
const ytc = new YoutubeChat("./oauth2.keys.json", "tokens");
3939
var tmiIdentity;
40-
var knownIds;
40+
var knownIds = new Object;
4141
try{
4242
let rawData = FS.readFileSync('./tmi.credentials.json');
4343
tmiIdentity = JSON.parse(rawData);
4444
if(FS.existsSync('./channels.json')){
45-
let channelsRaw = FS.readFileSync('')
45+
let channelsRaw = FS.readFileSync('./channels.json')
4646
knownIds = JSON.parse(channelsRaw);
4747
}
4848
}
@@ -78,28 +78,48 @@ wss.on('connection', function connection(ws, req) {
7878
ws.uuid = uuidv4();
7979
ws.waiting = true;
8080
ws.isAlive = true;
81+
ws.livechatStart = false;
8182
ws.tmiMessage = new Object;
8283
ws.send("Connected");
8384
ws.on('pong', heartbeat);
8485
console.log(`connection received from: ${ws.ip}`)
8586
ws.on('close', async function close() {
8687
console.log(`Connection closed: ${ws.ip}`)
8788
clearInterval(interval);
88-
ws.liveChat.stop();
89-
tmiClient.part(ws.twitchChannel)
90-
tmiClient.off("message", ws.tmiMessage[ws.uuid]);
89+
if(ws.livechatStart) {
90+
ws.liveChat.stop();
91+
tmiClient.part(ws.twitchChannel)
92+
tmiClient.off("message", ws.tmiMessage[ws.uuid]);
93+
}
9194
});
9295
ws.on('message', async function message(data) {
9396
try{
9497
const params = JSON.parse(data);
9598
ws.twitchChannel = params.forward;
9699
if(params.channelName) {
97-
if(!knownIds[params.channelName]){
100+
if(typeof knownIds[params.channelName] == 'undefined'){
98101
knownIds[params.channelName] = await ytc.getChannelId(params.channelName);
99-
FS.writeSync('./channels.json', JSON.stringify(knownIds, null, 2));
102+
FS.writeFileSync('./channels.json', JSON.stringify(knownIds, null, 2));
100103
}
101104
params.id = knownIds[params.channelName]
102105
}
106+
if(params.id.length == 24 && params.id.startsWith("UC")){
107+
ws.liveChat = new LiveChat({channelId: params.id})
108+
}
109+
else{
110+
ws.liveChat = new LiveChat({liveId: params.id})
111+
}
112+
try{
113+
await ws.liveChat.start()
114+
}
115+
catch(e){
116+
console.error(e)
117+
ws.send("400");
118+
ws.close();
119+
return;
120+
}
121+
ws.livechatStart = true;
122+
ws.youtubeChatId = await ytc.getLatestChatId(params.id);
103123
if(ws.twitchChannel){
104124
ws.tmiMessage[ws.uuid] = (channel, userstate, message, self) => {
105125
if(self) return;
@@ -111,32 +131,15 @@ wss.on('connection', function connection(ws, req) {
111131
message = `*${message}*`
112132
}
113133
message = `[Twitch] ${userstate['display-name']}: ${message}`
114-
ytc.sendMessage(message, channelName)
134+
ytc.sendMessage(message, ws.youtubeChatId)
115135
}
116136
}
117137
tmiClient.join(ws.twitchChannel)
118138
tmiClient.on("message", ws.tmiMessage[ws.uuid])
119139
}
120-
if(params.id.length == 24 && params.id.startsWith("UC")){
121-
ws.liveChat = new LiveChat({channelId: params.id})
122-
}
123-
else{
124-
ws.liveChat = new LiveChat({liveId: params.id})
125-
}
126140
setTimeout(() => {
127141
ws.waiting = false;//wait 5 seconds before we actually send messages so that the old stuff isn't sent.
128142
}, 5000);
129-
let ok;
130-
try{
131-
ok = await ws.liveChat.start()
132-
}
133-
catch(e){
134-
ws.send("400");
135-
ws.close();
136-
}
137-
if (!ok) {
138-
ws.send(`400`)
139-
}
140143
ws.liveChat.on("chat", (chatItem) => {
141144
if(ws.waiting) return;
142145

@@ -151,6 +154,11 @@ wss.on('connection', function connection(ws, req) {
151154
ws.liveChat.on("error", (err) => {
152155
ws.send(JSON.stringify(err));
153156
})
157+
ws.liveChat.on("end", (reason) => {
158+
console.log(reason);
159+
ws.send("410");
160+
ws.close();
161+
})
154162
}
155163
catch(e){
156164
console.error(e)

0 commit comments

Comments
 (0)