11import { emittable , receivable } from "./events"
2- import * as socketio from "socket.io"
3-
4- declare global {
5- interface Window { io : Function }
6- }
2+ import Scoreboard from "../controller/src/model/Scoreboard"
3+ import io from "socket.io-client"
74
85export default class OBS {
96 private css : string = `
@@ -14,11 +11,15 @@ export default class OBS {
1411
1512 private name : string
1613 private timeout : number
17- private onUpdate : Function
18- private socket ?: socketio . Server
14+ private onUpdate : ( scoreboard : Scoreboard ) => void
15+ private socket ?: SocketIOClient . Socket
1916 private printedNotReady : boolean = true
2017
21- constructor ( name : string , onUpdate : Function , timeout = 500 ) {
18+ constructor (
19+ name : string ,
20+ onUpdate : ( scoreboard : Scoreboard ) => void ,
21+ timeout = 500
22+ ) {
2223 if ( typeof name !== "string" )
2324 throw new Error ( "name must be a string" )
2425
@@ -48,34 +49,34 @@ export default class OBS {
4849 sendError ( error : Error ) {
4950 if ( typeof this . socket !== "object" ) {
5051 throw new Error ( "Socket not available yet" )
51- return
5252 }
5353
5454 this . socket . emit ( emittable . clientError , error . stack )
5555 }
5656
57- connect ( server : string ) {
58- if ( typeof window . io !== "function" )
59- throw new Error ( "socket.io not found" )
60-
61- /* global io */
62- const socket = window . io ( server )
57+ connect ( server : string , callback ?: Function ) {
58+ const socket = io ( server )
6359 this . socket = socket
6460
65- socket . on ( receivable . connect , ( ) => {
61+ socket . on ( receivable . connect , ( anything : any ) => {
62+ console . log ( anything )
6663 this . log ( "Connected to OBScore-Host" )
6764 socket . emit ( emittable . introduce , this . name )
6865 socket . emit ( emittable . gibData )
66+
67+ if ( callback ) {
68+ callback ( )
69+ }
6970 } )
7071
7172 socket . on ( receivable . disconnect , ( ) =>
7273 this . logError ( "OBScore-Host Disconnected" )
7374 )
7475
75- socket . on ( receivable . update , ( data : Object ) => {
76+ socket . on ( receivable . update , ( scoreboard : Scoreboard ) => {
7677 this . log ( "Received update:" )
77- console . log ( data )
78- this . onUpdate ( data )
78+ console . log ( scoreboard )
79+ this . onUpdate ( scoreboard )
7980 this . printedNotReady = false
8081 } )
8182
0 commit comments