This repository has been archived by the owner on Nov 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from claustra01/feature/websocket-base
WebSocketの基盤とか、角度センサーとか色々
- Loading branch information
Showing
11 changed files
with
190 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
VITE_HOST_NAME= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { useCallback, useEffect, useState } from "react"; | ||
|
||
export interface Orientation { | ||
alpha: number; | ||
beta: number; | ||
gamma: number; | ||
} | ||
|
||
export const useOrientation = () => { | ||
const [initialOrientation, setInitialOrientation] = | ||
useState<Orientation | null>(null); | ||
const [orientationDiff, setOrientationDiff] = useState<Orientation>({ | ||
alpha: 0, | ||
beta: 0, | ||
gamma: 0, | ||
}); | ||
|
||
const handleOrientation = useCallback( | ||
(event: DeviceOrientationEvent) => { | ||
if (!initialOrientation) { | ||
setInitialOrientation({ | ||
alpha: event.alpha || 0, | ||
beta: event.beta || 0, | ||
gamma: event.gamma || 0, | ||
}); | ||
} else { | ||
setOrientationDiff({ | ||
alpha: (event.alpha || 0) - initialOrientation.alpha, | ||
beta: (event.beta || 0) - initialOrientation.beta, | ||
gamma: (event.gamma || 0) - initialOrientation.gamma, | ||
}); | ||
} | ||
}, | ||
[initialOrientation], | ||
); | ||
|
||
const reset = useCallback(() => { | ||
setInitialOrientation(null); | ||
}, []); | ||
|
||
useEffect(() => { | ||
window.addEventListener("deviceorientation", handleOrientation); | ||
return () => { | ||
window.removeEventListener("deviceorientation", handleOrientation); | ||
}; | ||
}, [handleOrientation]); | ||
|
||
return { orientationDiff, reset }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { useSocketRefStore } from "./useSocketRefStore"; | ||
export { useUUIDStore } from "./useUUIDStore"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { MutableRefObject } from "react"; | ||
import { create } from "zustand"; | ||
|
||
type State = { | ||
socketRef: MutableRefObject<WebSocket | undefined> | null; | ||
}; | ||
|
||
type Action = { | ||
setRef: (ref: MutableRefObject<WebSocket | undefined>) => void; | ||
}; | ||
|
||
export const useSocketRefStore = create<State & Action>()((set) => ({ | ||
socketRef: null, | ||
setRef: (ref) => set(() => ({ socketRef: ref })), | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { create } from "zustand"; | ||
import { persist } from "zustand/middleware"; | ||
import { generateUUID } from "../utils/uuid"; | ||
|
||
type State = { | ||
uuid: string; | ||
}; | ||
|
||
type Action = { | ||
updateUUID: () => void; | ||
}; | ||
|
||
export const useUUIDStore = create<State & Action>()( | ||
persist( | ||
(set) => ({ | ||
uuid: generateUUID(), | ||
updateUUID: () => set(() => ({ uuid: generateUUID() })), | ||
}), | ||
{ | ||
name: "user-uuid", | ||
}, | ||
), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export interface Schema { | ||
id: string; | ||
interval: number; | ||
angle: angle; | ||
acceleration: acceleration; | ||
distance: distance; | ||
message_type: message_type; | ||
} | ||
|
||
interface angle { | ||
x: number; | ||
y: number; | ||
} | ||
|
||
interface acceleration { | ||
x: number; | ||
y: number; | ||
z: number; | ||
} | ||
|
||
interface distance { | ||
x: number; | ||
y: number; | ||
z: number; | ||
} | ||
|
||
export enum message_type { | ||
pointer = "pointer", | ||
action = "action", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* 角度・加速度センサーのパーミッションをリクエストする関数 | ||
*/ | ||
export const requestPermission = async () => { | ||
// @ts-expect-error only safari property | ||
if (typeof DeviceMotionEvent.requestPermission === "function") { | ||
try { | ||
// @ts-expect-error only safari property | ||
const permissionState = await DeviceMotionEvent.requestPermission(); | ||
if (permissionState !== "granted") { | ||
console.warn("Device Motion permission denied."); | ||
} | ||
} catch (error) { | ||
console.error("Error requesting Device Motion permission:", error); | ||
} | ||
} | ||
|
||
// @ts-expect-error only safari property | ||
if (typeof DeviceOrientationEvent.requestPermission === "function") { | ||
try { | ||
const permissionState = | ||
// @ts-expect-error only safari property | ||
await DeviceOrientationEvent.requestPermission(); | ||
if (permissionState !== "granted") { | ||
console.warn("Device Orientation permission denied."); | ||
} | ||
} catch (error) { | ||
console.error("Error requesting Device Orientation permission:", error); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const generateUUID = (): string => { | ||
const timestamp = Date.now().toString(16); | ||
const randomHex = Math.floor(Math.random() * 0xffffff).toString(16); | ||
return `${timestamp}-${randomHex}`; | ||
}; |