Skip to content
Snippets Groups Projects

Add comments to socket files

Merged Victor Löfgren requested to merge 192-comment-sockets-py-and-sockets-ts into dev
2 files
+ 39
5
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 24
1
/**
* Handles everything that has to do with syncing active competitions.
*
* @module
*/
import io from 'socket.io-client'
import { setCurrentSlideByOrder, setPresentationShowScoreboard, setPresentationTimer } from './actions/presentation'
import { TimerState } from './interfaces/Timer'
import store from './store'
/**
* The values that can be synced between clients connected to the same presentation.
*/
interface SyncInterface {
slide_order?: number
timer?: TimerState
@@ -11,9 +20,15 @@ interface SyncInterface {
let socket: SocketIOClient.Socket
/**
* Connect to server, setup authorization header and listen to some events.
*
* @param role The role the connecting client has
*/
export const socketConnect = (role: 'Judge' | 'Operator' | 'Team' | 'Audience') => {
if (socket) return
// The token is the JWT returned from the login/code API call.
const token = localStorage[`${role}Token`]
socket = io('localhost:5000', {
transportOptions: {
@@ -26,7 +41,7 @@ export const socketConnect = (role: 'Judge' | 'Operator' | 'Team' | 'Audience')
})
socket.on('sync', (data: SyncInterface) => {
// The order of these is important, for some reason
// The order of these is important, for some reason, so dont change it
if (data.timer !== undefined) setPresentationTimer(data.timer)(store.dispatch)
if (data.slide_order !== undefined) setCurrentSlideByOrder(data.slide_order)(store.dispatch, store.getState)
if (data.show_scoreboard !== undefined) setPresentationShowScoreboard(data.show_scoreboard)(store.dispatch)
@@ -37,10 +52,18 @@ export const socketConnect = (role: 'Judge' | 'Operator' | 'Team' | 'Audience')
})
}
/**
* Disconnect all clients.
*/
export const socketEndPresentation = () => {
socket.emit('end_presentation')
}
/**
* Sync data between all connected clients.
*
* @param syncData The data to sync between all clients connected to the same presentation
*/
export const socketSync = (syncData: SyncInterface) => {
socket.emit('sync', syncData)
}
Loading