Commit acfde169 authored by Aral Balkan's avatar Aral Balkan
Browse files

Private socket now stored in context and properly logged out

parent ef09efa8
......@@ -5,23 +5,24 @@
import { get } from 'svelte/store'
import { state } from '../state.js'
const { privateToken } = getContext(state)
const { privateToken, privateSocket } = getContext(state)
// NOTE: All this socket-related code should be taken out of the view.
let socketConnectionInitialised = false
let messages = []
function onShow() {
if (socketConnectionInitialised) return ''
console.log('Attempting to connect using private token', get(privateToken))
if ($privateSocket === null || $privateSocket.readyState !== 1) {
console.log('Attempting to connect using private token', get(privateToken))
// Create a web socket connection.
const privateSocket = new WebSocket(`wss://${location.hostname}/private/${get(privateToken)}`)
privateSocket.onmessage = message => {
messages = [...messages, message]
// Create a web socket connection.
$privateSocket = new WebSocket(`wss://${location.hostname}/private/${get(privateToken)}`)
$privateSocket.onmessage = message => {
messages = [...messages, message]
}
} else {
console.log('Socket is open, not creating a new one.')
}
socketConnectionInitialised = true
// Return an empty string so nothing is rendered as this is called from the view.
return ''
}
</script>
......
......@@ -3,12 +3,17 @@
import { state } from '../state.js'
import Section from './Section.svelte'
let { signedIn, privateToken, currentSection } = getContext(state)
const NORMAL_CLOSURE = 1000
let { signedIn, privateToken, privateSocket, currentSection } = getContext(state)
async function handleSignOut () {
console.log('private socket', $privateSocket)
$privateSocket.send('about to close')
$privateSocket.close(NORMAL_CLOSURE, 'Person signed out.')
$privateToken = null
$signedIn = false
$currentSection = '/private'
$currentSection = '/sign-in'
location.hash = '/sign-in'
}
</script>
......
......@@ -11,9 +11,10 @@ export const state = {}
export function initialise () {
const signedIn = writable(false)
const privateToken = writable(null)
const privateSocket = writable(null)
const currentSection = writable(currentSectionFromHash(false))
setContext(state, { currentSection, signedIn, privateToken })
setContext(state, { currentSection, signedIn, privateToken, privateSocket })
window.addEventListener('hashchange', () => {
const _signedIn = get(signedIn)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment