Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

WebSocket繋ぎ込み #25

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/pages/home/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { useEffect, useState } from "react";
import { DefaultButton } from "../../components/ui/Button";
import { useSocketRefStore } from "../../store";
import { device } from "../../utils/device";
import { requestPermission } from "../../utils/permission";
import styles from "./index.module.css";

function Home() {
const [isPcScreen, setIsPcScreen] = useState(
window.matchMedia(device.pc).matches,
);
const setRef = useSocketRefStore((state) => state.setRef);

useEffect(() => {
const mediaQuery = window.matchMedia(device.pc);
Expand Down Expand Up @@ -36,6 +39,12 @@ function Home() {
console.error("オーディオの音が出なかった", error);
window.location.href = isPcScreen ? "/yatai" : "/shooter";
});
requestPermission();
const socketRef = new WebSocket(
`wss://${import.meta.env.VITE_HOST_NAME}/ws`,
);
setRef({ current: socketRef });
window.location.href = isPcScreen ? "/yatai" : "/shooter";
};

return (
Expand Down Expand Up @@ -66,7 +75,9 @@ function Home() {
/>
</div>
<div
className={`${styles["go-game"]} ${isPcScreen ? styles["go-game-pc"] : ""}`}
className={`${styles["go-game"]} ${
isPcScreen ? styles["go-game-pc"] : ""
}`}
>
<DefaultButton color="red" size="lg" onClick={handleClick}>
射的へ向かう
Expand Down
47 changes: 38 additions & 9 deletions src/pages/shooter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import { type KeyboardEventHandler, useState } from "react";
import { DefaultButton } from "../../components/ui/Button";
import { Modal } from "../../components/ui/Modal";
import { ShooterButton } from "../../components/ui/ShooterButton";
import { useOrientation } from "../../hooks/useOrientation";
import { useSocketRefStore, useUUIDStore } from "../../store";
import { type Schema, message_type } from "../../type/schema";
import style from "./index.module.css";

function Shooter() {
const Shooter = () => {
const [isOpen, setIsOpen] = useState(true);
const { orientationDiff } = useOrientation();
const socketRef = useSocketRefStore((state) => state.socketRef);

const initialImages = [
"/2D_material/cork.webp",
Expand All @@ -14,18 +19,37 @@ function Shooter() {
];

const [images, setImages] = useState(initialImages);
const uuid = useUUIDStore((state) => state.uuid);

const handleClick = () => {
const audio = new Audio("/sound/cork_sound.mp3");
audio
.play()
.then(() => {
setImages((prevImages) => prevImages.slice(1));
})
.then(() => {})
.catch((error) => {
console.error("オーディオの音が出なかった", error);
setImages((prevImages) => prevImages.slice(1));
});
const data: Schema = {
id: uuid,
interval: 0,
angle: {
x: orientationDiff.beta,
y: orientationDiff.gamma,
},
acceleration: {
x: 0,
y: 0,
z: 0,
},
distance: {
x: 0,
y: 0,
z: 0,
},
message_type: message_type.action,
};
socketRef?.current?.send(JSON.stringify(data));
setImages((prevImages) => prevImages.slice(1));
};

const handleKeyUp: KeyboardEventHandler<HTMLDivElement> = (event) => {
Expand All @@ -43,19 +67,21 @@ function Shooter() {
<ShooterButton onClick={handleClick} onKeyUp={handleKeyUp} />
</div>
<div className={style.cork}>
{images.map((src) => (
<img key={src} src={src} alt="コルクの残量を表示しています" />
{images.map((src, i) => (
// biome-ignore lint/suspicious/noArrayIndexKey: <explanation>
<img key={i} src={src} alt="コルクの残量を表示しています" />
))}
</div>
</div>
);
}
};

type ModalContentProps = {
setIsOpen: (isOpen: boolean) => void;
};

const ModalContent: React.FC<ModalContentProps> = ({ setIsOpen }) => {
const { reset } = useOrientation();
return (
<div className={style["modal-wrapper"]}>
<img
Expand All @@ -76,7 +102,10 @@ const ModalContent: React.FC<ModalContentProps> = ({ setIsOpen }) => {
variant="outlined"
color="red"
size="md"
onClick={() => setIsOpen(false)}
onClick={() => {
reset();
setIsOpen(false);
}}
>
置いたよ!
</DefaultButton>
Expand Down
17 changes: 17 additions & 0 deletions src/pages/yatai/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Physics, useBox } from "@react-three/cannon";
import { Canvas, type ThreeElements, useThree } from "@react-three/fiber";
import { useEffect } from "react";
import type {
BufferGeometry,
Material,
Expand All @@ -8,9 +9,25 @@ import type {
Object3DEventMap,
} from "three";
import { randFloat } from "three/src/math/MathUtils.js";
import { useSocketRefStore } from "../../store";
import styles from "./index.module.css";

function Yatai() {
const socketRef = useSocketRefStore((state) => state.socketRef);

useEffect(() => {
const onMessage = (event: MessageEvent) => {
const data = JSON.parse(event.data);
// サーバーから受け取ったデータを使って処理をする
console.log(data);
};
const currentSocketRef = socketRef?.current;
currentSocketRef?.addEventListener("message", onMessage);
return () => {
currentSocketRef?.removeEventListener("message", onMessage);
};
}, [socketRef]);

// 土台
const Foundation = (props: ThreeElements["mesh"]) => {
const args: [number, number, number] = [10, 2, 2];
Expand Down
File renamed without changes.