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