Newer
Older
import LioWebRTC from "liowebrtc"
function extend(Y) {
class WebRTC extends Y.AbstractConnector {
constructor(y, options) {
if (options === undefined) {
throw new Error("Options must not be undefined!")
}
options.role = "slave"
super(y, options)
this.webrtcOptions = options
if (options.onUserEvent) {
this.onUserEvent(options.onUserEvent)
}
this.initialiseConnection()
Momo Langenstein
committed
window.addEventListener("unload", () => {
this.y.destroy()
})
initialiseConnection() {
Momo Langenstein
committed
this.webrtc = new LioWebRTC({
url: this.webrtcOptions.url,
dataOnly: true,
Moritz Langenstein
committed
/*network: {
minPeers: 4,
maxPeers: 8,
Moritz Langenstein
committed
},*/
Momo Langenstein
committed
this.peers = new Set()
Momo Langenstein
committed
this.webrtc.on("ready", () => {
this.webrtc.joinRoom(this.webrtcOptions.room)
})
Moritz Langenstein
committed
Momo Langenstein
committed
this.webrtc.on("joinedRoom", () => {
this.checkAndEnsureUser()
Momo Langenstein
committed
this.webrtc.on("channelOpen", (dataChannel, peer) => {
this.checkAndEnsureUser()
// Start a handshake to ensure both sides are able to use the channel
function handshake(peer) {
const _peer = this.webrtc.getPeerById(peer.id)
if (!_peer || _peer !== peer) {
return
}
Momo Langenstein
committed
if (this.peers.has(peer.id)) {
return
}
console.log("ping", peer.id)
// Initial message in the handshake
this.webrtc.whisper(peer, "tw-ml", "tw")
setTimeout(handshake.bind(this, peer), 500)
Momo Langenstein
committed
setTimeout(handshake.bind(this, peer), 100)
})
Momo Langenstein
committed
this.webrtc.on("receivedPeerData", (type, message, peer) => {
this.checkAndEnsureUser()
Momo Langenstein
committed
if (message.type !== "update") {
console.log("receivedData", peer.id, message)
}
if (type === "y-js") {
this.checkAndInsertPeer(peer.id)
this.receiveMessage(peer.id, message)
} else if (type === "tw-ml") {
if (message === "tw") {
// Response message in the handshake
this.webrtc.whisper(peer, "tw-ml", "ml")
} else if (message == "ml") {
// Handshake completed
this.checkAndInsertPeer(peer.id)
}
}
})
Momo Langenstein
committed
this.webrtc.on("channelClose", (dataChannel, peer) => {
this.checkAndEnsureUser()
this.checkAndRemovePeer(peer.id)
Momo Langenstein
committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Ensure that y-js is up to date on the user's id
checkAndEnsureUser() {
const id = this.webrtc.getMyId()
if (this.y.db.userId === id) {
return
}
for (let f of this.userEventListeners) {
f({ action: "userID", id: id })
}
this.setUserId(id)
}
// Ensure that y-js knows that the peer has joined
checkAndInsertPeer(uid) {
if (this.peers.has(uid)) {
return
}
this.peers.add(uid)
console.log("createdPeer", uid)
this.userJoined(uid, "master")
}
// Ensure that y-js knows that the peer has left
checkAndRemovePeer(uid) {
if (!this.peers.has(uid)) {
return
}
this.peers.delete(uid)
console.log("removedPeer", uid)
this.userLeft(uid)
}
connectToPeer(/*uid*/) {
// currently deprecated
this.webrtc.quit()
super.disconnect()
}
reconnect() {
this.initialiseConnection()
send(uid, message) {
Momo Langenstein
committed
// y-js db transactions can send messages after a peer has disconnected
if (!this.peers.has(uid) || !this.webrtc.getPeerById(uid)) {
return
}
console.log("send", uid, message)
this.webrtc.whisper(this.webrtc.getPeerById(uid), "y-js", message)
}
broadcast(message) {
Momo Langenstein
committed
if (message.type !== "update") console.log("broadcast", message)
this.webrtc.shout("y-js", message)
isDisconnected() {
return false
}
}
Y.extend("webrtc", WebRTC)
}
if (typeof Y !== "undefined") {
extend(Y)
}