Revert "Remove optionality from session socket"
This reverts commit b307a20169
.
This commit is contained in:
parent
c76e4e04fa
commit
99a27365ae
@ -23,10 +23,10 @@ export function PartyProvider({ session, children }: PartyProviderProps) {
|
||||
}
|
||||
}
|
||||
|
||||
session.socket.on("party_state", handleSocketPartyState);
|
||||
session.socket?.on("party_state", handleSocketPartyState);
|
||||
|
||||
return () => {
|
||||
session.socket.off("party_state", handleSocketPartyState);
|
||||
session.socket?.off("party_state", handleSocketPartyState);
|
||||
};
|
||||
});
|
||||
|
||||
|
@ -9,12 +9,10 @@ import useNetworkedState, {
|
||||
import Session, { SessionStatus } from "../network/Session";
|
||||
import { PlayerState } from "../types/PlayerState";
|
||||
|
||||
export const PlayerStateContext = React.createContext<PlayerState | undefined>(
|
||||
undefined
|
||||
);
|
||||
export const PlayerUpdaterContext = React.createContext<
|
||||
SetNetworkedState<PlayerState> | undefined
|
||||
>(undefined);
|
||||
export const PlayerStateContext =
|
||||
React.createContext<PlayerState | undefined>(undefined);
|
||||
export const PlayerUpdaterContext =
|
||||
React.createContext<SetNetworkedState<PlayerState> | undefined>(undefined);
|
||||
|
||||
type PlayerProviderProps = {
|
||||
session: Session;
|
||||
@ -106,13 +104,13 @@ export function PlayerProvider({ session, children }: PlayerProviderProps) {
|
||||
}
|
||||
|
||||
session.on("status", handleSocketStatus);
|
||||
session.socket.on("connect", handleSocketConnect);
|
||||
session.socket.io.on("reconnect", handleSocketConnect);
|
||||
session.socket?.on("connect", handleSocketConnect);
|
||||
session.socket?.io.on("reconnect", handleSocketConnect);
|
||||
|
||||
return () => {
|
||||
session.off("status", handleSocketStatus);
|
||||
session.socket.off("connect", handleSocketConnect);
|
||||
session.socket.io.off("reconnect", handleSocketConnect);
|
||||
session.socket?.off("connect", handleSocketConnect);
|
||||
session.socket?.io.off("reconnect", handleSocketConnect);
|
||||
};
|
||||
});
|
||||
|
||||
|
@ -60,7 +60,7 @@ function useNetworkedState<S extends { readonly [x: string]: any } | null>(
|
||||
const debouncedState = useDebounce(state, debounceRate);
|
||||
const lastSyncedStateRef = useRef<S>();
|
||||
useEffect(() => {
|
||||
if (dirtyRef.current) {
|
||||
if (session.socket && dirtyRef.current) {
|
||||
// If partial updates enabled, send just the changes to the socket
|
||||
if (
|
||||
lastSyncedStateRef.current &&
|
||||
@ -112,11 +112,11 @@ function useNetworkedState<S extends { readonly [x: string]: any } | null>(
|
||||
});
|
||||
}
|
||||
|
||||
session.socket.on(eventName, handleSocketEvent);
|
||||
session.socket.on(`${eventName}_update`, handleSocketUpdateEvent);
|
||||
session.socket?.on(eventName, handleSocketEvent);
|
||||
session.socket?.on(`${eventName}_update`, handleSocketUpdateEvent);
|
||||
return () => {
|
||||
session.socket.off(eventName, handleSocketEvent);
|
||||
session.socket.off(`${eventName}_update`, handleSocketUpdateEvent);
|
||||
session.socket?.off(eventName, handleSocketEvent);
|
||||
session.socket?.off(`${eventName}_update`, handleSocketUpdateEvent);
|
||||
};
|
||||
}, [session.socket, eventName, partialUpdatesKey]);
|
||||
|
||||
|
@ -202,12 +202,12 @@ function NetworkedMapAndTokens({ session }: { session: Session }) {
|
||||
) {
|
||||
// Clear map before sending new one
|
||||
setCurrentMap(null);
|
||||
session.socket.emit("map", null);
|
||||
session.socket?.emit("map", null);
|
||||
|
||||
setCurrentMapState(newMapState, true, true);
|
||||
setCurrentMap(newMap);
|
||||
|
||||
session.socket.emit("map", newMap);
|
||||
session.socket?.emit("map", newMap);
|
||||
|
||||
if (!newMap || !newMapState) {
|
||||
setAssetManifest(null, true, true);
|
||||
@ -395,12 +395,12 @@ function NetworkedMapAndTokens({ session }: { session: Session }) {
|
||||
|
||||
session.on("peerData", handlePeerData);
|
||||
session.on("peerDataProgress", handlePeerDataProgress);
|
||||
session.socket.on("map", handleSocketMap);
|
||||
session.socket?.on("map", handleSocketMap);
|
||||
|
||||
return () => {
|
||||
session.off("peerData", handlePeerData);
|
||||
session.off("peerDataProgress", handlePeerDataProgress);
|
||||
session.socket.off("map", handleSocketMap);
|
||||
session.socket?.off("map", handleSocketMap);
|
||||
};
|
||||
});
|
||||
|
||||
|
@ -152,10 +152,10 @@ function NetworkedMapPointer({ session, active }: NetworkedMapPointerProps) {
|
||||
}
|
||||
}
|
||||
|
||||
session.socket.on("player_pointer", handleSocketPlayerPointer);
|
||||
session.socket?.on("player_pointer", handleSocketPlayerPointer);
|
||||
|
||||
return () => {
|
||||
session.socket.off("player_pointer", handleSocketPlayerPointer);
|
||||
session.socket?.off("player_pointer", handleSocketPlayerPointer);
|
||||
};
|
||||
}, [session]);
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
import io from "socket.io-client";
|
||||
import io, { Socket } from "socket.io-client";
|
||||
import msgParser from "socket.io-msgpack-parser";
|
||||
import { EventEmitter } from "events";
|
||||
|
||||
@ -32,11 +32,10 @@ export type PeerReply = (id: string, data: PeerData, chunkId?: string) => void;
|
||||
class Session extends EventEmitter {
|
||||
/**
|
||||
* The socket io connection
|
||||
*
|
||||
* @type {io.Socket}
|
||||
*/
|
||||
socket = io(process.env.REACT_APP_BROKER_URL!, {
|
||||
withCredentials: true,
|
||||
parser: msgParser,
|
||||
});
|
||||
socket?: Socket;
|
||||
|
||||
/**
|
||||
* A mapping of socket ids to session peers
|
||||
@ -46,7 +45,7 @@ class Session extends EventEmitter {
|
||||
peers: Record<string, SessionPeer>;
|
||||
|
||||
get id() {
|
||||
return this.socket.id;
|
||||
return this.socket?.id || "";
|
||||
}
|
||||
|
||||
_iceServers: RTCIceServer[] = [];
|
||||
@ -77,6 +76,14 @@ class Session extends EventEmitter {
|
||||
const data = await response.json();
|
||||
this._iceServers = data.iceServers;
|
||||
|
||||
if (!process.env.REACT_APP_BROKER_URL) {
|
||||
return;
|
||||
}
|
||||
this.socket = io(process.env.REACT_APP_BROKER_URL, {
|
||||
withCredentials: true,
|
||||
parser: msgParser,
|
||||
});
|
||||
|
||||
this.socket.on("player_joined", this._handlePlayerJoined.bind(this));
|
||||
this.socket.on("player_left", this._handlePlayerLeft.bind(this));
|
||||
this.socket.on("joined_game", this._handleJoinedGame.bind(this));
|
||||
@ -95,7 +102,7 @@ class Session extends EventEmitter {
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
this.socket.disconnect();
|
||||
this.socket?.disconnect();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -184,7 +191,7 @@ class Session extends EventEmitter {
|
||||
|
||||
this._gameId = gameId;
|
||||
this._password = password;
|
||||
this.socket.emit(
|
||||
this.socket?.emit(
|
||||
"join_game",
|
||||
gameId,
|
||||
password,
|
||||
@ -217,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 = () => {
|
||||
@ -366,7 +373,7 @@ class Session extends EventEmitter {
|
||||
}
|
||||
|
||||
_handleForceUpdate() {
|
||||
this.socket.disconnect();
|
||||
this.socket?.disconnect();
|
||||
this.emit("status", "needs_update");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user