2022-10-01 08:57:34 +00:00
|
|
|
import { uniqueNamesGenerator, adjectives, animals } from 'unique-names-generator'
|
|
|
|
import { Awareness } from 'y-protocols/awareness'
|
|
|
|
import { WebrtcProvider } from 'y-webrtc'
|
2022-10-08 05:24:09 +00:00
|
|
|
import { Doc, XmlFragment } from 'yjs'
|
2022-10-19 16:35:24 +00:00
|
|
|
// import type { Reaction } from '../../../graphql/types.gen'
|
|
|
|
// import { setReactions } from '../../../stores/editor'
|
2022-10-01 08:57:34 +00:00
|
|
|
|
2022-10-19 15:56:29 +00:00
|
|
|
export const roomConnect = (room: string, username = '', keyname = 'collab'): [XmlFragment, WebrtcProvider] => {
|
2022-10-08 05:24:09 +00:00
|
|
|
const ydoc = new Doc()
|
2022-10-19 16:35:24 +00:00
|
|
|
// const yarr = ydoc.getArray(keyname + '-reactions')
|
2022-10-19 15:56:29 +00:00
|
|
|
// TODO: use reactions
|
2022-10-19 16:35:24 +00:00
|
|
|
// yarr.observeDeep(() => {
|
|
|
|
// console.debug('[p2p] yarray updated', yarr.toArray())
|
|
|
|
// setReactions(yarr.toArray() as Reaction[])
|
|
|
|
// })
|
2022-10-07 19:35:53 +00:00
|
|
|
const yXmlFragment = ydoc.getXmlFragment(keyname)
|
2022-10-01 08:57:34 +00:00
|
|
|
const webrtcOptions = {
|
|
|
|
awareness: new Awareness(ydoc),
|
|
|
|
filterBcConns: true,
|
|
|
|
maxConns: 33,
|
|
|
|
signaling: [
|
2022-10-07 19:35:53 +00:00
|
|
|
// 'wss://signaling.discours.io',
|
2022-10-01 08:57:34 +00:00
|
|
|
// 'wss://stun.l.google.com:19302',
|
|
|
|
'wss://y-webrtc-signaling-eu.herokuapp.com',
|
|
|
|
'wss://signaling.yjs.dev'
|
|
|
|
],
|
|
|
|
peerOpts: {},
|
|
|
|
password: ''
|
|
|
|
}
|
2022-10-18 15:50:49 +00:00
|
|
|
// connect with provider
|
2022-10-01 08:57:34 +00:00
|
|
|
const provider = new WebrtcProvider(room, ydoc, webrtcOptions)
|
2022-10-18 15:50:49 +00:00
|
|
|
console.debug('[p2p] provider', provider)
|
|
|
|
// setting username
|
|
|
|
provider.awareness.setLocalStateField('user', {
|
|
|
|
name:
|
|
|
|
username ??
|
|
|
|
uniqueNamesGenerator({
|
|
|
|
dictionaries: [adjectives, animals],
|
|
|
|
style: 'capital',
|
|
|
|
separator: ' ',
|
|
|
|
length: 2
|
|
|
|
})
|
2022-10-07 19:35:53 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return [yXmlFragment, provider]
|
2022-10-01 08:57:34 +00:00
|
|
|
}
|