From 84384a8d30c97b22fad027fb05ea62a095b93a7c Mon Sep 17 00:00:00 2001 From: cha0s Date: Mon, 18 Jan 2021 12:35:38 -0600 Subject: [PATCH] feat: reconnection --- app/src/react/components/play/index.jsx | 4 ++++ .../react/components/renderers/room-view.jsx | 7 ++----- packages/core/package.json | 1 + packages/core/src/hooks/use-room.js | 17 ++++++++++++++++- packages/universe/src/resources/universe.js | 4 ++++ packages/universe/src/server/packets/join.js | 5 ++++- 6 files changed, 31 insertions(+), 7 deletions(-) diff --git a/app/src/react/components/play/index.jsx b/app/src/react/components/play/index.jsx index 54040f8..75d9226 100644 --- a/app/src/react/components/play/index.jsx +++ b/app/src/react/components/play/index.jsx @@ -21,6 +21,10 @@ const Play = () => { dispatch(setSelfEntity(await socket.send(['Join', uuid]))); }; join(); + socket.on('reconnect', join); + return () => { + socket.off('reconnect', join); + }; }, [dispatch, socket, uuid]); useEffect(() => { if (!ref.current) { diff --git a/app/src/react/components/renderers/room-view.jsx b/app/src/react/components/renderers/room-view.jsx index f154787..d513122 100644 --- a/app/src/react/components/renderers/room-view.jsx +++ b/app/src/react/components/renderers/room-view.jsx @@ -36,15 +36,12 @@ const RoomViewComponent = () => { return renderer; } return undefined; - }, [app.renderer]); + }, [app]); const selfEntity = useSelfEntity(); const [offset, setOffset] = useState([0, 0]); const room = useRoom(); useEffect(() => { - if (!room) { - return undefined; - } - if (!selfEntity) { + if (!room || !selfEntity) { return undefined; } selfEntity.camera.realPosition = selfEntity.camera.position; diff --git a/packages/core/package.json b/packages/core/package.json index ab92690..b9c57ca 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -30,6 +30,7 @@ "@latus/core": "^2.0.0", "@latus/react": "^2.0.0", "@latus/redux": "^2.0.0", + "@latus/socket": "^2.0.0", "debug": "4.3.1" }, "devDependencies": { diff --git a/packages/core/src/hooks/use-room.js b/packages/core/src/hooks/use-room.js index 1723525..4f53f72 100644 --- a/packages/core/src/hooks/use-room.js +++ b/packages/core/src/hooks/use-room.js @@ -1,10 +1,25 @@ import {resource} from '@avocado/resource'; import {useEffect, useLatus, useState} from '@latus/react/client'; +import {useSocket} from '@latus/socket'; export default () => { const [room, setRoom] = useState(); const latus = useLatus(); + const socket = useSocket(); const synchronizer = latus.get('%synchronizer'); + useEffect(() => { + if (!room) { + return undefined; + } + const onDisconnect = () => { + synchronizer.deleteSynchronized(room.constructor.resourceId, room.s13nId()); + setRoom(undefined); + }; + socket.on('disconnect', onDisconnect); + return () => { + socket.off('disconnect', onDisconnect); + }; + }, [room, socket, synchronizer]); useEffect(() => { if (!synchronizer) { return undefined; @@ -24,6 +39,6 @@ export default () => { return () => { synchronizer.off('created', onCreated); }; - }, [latus, synchronizer]); + }, [latus, room, synchronizer]); return room; }; diff --git a/packages/universe/src/resources/universe.js b/packages/universe/src/resources/universe.js index 70f7517..c7801bc 100644 --- a/packages/universe/src/resources/universe.js +++ b/packages/universe/src/resources/universe.js @@ -70,6 +70,10 @@ export default (latus) => class Universe extends JsonResource { this.#tps = tps; } + playerForSocket(socketId) { + return this.#players.find(({socket}) => socket.id === socketId); + } + playerForUser(userId) { return this.#players.find(({user}) => user.id === userId); } diff --git a/packages/universe/src/server/packets/join.js b/packages/universe/src/server/packets/join.js index feb6e19..7051854 100644 --- a/packages/universe/src/server/packets/join.js +++ b/packages/universe/src/server/packets/join.js @@ -42,7 +42,10 @@ export default (latus) => class ServerJoin extends Join() { user, }); socket.on('disconnect', () => { - universe.removePlayer(player); + const player = universe.playerForSocket(socket.id); + if (player) { + universe.removePlayer(player); + } }); return entity.instanceUuid; }