Skip to content

Commit

Permalink
Merge pull request #119 from traP-jp/SSlime/fix-frontend-ws
Browse files Browse the repository at this point in the history
Update WebSocket hostname and improve dispatcher handling
  • Loading branch information
ras0q authored Jan 25, 2025
2 parents 9f7454a + efef4e3 commit 6030572
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const App = () => {
const dispatcher = useExplorerDispatcher();
const setDispatcher = useSetAtom(dispatcherAtom);
useEffect(() => {
setDispatcher(dispatcher);
setDispatcher(() => dispatcher);
}, [dispatcher, setDispatcher]);

return (
Expand Down
23 changes: 16 additions & 7 deletions client/src/api/explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useSetAtom } from "jotai";
import { Position } from "../model/position";
import { ExplorationField, ExplorationFieldEvents } from "../schema2/explore";
import { serverWSHostName } from "./hostname";
import { useEffect, useRef } from "react";
import { useCallback, useEffect, useRef } from "react";
import fieldMessagesAtom from "../state/message";
import fieldReactionsAtom from "../state/reactions";
import { ReactionName } from "../model/reactions";
Expand All @@ -28,21 +28,28 @@ const useExplorerDispatcher = () => {
const setFieldSpeakerPhones = useSetAtom(fieldSpeakerPhonesAtom);
const setFieldExplorers = useSetAtom(fieldExplorersAtom);

const dispatcher: ExplorerMessageDispatcher = (mes) => {
const dispatcher: ExplorerMessageDispatcher = useCallback((mes) => {
if (!mes) return;
subscriberRef.current.dispatchEvent(
new CustomEvent(explorerEvent, {
detail: {
position: mes.position,
size: mes.size,
position: {
x: Math.round(mes.position.x),
y: Math.round(mes.position.y),
},
size: {
width: Math.round(mes.size.width),
height: Math.round(mes.size.height),
},
},
}),
);
};
}, []);

useEffect(() => {
const ws = new WebSocket(serverWSHostName);
subscriberRef.current.addEventListener(explorerEvent, (event: Event) => {
const currentSubscriber = subscriberRef.current;
const subscriverHandler = (event: Event) => {
// @ts-expect-error event is CustomEvent
const message = event.detail as ExplorerMessage;

Expand All @@ -57,7 +64,8 @@ const useExplorerDispatcher = () => {
};

ws.send(JSON.stringify(explorationField));
});
};
currentSubscriber.addEventListener(explorerEvent, subscriverHandler);
ws.onmessage = (event) => {
if (event.type !== "text") {
return;
Expand Down Expand Up @@ -196,6 +204,7 @@ const useExplorerDispatcher = () => {
};
return () => {
ws.close();
currentSubscriber.removeEventListener(explorerEvent, subscriverHandler);
};
}, [
setFieldMessages,
Expand Down
2 changes: 1 addition & 1 deletion client/src/api/hostname.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const serverHostName = "http://localhost:8000";
export const serverWSHostName = "ws://localhost:8000";
export const serverWSHostName = "ws://localhost:8000/ws";
export default serverHostName;
24 changes: 14 additions & 10 deletions client/src/pixi/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
} from "../model/position";
import Explorer from "./components/Explorer";
import PIXI from "pixi.js";
import useExplorerDispatcher from "../api/explorer";
import { useAtomValue } from "jotai";
import dispatcherAtom from "../state/dispatcher";

const mountHandler = import.meta.env.DEV
? (app: PIXI.Application) => {
Expand All @@ -39,9 +40,9 @@ const Canvas: React.FC<Props> = (props) => {
width: number;
height: number;
} | null>(null);
const [intervalID, setIntervalID] = useState<number | null>(null);
const intervalID = useRef<number | null>(null);
const stageRef = useRef<HTMLDivElement>(null);
const dispatcher = useExplorerDispatcher();
const dispatcher = useAtomValue(dispatcherAtom);

useEffect(() => {
const width = (window.innerWidth * 3) / 5;
Expand Down Expand Up @@ -79,24 +80,26 @@ const Canvas: React.FC<Props> = (props) => {
y: targetPosition.y - position.y,
};
if (Math.abs(diff.x) < 3 && Math.abs(diff.y) < 3) {
dispatcher({
dispatcher?.({
position: targetPosition,
size: fieldSize,
});

clearInterval(intervalID.current ?? undefined);
return targetPosition;
}
const nextPosition = calcNewPosition(position, {
x: diff.x / 10,
y: diff.y / 10,
});
dispatcher({
dispatcher?.({
position: nextPosition,
size: fieldSize,
});
return nextPosition;
});
},
[dispatcher, fieldSize],
[dispatcher, fieldSize, intervalID],
);

const onFieldClick = useCallback(
Expand Down Expand Up @@ -124,15 +127,16 @@ const Canvas: React.FC<Props> = (props) => {
userDisplayPosition,
);

if (intervalID !== null) {
clearInterval(intervalID);
if (intervalID.current !== null) {
clearInterval(intervalID.current);
}

const id = setInterval(() => {
updateUserPosition(clickPosition);
}, 1000 / 60);
setIntervalID(id);
intervalID.current = id;
},
[intervalID, updateUserPosition, userDisplayPosition, userPosition],
[updateUserPosition, userDisplayPosition, userPosition],
);

if (
Expand Down

0 comments on commit 6030572

Please sign in to comment.