Skip to content

Commit

Permalink
Retrieve ice candidates
Browse files Browse the repository at this point in the history
This was hard! 😓

Breaking on caught exceptions helped
  • Loading branch information
benfoxall committed Sep 22, 2024
1 parent e816b87 commit e1afb4e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
17 changes: 10 additions & 7 deletions src/components/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,16 @@ export const Editor: FC<{

_SET_MONACO(mon);

new MonacoBinding(
type,
model,
new Set([editor]),
yconfig.provider.awareness,
);

yconfig.provider.then(provider => {
new MonacoBinding(
type,
model,
new Set([editor]),
provider.awareness,
);
})


editor.setModel(model);

return () => {
Expand Down
32 changes: 28 additions & 4 deletions src/yconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if (import.meta.hot) {

interface YI {
doc: Y.Doc;
provider: WebrtcProvider;
provider: Promise<WebrtcProvider>;
room: string;
initiator: boolean;
}
Expand All @@ -34,9 +34,33 @@ const doc = new Y.Doc();

const [name, password] = room.split('~');

// @ts-expect-error
// WebrtcProvider expects full Opts object, though it seems that Partial<Opts> works okay
const provider = new WebrtcProvider(name, doc, { password, signaling: ['wss://y-ben.fly.dev'] });
const provider = fetch('https://nice.benfoxall.workers.dev/')
.then((res) => res.json())
.then((config) => {
// correct iterable ice servers
if (config?.iceServers && !Array.isArray(config?.iceServers)) {
console.log('correcting ice servers');
config.iceServers = [config.iceServers];
}

// nest for peerOpts
return config;
})
.catch(() => undefined)
.then((config) => {
// @ts-expect-error
// WebrtcProvider expects full Opts object, though it seems that Partial<Opts> works okay
const provider = new WebrtcProvider(name, doc, {
password: null,
signaling: ['wss://y-ben.fly.dev'],
filterBcConns: false,
peerOpts: config ? { config } : undefined,
});

console.log('INIT PROVIDER', provider);

return provider;
});

const config: YI = { room, doc, provider, initiator };

Expand Down

0 comments on commit e1afb4e

Please sign in to comment.