Skip to content

Commit

Permalink
Minor tweaks to try and maintain better connections
Browse files Browse the repository at this point in the history
  • Loading branch information
helloitsdani committed Feb 20, 2022
1 parent fa6e22c commit 5915b3d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 32 deletions.
7 changes: 3 additions & 4 deletions src/dashboard/scripts/verify-login.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/* global nodecg, NodeCG */

(async () => {
;(async () => {
const loginURL = '/login/twitch'
const verifyURL = '/login/twitch/verify'

const { timeBetweenUpdates = 60000, requireLogin = true } = nodecg.bundleConfig

const loggedInStatus = NodeCG.Replicant('login.status', 'nodecg-twitchie')

await NodeCG.waitForReplicants(loggedInStatus)
Expand All @@ -18,7 +17,7 @@
// trigger nodecg's "login" express event to be emitted
// manually hitting this endpoint should ensure this happens
const verifyLogin = async () => {
const response = await fetch(verifyURL, { method: 'GET', credentials: 'include' })
const response = await fetch(verifyURL)

if (!response.ok) {
throw new Error('Login endpoint returned error', response.status)
Expand All @@ -40,7 +39,7 @@

pollForLoginStatus()

loggedInStatus.on('change', isLoggedIn => {
loggedInStatus.on('change', (isLoggedIn) => {
if (requireLogin && !isLoggedIn) {
redirectToLogin()
}
Expand Down
11 changes: 3 additions & 8 deletions src/extension/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TwitchieClientWrapper {

public client?: ChatClient

public isConnected = () => !!this.client
public isConnected = () => this.client && (this.client.isConnected || this.client.isConnecting)

public disconnect = async () => {
if (!this.client) {
Expand All @@ -26,7 +26,7 @@ class TwitchieClientWrapper {
try {
await this.client.quit()
} catch (e) {
// Oh Well
context.log.error('Could not tear down twitch chat client!', e)
}

this.client = undefined
Expand All @@ -42,19 +42,14 @@ class TwitchieClientWrapper {
const authProvider = new StaticAuthProvider(context.config.clientID, auth.token)

this.api = new ApiClient({ authProvider })

this.client = new ChatClient({
authProvider,
channels: () => {
const currentChannel = context.replicants.channel.id.value || auth.username
return currentChannel ? [currentChannel] : []
},
webSocket: true,
})

try {
bindChatHandlers(this.client)
await this.client.connect()

context.replicants.channel.id.value = auth.username
} catch (error) {
this.api = undefined
Expand Down
9 changes: 7 additions & 2 deletions src/extension/modules/chat/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@ context.replicants.channel.id.on('change', (_, oldChannel) => {
}

try {
context.log.debug(`Parting ${oldChannel}...`)
context.twitch.client.part(oldChannel)
context.log.debug('Parted successfully.')
} catch (e) {
// Oh No!!
console.log(e)
context.log.error('Failed to part old channel', e)
}
})

// only try and join a channel when we're sure the provided
// channel id actually resolves to a real user
// trying to join nonexistent channels on twitch irc can
// cause issues
context.replicants.user.info.on('change', async newUserInfo => {
context.replicants.user.info.on('change', async (newUserInfo) => {
if (!newUserInfo) {
return
}
Expand All @@ -33,6 +35,9 @@ context.replicants.user.info.on('change', async newUserInfo => {
return
}

context.log.debug(`Joining #${newUserInfo.login}...`)
await context.twitch.client.join(newUserInfo.login)
context.log.debug('Joined successfully.')

context.replicants.chat.channel.value = newUserInfo.login
})
25 changes: 7 additions & 18 deletions src/extension/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,24 @@ const isTwitchSession = (session: any) =>

const hasAccessDetails = (session: any) => session.passport.user.username && session.passport.user.accessToken

const performConnect = (session: any) => {
const { user } = session.passport

log.debug('Performing connect...')

return twitch.connect({
username: user.username,
token: user.accessToken,
})
}

const performDisconnect = () => {
log.debug('Performing disconnect...')
return twitch.disconnect()
}

const handleConnect = (session: any) => {
log.debug('Handling connect...')

if (!isTwitchSession(session) || !hasAccessDetails(session)) {
throw new Error('Invalid session data receieved')
}

return performConnect(session)
const { user } = session.passport

return twitch.connect({
username: user.username,
token: user.accessToken,
})
}

const handleDisconnect = () => {
log.debug('Handling disconnect...')
return performDisconnect()
return twitch.disconnect()
}

// listen for login and logout events emitted from nodecg's login module
Expand Down

0 comments on commit 5915b3d

Please sign in to comment.