feat: reconnection

This commit is contained in:
cha0s 2021-01-18 12:35:38 -06:00
parent 8964b49748
commit 84384a8d30
6 changed files with 31 additions and 7 deletions

View File

@ -21,6 +21,10 @@ const Play = () => {
dispatch(setSelfEntity(await socket.send(['Join', uuid]))); dispatch(setSelfEntity(await socket.send(['Join', uuid])));
}; };
join(); join();
socket.on('reconnect', join);
return () => {
socket.off('reconnect', join);
};
}, [dispatch, socket, uuid]); }, [dispatch, socket, uuid]);
useEffect(() => { useEffect(() => {
if (!ref.current) { if (!ref.current) {

View File

@ -36,15 +36,12 @@ const RoomViewComponent = () => {
return renderer; return renderer;
} }
return undefined; return undefined;
}, [app.renderer]); }, [app]);
const selfEntity = useSelfEntity(); const selfEntity = useSelfEntity();
const [offset, setOffset] = useState([0, 0]); const [offset, setOffset] = useState([0, 0]);
const room = useRoom(); const room = useRoom();
useEffect(() => { useEffect(() => {
if (!room) { if (!room || !selfEntity) {
return undefined;
}
if (!selfEntity) {
return undefined; return undefined;
} }
selfEntity.camera.realPosition = selfEntity.camera.position; selfEntity.camera.realPosition = selfEntity.camera.position;

View File

@ -30,6 +30,7 @@
"@latus/core": "^2.0.0", "@latus/core": "^2.0.0",
"@latus/react": "^2.0.0", "@latus/react": "^2.0.0",
"@latus/redux": "^2.0.0", "@latus/redux": "^2.0.0",
"@latus/socket": "^2.0.0",
"debug": "4.3.1" "debug": "4.3.1"
}, },
"devDependencies": { "devDependencies": {

View File

@ -1,10 +1,25 @@
import {resource} from '@avocado/resource'; import {resource} from '@avocado/resource';
import {useEffect, useLatus, useState} from '@latus/react/client'; import {useEffect, useLatus, useState} from '@latus/react/client';
import {useSocket} from '@latus/socket';
export default () => { export default () => {
const [room, setRoom] = useState(); const [room, setRoom] = useState();
const latus = useLatus(); const latus = useLatus();
const socket = useSocket();
const synchronizer = latus.get('%synchronizer'); 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(() => { useEffect(() => {
if (!synchronizer) { if (!synchronizer) {
return undefined; return undefined;
@ -24,6 +39,6 @@ export default () => {
return () => { return () => {
synchronizer.off('created', onCreated); synchronizer.off('created', onCreated);
}; };
}, [latus, synchronizer]); }, [latus, room, synchronizer]);
return room; return room;
}; };

View File

@ -70,6 +70,10 @@ export default (latus) => class Universe extends JsonResource {
this.#tps = tps; this.#tps = tps;
} }
playerForSocket(socketId) {
return this.#players.find(({socket}) => socket.id === socketId);
}
playerForUser(userId) { playerForUser(userId) {
return this.#players.find(({user}) => user.id === userId); return this.#players.find(({user}) => user.id === userId);
} }

View File

@ -42,7 +42,10 @@ export default (latus) => class ServerJoin extends Join() {
user, user,
}); });
socket.on('disconnect', () => { socket.on('disconnect', () => {
universe.removePlayer(player); const player = universe.playerForSocket(socket.id);
if (player) {
universe.removePlayer(player);
}
}); });
return entity.instanceUuid; return entity.instanceUuid;
} }