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