Fix auth flow for livekit
This commit is contained in:
@@ -1498,10 +1498,10 @@ function disconnect(): void {
|
|||||||
/** Connects to the LiveKit room and ensures peers exist for roster members. */
|
/** Connects to the LiveKit room and ensures peers exist for roster members. */
|
||||||
async function connectLiveKit(url: string, token: string): Promise<void> {
|
async function connectLiveKit(url: string, token: string): Promise<void> {
|
||||||
try {
|
try {
|
||||||
await peerManager.connectToRoom(url, token);
|
|
||||||
for (const peer of state.peers.values()) {
|
for (const peer of state.peers.values()) {
|
||||||
peerManager.ensurePeer(peer.id, peer);
|
peerManager.ensurePeer(peer.id, peer);
|
||||||
}
|
}
|
||||||
|
await peerManager.connectToRoom(url, token);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('LiveKit connect failed:', error);
|
console.error('LiveKit connect failed:', error);
|
||||||
updateStatus('LiveKit connection failed.');
|
updateStatus('LiveKit connection failed.');
|
||||||
|
|||||||
@@ -162,20 +162,21 @@ export function createOnMessageHandler(deps: MessageHandlerDeps): (message: Inco
|
|||||||
deps.state.player.y = message.y;
|
deps.state.player.y = message.y;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
const peer = deps.state.peers.get(message.id);
|
let peer = deps.state.peers.get(message.id);
|
||||||
const prevX = peer?.x ?? message.x;
|
if (!peer) {
|
||||||
const prevY = peer?.y ?? message.y;
|
peer = { id: message.id, nickname: 'user...', x: message.x, y: message.y };
|
||||||
if (peer) {
|
deps.state.peers.set(message.id, peer);
|
||||||
|
deps.peerManager.ensurePeer(message.id, peer);
|
||||||
|
}
|
||||||
|
const prevX = peer.x;
|
||||||
|
const prevY = peer.y;
|
||||||
peer.x = message.x;
|
peer.x = message.x;
|
||||||
peer.y = message.y;
|
peer.y = message.y;
|
||||||
}
|
|
||||||
deps.peerManager.setPeerPosition(message.id, message.x, message.y);
|
deps.peerManager.setPeerPosition(message.id, message.x, message.y);
|
||||||
if (peer) {
|
|
||||||
const movementDelta = Math.hypot(message.x - prevX, message.y - prevY);
|
const movementDelta = Math.hypot(message.x - prevX, message.y - prevY);
|
||||||
if (movementDelta <= 1.5 && deps.getAudioLayers().world) {
|
if (movementDelta <= 1.5 && deps.getAudioLayers().world) {
|
||||||
deps.playRemoteSpatialStepOrTeleport(deps.randomFootstepUrl(), peer.x, peer.y);
|
deps.playRemoteSpatialStepOrTeleport(deps.randomFootstepUrl(), peer.x, peer.y);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ export class PeerManager {
|
|||||||
private outputDeviceId = '';
|
private outputDeviceId = '';
|
||||||
private room: Room | null = null;
|
private room: Room | null = null;
|
||||||
private localTrack: LocalAudioTrack | null = null;
|
private localTrack: LocalAudioTrack | null = null;
|
||||||
|
private pendingOutboundStream: MediaStream | null = null;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly audio: AudioEngine,
|
private readonly audio: AudioEngine,
|
||||||
@@ -100,6 +101,11 @@ export class PeerManager {
|
|||||||
|
|
||||||
await room.connect(url, token);
|
await room.connect(url, token);
|
||||||
this.room = room;
|
this.room = room;
|
||||||
|
if (this.pendingOutboundStream) {
|
||||||
|
const pending = this.pendingOutboundStream;
|
||||||
|
this.pendingOutboundStream = null;
|
||||||
|
await this.replaceOutgoingTrack(pending);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Ensure a peer entry exists for a given user (called when roster arrives). */
|
/** Ensure a peer entry exists for a given user (called when roster arrives). */
|
||||||
@@ -124,7 +130,11 @@ export class PeerManager {
|
|||||||
const newTrack = stream.getAudioTracks()[0];
|
const newTrack = stream.getAudioTracks()[0];
|
||||||
if (!newTrack) return;
|
if (!newTrack) return;
|
||||||
|
|
||||||
if (!this.room) return;
|
if (!this.room) {
|
||||||
|
this.pendingOutboundStream = stream;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.pendingOutboundStream = null;
|
||||||
|
|
||||||
if (this.localTrack) {
|
if (this.localTrack) {
|
||||||
// Replace the underlying MediaStreamTrack on the existing LiveKit track.
|
// Replace the underlying MediaStreamTrack on the existing LiveKit track.
|
||||||
@@ -159,6 +169,7 @@ export class PeerManager {
|
|||||||
this.room = null;
|
this.room = null;
|
||||||
}
|
}
|
||||||
this.localTrack = null;
|
this.localTrack = null;
|
||||||
|
this.pendingOutboundStream = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
setPeerPosition(id: string, x: number, y: number): void {
|
setPeerPosition(id: string, x: number, y: number): void {
|
||||||
@@ -215,8 +226,13 @@ export class PeerManager {
|
|||||||
if (!mediaStreamTrack) return;
|
if (!mediaStreamTrack) return;
|
||||||
|
|
||||||
const stream = new MediaStream([mediaStreamTrack]);
|
const stream = new MediaStream([mediaStreamTrack]);
|
||||||
const peer = this.peers.get(participant.identity);
|
let peer = this.peers.get(participant.identity);
|
||||||
if (!peer) return;
|
if (!peer) {
|
||||||
|
peer = this.ensurePeer(participant.identity, {
|
||||||
|
id: participant.identity,
|
||||||
|
nickname: participant.name ?? 'user...',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
peer.remoteStream = stream;
|
peer.remoteStream = stream;
|
||||||
if (this.audio.isVoiceLayerEnabled()) {
|
if (this.audio.isVoiceLayerEnabled()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user