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