mirror of
https://github.com/streamyfin/streamyfin.git
synced 2026-05-27 01:06:42 +01:00
fix: ws now works as expexted
This commit is contained in:
@@ -25,7 +25,7 @@ export const CurrentlyPlayingBar: React.FC = () => {
|
|||||||
pauseVideo,
|
pauseVideo,
|
||||||
playVideo,
|
playVideo,
|
||||||
setCurrentlyPlayingState,
|
setCurrentlyPlayingState,
|
||||||
stopVideo,
|
stopPlayback,
|
||||||
setIsPlaying,
|
setIsPlaying,
|
||||||
isPlaying,
|
isPlaying,
|
||||||
videoRef,
|
videoRef,
|
||||||
@@ -260,7 +260,7 @@ export const CurrentlyPlayingBar: React.FC = () => {
|
|||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
<TouchableOpacity
|
<TouchableOpacity
|
||||||
onPress={() => {
|
onPress={() => {
|
||||||
setCurrentlyPlayingState(null);
|
stopPlayback();
|
||||||
}}
|
}}
|
||||||
className="aspect-square rounded flex flex-col items-center justify-center p-2"
|
className="aspect-square rounded flex flex-col items-center justify-center p-2"
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -1,56 +0,0 @@
|
|||||||
import { apiAtom } from "@/providers/JellyfinProvider";
|
|
||||||
import { useAtom } from "jotai";
|
|
||||||
import React, { useEffect, useState } from "react";
|
|
||||||
import { View, Text } from "react-native";
|
|
||||||
import { currentlyPlayingItemAtom, playingAtom } from "../CurrentlyPlayingBar";
|
|
||||||
|
|
||||||
export const WebSocketsTest = () => {
|
|
||||||
const [ws, setWs] = useState<WebSocket | null>(null);
|
|
||||||
|
|
||||||
const [api] = useAtom(apiAtom);
|
|
||||||
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!api || !api.accessToken || !api.basePath) return;
|
|
||||||
|
|
||||||
// Set up WebSocket connection
|
|
||||||
const newWebSocket = new WebSocket(
|
|
||||||
`wss://${api?.basePath
|
|
||||||
.replace("https://", "")
|
|
||||||
.replace("http://", "")}/socket?api_key=${api?.accessToken}&deviceId=${
|
|
||||||
api?.deviceInfo.id
|
|
||||||
}`
|
|
||||||
);
|
|
||||||
|
|
||||||
newWebSocket.onopen = () => {
|
|
||||||
console.log("WebSocket connection established");
|
|
||||||
// You can also send data once the connection is open
|
|
||||||
newWebSocket.send(
|
|
||||||
JSON.stringify({ type: "greeting", payload: "Hello from client!" })
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
newWebSocket.onmessage = (e) => {};
|
|
||||||
|
|
||||||
newWebSocket.onerror = (e) => {
|
|
||||||
console.error("WebSocket error:", e);
|
|
||||||
};
|
|
||||||
|
|
||||||
newWebSocket.onclose = (e) => {
|
|
||||||
console.log("WebSocket connection closed:", e.reason);
|
|
||||||
};
|
|
||||||
|
|
||||||
setWs(newWebSocket);
|
|
||||||
|
|
||||||
// Clean up function
|
|
||||||
return () => {
|
|
||||||
newWebSocket.close();
|
|
||||||
};
|
|
||||||
}, [api]);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<View>
|
|
||||||
<Text>WebSocket Demo</Text>
|
|
||||||
</View>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
@@ -73,67 +73,6 @@ export const JellyfinProvider: React.FC<{ children: ReactNode }> = ({
|
|||||||
|
|
||||||
const [api, setApi] = useAtom(apiAtom);
|
const [api, setApi] = useAtom(apiAtom);
|
||||||
const [user, setUser] = useAtom(userAtom);
|
const [user, setUser] = useAtom(userAtom);
|
||||||
const [ws, setWs] = useAtom(wsAtom);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!deviceId || !api) return;
|
|
||||||
|
|
||||||
const url = `wss://${api?.basePath
|
|
||||||
.replace("https://", "")
|
|
||||||
.replace("http://", "")}/socket?api_key=${
|
|
||||||
api?.accessToken
|
|
||||||
}&deviceId=${deviceId}`;
|
|
||||||
|
|
||||||
console.log("WS", url);
|
|
||||||
|
|
||||||
const newWebSocket = new WebSocket(url);
|
|
||||||
|
|
||||||
let keepAliveInterval: NodeJS.Timeout | null = null;
|
|
||||||
|
|
||||||
newWebSocket.onopen = () => {
|
|
||||||
setIsConnected(true);
|
|
||||||
// Start sending "KeepAlive" message every 30 seconds
|
|
||||||
keepAliveInterval = setInterval(() => {
|
|
||||||
if (newWebSocket.readyState === WebSocket.OPEN) {
|
|
||||||
newWebSocket.send(JSON.stringify({ MessageType: "KeepAlive" }));
|
|
||||||
console.log("KeepAlive message sent");
|
|
||||||
}
|
|
||||||
}, 30000);
|
|
||||||
};
|
|
||||||
|
|
||||||
newWebSocket.onmessage = (e) => {
|
|
||||||
const json = JSON.parse(e.data);
|
|
||||||
const command = json?.Data?.Command;
|
|
||||||
|
|
||||||
// On PlayPause
|
|
||||||
if (command === "PlayPause") {
|
|
||||||
console.log("Command ~ PlayPause");
|
|
||||||
} else if (command === "Stop") {
|
|
||||||
console.log("Command ~ Stop");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
newWebSocket.onerror = (e) => {
|
|
||||||
console.error("WebSocket error:", e);
|
|
||||||
setIsConnected(false);
|
|
||||||
};
|
|
||||||
|
|
||||||
newWebSocket.onclose = (e) => {
|
|
||||||
console.log("WebSocket connection closed:", e.reason);
|
|
||||||
if (keepAliveInterval) {
|
|
||||||
clearInterval(keepAliveInterval);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
setWs(newWebSocket);
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
if (keepAliveInterval) {
|
|
||||||
clearInterval(keepAliveInterval);
|
|
||||||
}
|
|
||||||
newWebSocket.close();
|
|
||||||
};
|
|
||||||
}, [api, deviceId]);
|
|
||||||
|
|
||||||
const discoverServers = async (url: string): Promise<Server[]> => {
|
const discoverServers = async (url: string): Promise<Server[]> => {
|
||||||
const servers = await jellyfin?.discovery.getRecommendedServerCandidates(
|
const servers = await jellyfin?.discovery.getRecommendedServerCandidates(
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import React, {
|
|||||||
ReactNode,
|
ReactNode,
|
||||||
useCallback,
|
useCallback,
|
||||||
useContext,
|
useContext,
|
||||||
|
useEffect,
|
||||||
useRef,
|
useRef,
|
||||||
useState,
|
useState,
|
||||||
} from "react";
|
} from "react";
|
||||||
@@ -19,6 +20,7 @@ import { getMediaInfoApi } from "@jellyfin/sdk/lib/utils/api";
|
|||||||
import { useAtom } from "jotai";
|
import { useAtom } from "jotai";
|
||||||
import { OnProgressData, type VideoRef } from "react-native-video";
|
import { OnProgressData, type VideoRef } from "react-native-video";
|
||||||
import { apiAtom, userAtom } from "./JellyfinProvider";
|
import { apiAtom, userAtom } from "./JellyfinProvider";
|
||||||
|
import { getDeviceId } from "@/utils/device";
|
||||||
|
|
||||||
type CurrentlyPlayingState = {
|
type CurrentlyPlayingState = {
|
||||||
url: string;
|
url: string;
|
||||||
@@ -34,7 +36,7 @@ interface PlaybackContextType {
|
|||||||
progressTicks: number | null;
|
progressTicks: number | null;
|
||||||
playVideo: () => void;
|
playVideo: () => void;
|
||||||
pauseVideo: () => void;
|
pauseVideo: () => void;
|
||||||
stopVideo: () => void;
|
stopPlayback: () => void;
|
||||||
presentFullscreenPlayer: () => void;
|
presentFullscreenPlayer: () => void;
|
||||||
dismissFullscreenPlayer: () => void;
|
dismissFullscreenPlayer: () => void;
|
||||||
setIsFullscreen: (isFullscreen: boolean) => void;
|
setIsFullscreen: (isFullscreen: boolean) => void;
|
||||||
@@ -63,6 +65,10 @@ export const PlaybackProvider: React.FC<{ children: ReactNode }> = ({
|
|||||||
const [currentlyPlaying, setCurrentlyPlaying] =
|
const [currentlyPlaying, setCurrentlyPlaying] =
|
||||||
useState<CurrentlyPlayingState | null>(null);
|
useState<CurrentlyPlayingState | null>(null);
|
||||||
|
|
||||||
|
// WS
|
||||||
|
const [ws, setWs] = useState<WebSocket | null>(null);
|
||||||
|
const [isConnected, setIsConnected] = useState(false);
|
||||||
|
|
||||||
const { data: sessionData } = useQuery({
|
const { data: sessionData } = useQuery({
|
||||||
queryKey: ["sessionData", currentlyPlaying?.item.Id, user?.Id, api],
|
queryKey: ["sessionData", currentlyPlaying?.item.Id, user?.Id, api],
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
@@ -76,6 +82,11 @@ export const PlaybackProvider: React.FC<{ children: ReactNode }> = ({
|
|||||||
enabled: !!currentlyPlaying?.item.Id && !!api && !!user?.Id,
|
enabled: !!currentlyPlaying?.item.Id && !!api && !!user?.Id,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const { data: deviceId } = useQuery({
|
||||||
|
queryKey: ["deviceId", api],
|
||||||
|
queryFn: getDeviceId,
|
||||||
|
});
|
||||||
|
|
||||||
const setCurrentlyPlayingState = (state: CurrentlyPlayingState | null) => {
|
const setCurrentlyPlayingState = (state: CurrentlyPlayingState | null) => {
|
||||||
if (state) {
|
if (state) {
|
||||||
setCurrentlyPlaying(state);
|
setCurrentlyPlaying(state);
|
||||||
@@ -120,14 +131,15 @@ export const PlaybackProvider: React.FC<{ children: ReactNode }> = ({
|
|||||||
});
|
});
|
||||||
}, [sessionData?.PlaySessionId, currentlyPlaying?.item.Id, progressTicks]);
|
}, [sessionData?.PlaySessionId, currentlyPlaying?.item.Id, progressTicks]);
|
||||||
|
|
||||||
const stopVideo = useCallback(() => {
|
const stopPlayback = useCallback(async () => {
|
||||||
reportPlaybackStopped({
|
await reportPlaybackStopped({
|
||||||
api,
|
api,
|
||||||
itemId: currentlyPlaying?.item?.Id,
|
itemId: currentlyPlaying?.item?.Id,
|
||||||
sessionId: sessionData?.PlaySessionId,
|
sessionId: sessionData?.PlaySessionId,
|
||||||
positionTicks: progressTicks ? progressTicks : 0,
|
positionTicks: progressTicks ? progressTicks : 0,
|
||||||
});
|
});
|
||||||
}, [currentlyPlaying?.item?.Id, sessionData?.PlaySessionId, progressTicks]);
|
setCurrentlyPlayingState(null);
|
||||||
|
}, [currentlyPlaying, sessionData, progressTicks]);
|
||||||
|
|
||||||
const onProgress = useCallback(
|
const onProgress = useCallback(
|
||||||
({ currentTime }: OnProgressData) => {
|
({ currentTime }: OnProgressData) => {
|
||||||
@@ -154,6 +166,73 @@ export const PlaybackProvider: React.FC<{ children: ReactNode }> = ({
|
|||||||
setIsFullscreen(false);
|
setIsFullscreen(false);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!deviceId || !api) return;
|
||||||
|
|
||||||
|
const url = `wss://${api?.basePath
|
||||||
|
.replace("https://", "")
|
||||||
|
.replace("http://", "")}/socket?api_key=${
|
||||||
|
api?.accessToken
|
||||||
|
}&deviceId=${deviceId}`;
|
||||||
|
|
||||||
|
console.log("WS", url);
|
||||||
|
|
||||||
|
const newWebSocket = new WebSocket(url);
|
||||||
|
|
||||||
|
let keepAliveInterval: NodeJS.Timeout | null = null;
|
||||||
|
|
||||||
|
newWebSocket.onopen = () => {
|
||||||
|
setIsConnected(true);
|
||||||
|
// Start sending "KeepAlive" message every 30 seconds
|
||||||
|
keepAliveInterval = setInterval(() => {
|
||||||
|
if (newWebSocket.readyState === WebSocket.OPEN) {
|
||||||
|
newWebSocket.send(JSON.stringify({ MessageType: "KeepAlive" }));
|
||||||
|
console.log("KeepAlive message sent");
|
||||||
|
}
|
||||||
|
}, 30000);
|
||||||
|
};
|
||||||
|
|
||||||
|
newWebSocket.onerror = (e) => {
|
||||||
|
console.error("WebSocket error:", e);
|
||||||
|
setIsConnected(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
newWebSocket.onclose = (e) => {
|
||||||
|
console.log("WebSocket connection closed:", e.reason);
|
||||||
|
if (keepAliveInterval) {
|
||||||
|
clearInterval(keepAliveInterval);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
setWs(newWebSocket);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
if (keepAliveInterval) {
|
||||||
|
clearInterval(keepAliveInterval);
|
||||||
|
}
|
||||||
|
newWebSocket.close();
|
||||||
|
};
|
||||||
|
}, [api, deviceId]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!ws) return;
|
||||||
|
|
||||||
|
ws.onmessage = (e) => {
|
||||||
|
const json = JSON.parse(e.data);
|
||||||
|
const command = json?.Data?.Command;
|
||||||
|
|
||||||
|
// On PlayPause
|
||||||
|
if (command === "PlayPause") {
|
||||||
|
console.log("Command ~ PlayPause");
|
||||||
|
if (isPlaying) pauseVideo();
|
||||||
|
else playVideo();
|
||||||
|
} else if (command === "Stop") {
|
||||||
|
console.log("Command ~ Stop");
|
||||||
|
stopPlayback();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}, [ws, stopPlayback, playVideo, pauseVideo]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<PlaybackContext.Provider
|
<PlaybackContext.Provider
|
||||||
value={{
|
value={{
|
||||||
@@ -169,7 +248,7 @@ export const PlaybackProvider: React.FC<{ children: ReactNode }> = ({
|
|||||||
playVideo,
|
playVideo,
|
||||||
setCurrentlyPlayingState,
|
setCurrentlyPlayingState,
|
||||||
pauseVideo,
|
pauseVideo,
|
||||||
stopVideo,
|
stopPlayback,
|
||||||
presentFullscreenPlayer,
|
presentFullscreenPlayer,
|
||||||
dismissFullscreenPlayer,
|
dismissFullscreenPlayer,
|
||||||
}}
|
}}
|
||||||
|
|||||||
19
utils/device.ts
Normal file
19
utils/device.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||||
|
import uuid from "react-native-uuid";
|
||||||
|
|
||||||
|
export const getOrSetDeviceId = async () => {
|
||||||
|
let deviceId = await AsyncStorage.getItem("deviceId");
|
||||||
|
|
||||||
|
if (!deviceId) {
|
||||||
|
deviceId = uuid.v4() as string;
|
||||||
|
await AsyncStorage.setItem("deviceId", deviceId);
|
||||||
|
}
|
||||||
|
|
||||||
|
return deviceId;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getDeviceId = async () => {
|
||||||
|
let deviceId = await AsyncStorage.getItem("deviceId");
|
||||||
|
|
||||||
|
return deviceId || null;
|
||||||
|
};
|
||||||
@@ -24,7 +24,12 @@ export const reportPlaybackProgress = async ({
|
|||||||
IsPaused = false,
|
IsPaused = false,
|
||||||
}: ReportPlaybackProgressParams): Promise<void> => {
|
}: ReportPlaybackProgressParams): Promise<void> => {
|
||||||
if (!api || !sessionId || !itemId || !positionTicks) {
|
if (!api || !sessionId || !itemId || !positionTicks) {
|
||||||
console.error("Missing required parameter");
|
console.error(
|
||||||
|
"Missing required parameter",
|
||||||
|
sessionId,
|
||||||
|
itemId,
|
||||||
|
positionTicks
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user