Removed safari work around for data connection and added cleaner nickname syncing

This commit is contained in:
Mitchell McCaffrey 2020-03-26 19:50:14 +11:00
parent f8d992fa91
commit 7ac13c1145
5 changed files with 25 additions and 47 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "owlbear-rodeo", "name": "owlbear-rodeo",
"version": "0.2.0", "version": "0.2.1",
"private": true, "private": true,
"homepage": "https://owlbear.rodeo", "homepage": "https://owlbear.rodeo",
"dependencies": { "dependencies": {

View File

@ -8,24 +8,3 @@ export function omit(obj, keys) {
} }
return tmp; return tmp;
} }
/**
* This asks for audio permission on safari based devices
* this is needed to fix a implementation detail in safari
* where webRTC data channels cannot be opened without first
* using getUserMedia see:
* https://github.com/webrtc/samples/issues/1123
* https://bugs.webkit.org/show_bug.cgi?id=189503
* https://github.com/w3c/webrtc-nv-use-cases/issues/58
*/
export async function enableDataConnectionForSafari() {
if (
navigator.userAgent.match(/iPad/i) ||
navigator.userAgent.match(/iPhone/i) ||
(navigator.userAgent.match(/Safari/i) &&
!navigator.userAgent.match(/Chrome/i))
) {
let stream = await navigator.mediaDevices.getUserMedia({ audio: true });
stream.getTracks().forEach(t => t.stop());
}
}

View File

@ -1,8 +1,6 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import Peer from "peerjs"; import Peer from "peerjs";
import { enableDataConnectionForSafari } from "./shared";
function useSession(onConnectionOpen, onConnectionSync) { function useSession(onConnectionOpen, onConnectionSync) {
const [peerId, setPeerId] = useState(null); const [peerId, setPeerId] = useState(null);
const [peer, setPeer] = useState(null); const [peer, setPeer] = useState(null);
@ -31,7 +29,6 @@ function useSession(onConnectionOpen, onConnectionSync) {
function handleOpen(id) { function handleOpen(id) {
console.log("Peer open", id); console.log("Peer open", id);
setPeerId(id); setPeerId(id);
enableDataConnectionForSafari();
} }
function handleConnection(connection) { function handleConnection(connection) {
@ -87,7 +84,7 @@ function useSession(onConnectionOpen, onConnectionSync) {
}; };
}, [peer, peerId, connections, onConnectionOpen, onConnectionSync]); }, [peer, peerId, connections, onConnectionOpen, onConnectionSync]);
function connectTo(connectionId) { function connectTo(connectionId, payload) {
console.log("Connecting to", connectionId); console.log("Connecting to", connectionId);
if (connectionId in connections) { if (connectionId in connections) {
return; return;
@ -113,6 +110,9 @@ function useSession(onConnectionOpen, onConnectionSync) {
if (onConnectionOpen) { if (onConnectionOpen) {
onConnectionOpen(syncConnection); onConnectionOpen(syncConnection);
} }
if (payload) {
syncConnection.send(payload);
}
}); });
} }
} }
@ -120,6 +120,9 @@ function useSession(onConnectionOpen, onConnectionSync) {
if (onConnectionOpen) { if (onConnectionOpen) {
onConnectionOpen(connection); onConnectionOpen(connection);
} }
if (payload) {
connection.send(payload);
}
}); });
} }

View File

@ -17,6 +17,8 @@ import Party from "../components/Party";
import Tokens from "../components/Tokens"; import Tokens from "../components/Tokens";
import Map from "../components/Map"; import Map from "../components/Map";
const defaultNickname = getRandomMonster();
function Game() { function Game() {
const { gameId } = useContext(GameContext); const { gameId } = useContext(GameContext);
const handleConnectionOpenCallback = useCallback(handleConnectionOpen); const handleConnectionOpenCallback = useCallback(handleConnectionOpen);
@ -26,11 +28,20 @@ function Game() {
handleConnectionSyncCallback handleConnectionSyncCallback
); );
const [nicknames, setNicknames] = useState({});
useEffect(() => { useEffect(() => {
if (gameId !== null && peerId !== null && !(gameId in connections)) { // Initialize nickname state
connectTo(gameId); if (peerId !== null && !(peerId in nicknames)) {
setNicknames({ [peerId]: defaultNickname });
} }
}, [gameId, peerId, connectTo, connections]); if (gameId !== null && peerId !== null && !(gameId in connections)) {
connectTo(gameId, {
id: "nickname",
data: { [peerId]: nicknames[peerId] || defaultNickname }
});
}
}, [gameId, peerId, connectTo, connections, nicknames]);
const [mapSource, setMapSource] = useState(null); const [mapSource, setMapSource] = useState(null);
const mapDataRef = useRef(null); const mapDataRef = useRef(null);
@ -70,10 +81,7 @@ function Game() {
} }
} }
const currentNicknameRef = useRef(getRandomMonster());
const [nicknames, setNicknames] = useState({});
function handleNicknameChange(nickname) { function handleNicknameChange(nickname) {
currentNicknameRef.current = nickname;
setNicknames(prevNicknames => ({ setNicknames(prevNicknames => ({
...prevNicknames, ...prevNicknames,
[peerId]: nickname [peerId]: nickname
@ -83,15 +91,6 @@ function Game() {
connection.send({ id: "nickname", data }); connection.send({ id: "nickname", data });
} }
} }
useEffect(() => {
// If we don't have a nickname generate one when we have a peer
if (peerId !== null && !(peerId in nicknames)) {
setNicknames(prevNicknames => ({
...prevNicknames,
[peerId]: currentNicknameRef.current
}));
}
}, [peerId, nicknames, currentNicknameRef]);
function handleConnectionOpen(connection) { function handleConnectionOpen(connection) {
connection.on("data", data => { connection.on("data", data => {
@ -118,13 +117,10 @@ function Game() {
})); }));
} }
}); });
connection.on("error", () => { connection.on("error", error => {
console.error("Data Connection error", error);
setNicknames(prevNicknames => omit(prevNicknames, [connection.peer])); setNicknames(prevNicknames => omit(prevNicknames, [connection.peer]));
}); });
connection.send({
id: "nickname",
data: { [peerId]: currentNicknameRef.current }
});
} }
function handleConnectionSync(connection) { function handleConnectionSync(connection) {

View File

@ -38,7 +38,7 @@ function Home() {
Join Game Join Game
</Button> </Button>
<Text variant="caption" sx={{ textAlign: "center" }}> <Text variant="caption" sx={{ textAlign: "center" }}>
Alpha v0.2.0 Alpha v0.2.1
</Text> </Text>
</Flex> </Flex>
</Container> </Container>