diff --git a/src/network/Session.ts b/src/network/Session.ts index de04cc1..a85b8ad 100644 --- a/src/network/Session.ts +++ b/src/network/Session.ts @@ -35,7 +35,7 @@ class Session extends EventEmitter { * * @type {io.Socket} */ - socket: Socket = io(); + socket?: Socket; /** * A mapping of socket ids to session peers @@ -45,7 +45,7 @@ class Session extends EventEmitter { peers: Record; get id() { - return this.socket && this.socket.id; + return this.socket?.id || ""; } _iceServers: RTCIceServer[] = []; @@ -191,7 +191,7 @@ class Session extends EventEmitter { this._gameId = gameId; this._password = password; - this.socket.emit( + this.socket?.emit( "join_game", gameId, password, @@ -224,7 +224,7 @@ class Session extends EventEmitter { }; const handleSignal = (signal: SignalData) => { - this.socket.emit("signal", JSON.stringify({ to: peer.id, signal })); + this.socket?.emit("signal", JSON.stringify({ to: peer.id, signal })); }; const handleConnect = () => { @@ -373,7 +373,7 @@ class Session extends EventEmitter { } _handleForceUpdate() { - this.socket.disconnect(); + this.socket?.disconnect(); this.emit("status", "needs_update"); } }