James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 1 | import {Builder, ByteBuffer, Offset} from 'flatbuffers'; |
| 2 | import {Channel as ChannelFb, Configuration} from 'org_frc971/aos/configuration_generated'; |
| 3 | import {ChannelRequest as ChannelRequestFb, ChannelState, MessageHeader, Payload, SdpType, SubscriberRequest, TransferMethod, WebSocketIce, WebSocketMessage, WebSocketSdp} from 'org_frc971/aos/network/web_proxy_generated'; |
James Kuszmaul | 136aa2b | 2022-04-02 14:50:56 -0700 | [diff] [blame] | 4 | import {Schema} from 'flatbuffers_reflection/reflection_generated'; |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 5 | |
| 6 | // There is one handler for each DataChannel, it maintains the state of |
| 7 | // multi-part messages and delegates to a callback when the message is fully |
| 8 | // assembled. |
| 9 | export class Handler { |
| 10 | private dataBuffer: Uint8Array|null = null; |
| 11 | private receivedMessageLength: number = 0; |
| 12 | constructor( |
James Kuszmaul | 48413bf | 2020-09-01 19:19:05 -0700 | [diff] [blame] | 13 | private readonly handlerFunc: |
| 14 | (data: Uint8Array, sentTime: number) => void, |
Philipp Schrader | 47445a0 | 2020-11-14 17:31:04 -0800 | [diff] [blame] | 15 | private readonly channel: RTCDataChannel) { |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 16 | channel.addEventListener('message', (e) => this.handleMessage(e)); |
| 17 | } |
| 18 | |
| 19 | handleMessage(e: MessageEvent): void { |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 20 | const fbBuffer = new ByteBuffer(new Uint8Array(e.data)); |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 21 | const messageHeader = MessageHeader.getRootAsMessageHeader(fbBuffer); |
| 22 | const time = Number(messageHeader.monotonicSentTime()) * 1e-9; |
James Kuszmaul | a582268 | 2021-12-23 18:39:28 -0800 | [diff] [blame] | 23 | |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 24 | const stateBuilder = new Builder(512); |
James Kuszmaul | a582268 | 2021-12-23 18:39:28 -0800 | [diff] [blame] | 25 | ChannelState.startChannelState(stateBuilder); |
| 26 | ChannelState.addQueueIndex(stateBuilder, messageHeader.queueIndex()); |
| 27 | ChannelState.addPacketIndex(stateBuilder, messageHeader.packetIndex()); |
| 28 | const state = ChannelState.endChannelState(stateBuilder); |
| 29 | stateBuilder.finish(state); |
| 30 | const stateArray = stateBuilder.asUint8Array(); |
| 31 | this.channel.send(stateArray); |
| 32 | |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 33 | // Short circuit if only one packet |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 34 | if (messageHeader.packetCount() === 1) { |
James Kuszmaul | 48413bf | 2020-09-01 19:19:05 -0700 | [diff] [blame] | 35 | this.handlerFunc(messageHeader.dataArray(), time); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 36 | return; |
| 37 | } |
| 38 | |
| 39 | if (messageHeader.packetIndex() === 0) { |
| 40 | this.dataBuffer = new Uint8Array(messageHeader.length()); |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 41 | this.receivedMessageLength = 0; |
| 42 | } |
| 43 | if (!messageHeader.dataLength()) { |
| 44 | return; |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 45 | } |
| 46 | this.dataBuffer.set( |
| 47 | messageHeader.dataArray(), |
| 48 | this.receivedMessageLength); |
| 49 | this.receivedMessageLength += messageHeader.dataLength(); |
| 50 | |
| 51 | if (messageHeader.packetIndex() === messageHeader.packetCount() - 1) { |
James Kuszmaul | 48413bf | 2020-09-01 19:19:05 -0700 | [diff] [blame] | 52 | this.handlerFunc(this.dataBuffer, time); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | } |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 56 | |
James Kuszmaul | 527038a | 2020-12-21 23:40:44 -0800 | [diff] [blame] | 57 | class Channel { |
| 58 | constructor(public readonly name: string, public readonly type: string) {} |
| 59 | key(): string { |
| 60 | return this.name + "/" + this.type; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | class ChannelRequest { |
| 65 | constructor( |
| 66 | public readonly channel: Channel, |
| 67 | public readonly transferMethod: TransferMethod) {} |
| 68 | } |
| 69 | |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 70 | // Analogous to the Connection class in //aos/network/web_proxy.h. Because most |
| 71 | // of the apis are native in JS, it is much simpler. |
| 72 | export class Connection { |
| 73 | private webSocketConnection: WebSocket|null = null; |
| 74 | private rtcPeerConnection: RTCPeerConnection|null = null; |
Philipp Schrader | 47445a0 | 2020-11-14 17:31:04 -0800 | [diff] [blame] | 75 | private dataChannel: RTCDataChannel|null = null; |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 76 | private webSocketUrl: string; |
Alex Perry | 6249aaf | 2020-02-29 14:51:49 -0800 | [diff] [blame] | 77 | |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 78 | private configInternal: Configuration|null = null; |
Alex Perry | 6249aaf | 2020-02-29 14:51:49 -0800 | [diff] [blame] | 79 | // A set of functions that accept the config to handle. |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 80 | private readonly configHandlers = new Set<(config: Configuration) => void>(); |
Alex Perry | 6249aaf | 2020-02-29 14:51:49 -0800 | [diff] [blame] | 81 | |
James Kuszmaul | 48413bf | 2020-09-01 19:19:05 -0700 | [diff] [blame] | 82 | private readonly handlerFuncs = |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 83 | new Map<string, ((data: Uint8Array, sentTime: number) => void)[]>(); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 84 | private readonly handlers = new Set<Handler>(); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 85 | |
James Kuszmaul | 527038a | 2020-12-21 23:40:44 -0800 | [diff] [blame] | 86 | private subscribedChannels: ChannelRequest[] = []; |
| 87 | |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 88 | constructor() { |
| 89 | const server = location.host; |
| 90 | this.webSocketUrl = `ws://${server}/ws`; |
| 91 | } |
| 92 | |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 93 | addConfigHandler(handler: (config: Configuration) => void): void { |
Alex Perry | 6249aaf | 2020-02-29 14:51:49 -0800 | [diff] [blame] | 94 | this.configHandlers.add(handler); |
| 95 | } |
| 96 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 97 | /** |
James Kuszmaul | 527038a | 2020-12-21 23:40:44 -0800 | [diff] [blame] | 98 | * Add a handler for a specific message type, with reliable delivery of all |
| 99 | * messages. |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 100 | */ |
James Kuszmaul | 527038a | 2020-12-21 23:40:44 -0800 | [diff] [blame] | 101 | addReliableHandler( |
| 102 | name: string, type: string, |
| 103 | handler: (data: Uint8Array, sentTime: number) => void): void { |
| 104 | this.addHandlerImpl( |
James Kuszmaul | 1a29c08 | 2022-02-03 14:02:47 -0800 | [diff] [blame] | 105 | name, type, TransferMethod.LOSSLESS, handler); |
James Kuszmaul | 527038a | 2020-12-21 23:40:44 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Add a handler for a specific message type. |
| 110 | */ |
| 111 | addHandler( |
| 112 | name: string, type: string, |
| 113 | handler: (data: Uint8Array, sentTime: number) => void): void { |
| 114 | this.addHandlerImpl(name, type, TransferMethod.SUBSAMPLE, handler); |
| 115 | } |
| 116 | |
| 117 | addHandlerImpl( |
| 118 | name: string, type: string, method: TransferMethod, |
| 119 | handler: (data: Uint8Array, sentTime: number) => void): void { |
| 120 | const channel = new Channel(name, type); |
| 121 | const request = new ChannelRequest(channel, method); |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 122 | if (!this.handlerFuncs.has(channel.key())) { |
| 123 | this.handlerFuncs.set(channel.key(), []); |
James Kuszmaul | c4ae11c | 2020-12-26 16:26:58 -0800 | [diff] [blame] | 124 | } else { |
James Kuszmaul | 1a29c08 | 2022-02-03 14:02:47 -0800 | [diff] [blame] | 125 | if (method == TransferMethod.LOSSLESS) { |
James Kuszmaul | c4ae11c | 2020-12-26 16:26:58 -0800 | [diff] [blame] | 126 | console.warn( |
| 127 | 'Behavior of multiple reliable handlers is currently poorly ' + |
| 128 | 'defined and may not actually deliver all of the messages.'); |
| 129 | } |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 130 | } |
| 131 | this.handlerFuncs.get(channel.key()).push(handler); |
James Kuszmaul | 527038a | 2020-12-21 23:40:44 -0800 | [diff] [blame] | 132 | this.subscribeToChannel(request); |
| 133 | } |
| 134 | |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 135 | getSchema(typeName: string): Schema { |
| 136 | let schema = null; |
| 137 | const config = this.getConfig(); |
| 138 | for (let ii = 0; ii < config.channelsLength(); ++ii) { |
| 139 | if (config.channels(ii).type() === typeName) { |
| 140 | schema = config.channels(ii).schema(); |
| 141 | } |
| 142 | } |
| 143 | if (schema === null) { |
| 144 | throw new Error('Unable to find schema for ' + typeName); |
| 145 | } |
| 146 | return schema; |
| 147 | } |
| 148 | |
James Kuszmaul | 527038a | 2020-12-21 23:40:44 -0800 | [diff] [blame] | 149 | subscribeToChannel(channel: ChannelRequest): void { |
| 150 | this.subscribedChannels.push(channel); |
| 151 | if (this.configInternal === null) { |
| 152 | throw new Error( |
| 153 | 'Must call subscribeToChannel after we\'ve received the config.'); |
| 154 | } |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 155 | const builder = new Builder(512); |
| 156 | const channels: Offset[] = []; |
James Kuszmaul | 527038a | 2020-12-21 23:40:44 -0800 | [diff] [blame] | 157 | for (const channel of this.subscribedChannels) { |
| 158 | const nameFb = builder.createString(channel.channel.name); |
| 159 | const typeFb = builder.createString(channel.channel.type); |
| 160 | ChannelFb.startChannel(builder); |
| 161 | ChannelFb.addName(builder, nameFb); |
| 162 | ChannelFb.addType(builder, typeFb); |
| 163 | const channelFb = ChannelFb.endChannel(builder); |
| 164 | ChannelRequestFb.startChannelRequest(builder); |
| 165 | ChannelRequestFb.addChannel(builder, channelFb); |
| 166 | ChannelRequestFb.addMethod(builder, channel.transferMethod); |
| 167 | channels.push(ChannelRequestFb.endChannelRequest(builder)); |
| 168 | } |
| 169 | |
| 170 | const channelsFb = |
| 171 | SubscriberRequest.createChannelsToTransferVector(builder, channels); |
| 172 | SubscriberRequest.startSubscriberRequest(builder); |
| 173 | SubscriberRequest.addChannelsToTransfer(builder, channelsFb); |
| 174 | const connect = SubscriberRequest.endSubscriberRequest(builder); |
| 175 | builder.finish(connect); |
| 176 | this.sendConnectMessage(builder); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 179 | connect(): void { |
| 180 | this.webSocketConnection = new WebSocket(this.webSocketUrl); |
| 181 | this.webSocketConnection.binaryType = 'arraybuffer'; |
| 182 | this.webSocketConnection.addEventListener( |
| 183 | 'open', () => this.onWebSocketOpen()); |
| 184 | this.webSocketConnection.addEventListener( |
| 185 | 'message', (e) => this.onWebSocketMessage(e)); |
| 186 | } |
| 187 | |
Alex Perry | 3dfcb81 | 2020-03-04 19:32:17 -0800 | [diff] [blame] | 188 | getConfig() { |
Philipp Schrader | 47445a0 | 2020-11-14 17:31:04 -0800 | [diff] [blame] | 189 | return this.configInternal; |
Alex Perry | 6249aaf | 2020-02-29 14:51:49 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 192 | // Handle messages on the DataChannel. Handles the Configuration message as |
| 193 | // all other messages are sent on specific DataChannels. |
James Kuszmaul | 1ec7443 | 2020-07-30 20:26:45 -0700 | [diff] [blame] | 194 | onConfigMessage(data: Uint8Array): void { |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 195 | const fbBuffer = new ByteBuffer(data); |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 196 | this.configInternal = Configuration.getRootAsConfiguration(fbBuffer); |
Alex Perry | 3dfcb81 | 2020-03-04 19:32:17 -0800 | [diff] [blame] | 197 | for (const handler of Array.from(this.configHandlers)) { |
Alex Perry | 6249aaf | 2020-02-29 14:51:49 -0800 | [diff] [blame] | 198 | handler(this.configInternal); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | onDataChannel(ev: RTCDataChannelEvent): void { |
| 203 | const channel = ev.channel; |
| 204 | const name = channel.label; |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 205 | const handlers = this.handlerFuncs.get(name); |
| 206 | for (const handler of handlers) { |
| 207 | this.handlers.add(new Handler(handler, channel)); |
| 208 | } |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | onIceCandidate(e: RTCPeerConnectionIceEvent): void { |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 212 | if (!e.candidate) { |
| 213 | return; |
| 214 | } |
| 215 | const candidate = e.candidate; |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 216 | const builder = new Builder(512); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 217 | const candidateString = builder.createString(candidate.candidate); |
| 218 | const sdpMidString = builder.createString(candidate.sdpMid); |
| 219 | |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 220 | const iceFb = WebSocketIce.createWebSocketIce( |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 221 | builder, candidateString, sdpMidString, candidate.sdpMLineIndex); |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 222 | const messageFb = WebSocketMessage.createWebSocketMessage( |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 223 | builder, Payload.WebSocketIce, iceFb); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 224 | builder.finish(messageFb); |
| 225 | const array = builder.asUint8Array(); |
| 226 | this.webSocketConnection.send(array.buffer.slice(array.byteOffset)); |
| 227 | } |
| 228 | |
Philipp Schrader | 87277f4 | 2022-01-01 07:45:12 -0800 | [diff] [blame] | 229 | onIceCandidateError(e: Event): void { |
James Kuszmaul | 54424d0 | 2020-12-26 18:09:20 -0800 | [diff] [blame] | 230 | console.warn(e); |
| 231 | } |
| 232 | |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 233 | // Called for new SDPs. Make sure to set it locally and remotely. |
Philipp Schrader | 47445a0 | 2020-11-14 17:31:04 -0800 | [diff] [blame] | 234 | onOfferCreated(description: RTCSessionDescriptionInit): void { |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 235 | this.rtcPeerConnection.setLocalDescription(description); |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 236 | const builder = new Builder(512); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 237 | const offerString = builder.createString(description.sdp); |
| 238 | |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 239 | const webSocketSdp = WebSocketSdp.createWebSocketSdp( |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 240 | builder, SdpType.OFFER, offerString); |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 241 | const message = WebSocketMessage.createWebSocketMessage( |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 242 | builder, Payload.WebSocketSdp, |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 243 | webSocketSdp); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 244 | builder.finish(message); |
| 245 | const array = builder.asUint8Array(); |
| 246 | this.webSocketConnection.send(array.buffer.slice(array.byteOffset)); |
| 247 | } |
| 248 | |
| 249 | // We now have a websocket, so start setting up the peer connection. We only |
| 250 | // want a DataChannel, so create it and then create an offer to send. |
| 251 | onWebSocketOpen(): void { |
James Kuszmaul | 54424d0 | 2020-12-26 18:09:20 -0800 | [diff] [blame] | 252 | this.rtcPeerConnection = new RTCPeerConnection( |
| 253 | {'iceServers': [{'urls': ['stun:stun.l.google.com:19302']}]}); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 254 | this.rtcPeerConnection.addEventListener( |
Alex Perry | 22824d7 | 2020-02-29 17:11:43 -0800 | [diff] [blame] | 255 | 'datachannel', (e) => this.onDataChannel(e)); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 256 | this.dataChannel = this.rtcPeerConnection.createDataChannel('signalling'); |
James Kuszmaul | 1ec7443 | 2020-07-30 20:26:45 -0700 | [diff] [blame] | 257 | this.handlers.add( |
| 258 | new Handler((data) => this.onConfigMessage(data), this.dataChannel)); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 259 | this.rtcPeerConnection.addEventListener( |
| 260 | 'icecandidate', (e) => this.onIceCandidate(e)); |
James Kuszmaul | 54424d0 | 2020-12-26 18:09:20 -0800 | [diff] [blame] | 261 | this.rtcPeerConnection.addEventListener( |
| 262 | 'icecandidateerror', (e) => this.onIceCandidateError(e)); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 263 | this.rtcPeerConnection.createOffer().then( |
| 264 | (offer) => this.onOfferCreated(offer)); |
| 265 | } |
| 266 | |
| 267 | // When we receive a websocket message, we need to determine what type it is |
| 268 | // and handle appropriately. Either by setting the remote description or |
| 269 | // adding the remote ice candidate. |
| 270 | onWebSocketMessage(e: MessageEvent): void { |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 271 | const buffer = new Uint8Array(e.data) |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 272 | const fbBuffer = new ByteBuffer(buffer); |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 273 | const message = WebSocketMessage.getRootAsWebSocketMessage(fbBuffer); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 274 | switch (message.payloadType()) { |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 275 | case Payload.WebSocketSdp: |
| 276 | const sdpFb = message.payload(new WebSocketSdp()); |
| 277 | if (sdpFb.type() !== SdpType.ANSWER) { |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 278 | console.log('got something other than an answer back'); |
| 279 | break; |
| 280 | } |
| 281 | this.rtcPeerConnection.setRemoteDescription(new RTCSessionDescription( |
| 282 | {'type': 'answer', 'sdp': sdpFb.payload()})); |
| 283 | break; |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 284 | case Payload.WebSocketIce: |
| 285 | const iceFb = message.payload(new WebSocketIce()); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 286 | const candidate = {} as RTCIceCandidateInit; |
| 287 | candidate.candidate = iceFb.candidate(); |
| 288 | candidate.sdpMid = iceFb.sdpMid(); |
| 289 | candidate.sdpMLineIndex = iceFb.sdpMLineIndex(); |
| 290 | this.rtcPeerConnection.addIceCandidate(candidate); |
| 291 | break; |
| 292 | default: |
| 293 | console.log('got an unknown message'); |
| 294 | break; |
| 295 | } |
| 296 | } |
Alex Perry | 6249aaf | 2020-02-29 14:51:49 -0800 | [diff] [blame] | 297 | |
| 298 | /** |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 299 | * Subscribes to messages. Only the most recent connect message is in use. Any |
| 300 | * channels not specified in the message are implicitely unsubscribed. |
Alex Perry | 6249aaf | 2020-02-29 14:51:49 -0800 | [diff] [blame] | 301 | * @param a Finished flatbuffer.Builder containing a Connect message to send. |
| 302 | */ |
| 303 | sendConnectMessage(builder: any) { |
Alex Perry | 3dfcb81 | 2020-03-04 19:32:17 -0800 | [diff] [blame] | 304 | const array = builder.asUint8Array(); |
Alex Perry | 6249aaf | 2020-02-29 14:51:49 -0800 | [diff] [blame] | 305 | this.dataChannel.send(array.buffer.slice(array.byteOffset)); |
| 306 | } |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 307 | } |