diff --git a/flottform/forms/src/flottform-channel-client.ts b/flottform/forms/src/flottform-channel-client.ts index 3498cda..0c53a13 100644 --- a/flottform/forms/src/flottform-channel-client.ts +++ b/flottform/forms/src/flottform-channel-client.ts @@ -88,7 +88,6 @@ export class FlottformChannelClient extends EventEmitter { this.dataChannel.bufferedAmountLowThreshold = this.BUFFER_THRESHOLD; // Set the listener to listen then emit an event when the buffer has more space available and can be used to send more data this.dataChannel.onbufferedamountlow = () => { - console.log('----------EVENT FIRED----------'); this.emit('bufferedamountlow'); }; this.dataChannel.onopen = (e) => { diff --git a/flottform/forms/src/flottform-text-input-client.ts b/flottform/forms/src/flottform-text-input-client.ts new file mode 100644 index 0000000..c0e11ce --- /dev/null +++ b/flottform/forms/src/flottform-text-input-client.ts @@ -0,0 +1,78 @@ +import { FlottformChannelClient } from './flottform-channel-client'; +import { DEFAULT_WEBRTC_CONFIG, EventEmitter, Logger, POLL_TIME_IN_MS } from './internal'; + +type Listeners = { + connected: []; + 'webrtc:connection-impossible': []; + sending: []; // Emitted to signal the start of sending the file(s) + done: []; + disconnected: []; + error: [e: string]; +}; + +export class FlottformTextInputClient extends EventEmitter { + private channel: FlottformChannelClient | null = null; + private logger: Logger; + + constructor({ + endpointId, + flottformApi, + rtcConfiguration = DEFAULT_WEBRTC_CONFIG, + pollTimeForIceInMs = POLL_TIME_IN_MS, + logger = console + }: { + endpointId: string; + flottformApi: string; + rtcConfiguration?: RTCConfiguration; + pollTimeForIceInMs?: number; + logger?: Logger; + }) { + super(); + this.channel = new FlottformChannelClient({ + endpointId, + flottformApi, + rtcConfiguration, + pollTimeForIceInMs, + logger + }); + this.logger = logger; + this.registerListeners(); + } + start = () => { + this.channel?.start(); + }; + + close = () => { + // Should be called once all the text has been sent. + this.channel?.close(); + }; + + sendText = (text: string) => { + // For now, I didn't handle very large texts since for most use cases the text won't exceed the size of 1 chunk ( 16KB ) + this.emit('sending'); + this.channel?.sendData(text); + this.emit('done'); + }; + + private registerListeners = () => { + this.channel?.on('init', () => {}); + this.channel?.on('retrieving-info-from-endpoint', () => {}); + this.channel?.on('sending-client-info', () => {}); + this.channel?.on('connecting-to-host', () => {}); + this.channel?.on('connected', () => { + this.emit('connected'); + }); + this.channel?.on('connection-impossible', () => { + this.emit('webrtc:connection-impossible'); + }); + this.channel?.on('done', () => { + this.emit('done'); + }); + this.channel?.on('disconnected', () => { + this.emit('disconnected'); + }); + this.channel?.on('error', (e) => { + this.emit('error', e); + }); + }; +} diff --git a/flottform/forms/src/flottform-text-input-host.ts b/flottform/forms/src/flottform-text-input-host.ts new file mode 100644 index 0000000..f74482f --- /dev/null +++ b/flottform/forms/src/flottform-text-input-host.ts @@ -0,0 +1,116 @@ +import { FlottformChannelHost } from './flottform-channel-host'; +import { Styles } from './flottform-styles'; +import { DEFAULT_WEBRTC_CONFIG, EventEmitter, Logger, POLL_TIME_IN_MS } from './internal'; + +type Listeners = { + new: []; + disconnected: []; + error: [error: any]; + connected: []; + receive: []; // Emitted to signal the start of receiving the file(s) + done: [data: string]; + 'endpoint-created': [{ link: string; qrCode: string }]; + 'webrtc:waiting-for-client': [ + event: { link: string; qrCode: string; channel: FlottformChannelHost } + ]; + 'webrtc:waiting-for-ice': []; + 'webrtc:waiting-for-file': []; +}; + +const noop = () => {}; + +export class FlottformTextInputHost extends EventEmitter { + private channel: FlottformChannelHost | null = null; + private logger: Logger; + private link: string = ''; + private qrCode: string = ''; + + constructor({ + flottformApi, + createClientUrl, + rtcConfiguration = DEFAULT_WEBRTC_CONFIG, + pollTimeForIceInMs = POLL_TIME_IN_MS, + theme, + logger = console + }: { + flottformApi: string | URL; + createClientUrl: (params: { endpointId: string }) => Promise; + rtcConfiguration?: RTCConfiguration; + pollTimeForIceInMs?: number; + theme?: (myself: FlottformTextInputHost) => void; + logger?: Logger; + }) { + super(); + this.channel = new FlottformChannelHost({ + flottformApi, + createClientUrl, + rtcConfiguration, + pollTimeForIceInMs, + logger + }); + this.logger = logger; + + this.registerListeners(); + theme && theme(this); + } + + start = () => { + this.channel?.start(); + }; + + close = () => { + this.channel?.close(); + }; + + getLink = () => { + if (this.link === '') { + this.logger.error( + 'Flottform is currently establishing the connection. Link is unavailable for now!' + ); + } + return this.link; + }; + + getQrCode = () => { + if (this.qrCode === '') { + this.logger.error( + 'Flottform is currently establishing the connection. qrCode is unavailable for now!' + ); + } + return this.qrCode; + }; + + private handleIncomingData = (e: MessageEvent) => { + // We suppose that the data received is small enough to be all included in 1 message + this.emit('done', e.data); + }; + + private registerListeners = () => { + this.channel?.on('new', ({ channel }) => { + this.emit('new'); + }); + this.channel?.on('waiting-for-client', (event) => { + this.emit('webrtc:waiting-for-client', event); + const { qrCode, link } = event; + this.emit('endpoint-created', { link, qrCode }); + this.link = link; + this.qrCode = qrCode; + }); + this.channel?.on('waiting-for-ice', () => { + this.emit('webrtc:waiting-for-ice'); + }); + this.channel?.on('waiting-for-file', () => { + this.emit('webrtc:waiting-for-file'); + this.emit('connected'); + }); + this.channel?.on('receiving-data', (e) => { + this.handleIncomingData(e); + }); + this.channel?.on('disconnected', () => { + this.emit('disconnected'); + }); + this.channel?.on('error', (error) => { + this.emit('error', error); + }); + }; +} diff --git a/flottform/forms/src/index.ts b/flottform/forms/src/index.ts index 43c2bd5..dbb38e8 100644 --- a/flottform/forms/src/index.ts +++ b/flottform/forms/src/index.ts @@ -1,5 +1,7 @@ export { FlottformFileInputHost } from './flottform-file-input-host'; export { FlottformFileInputClient } from './flottform-file-input-client'; +export { FlottformTextInputClient } from './flottform-text-input-client'; +export { FlottformTextInputHost } from './flottform-text-input-host'; export type { ClientState, SafeEndpointInfo, Logger } from './internal'; import './theme/style.css'; export { defaultThemeForFileInput } from './theme/defaultTheme'; diff --git a/flottform/where-are-you-at/src/routes/+page.svelte b/flottform/where-are-you-at/src/routes/+page.svelte index 984226a..3201121 100644 --- a/flottform/where-are-you-at/src/routes/+page.svelte +++ b/flottform/where-are-you-at/src/routes/+page.svelte @@ -1,12 +1,21 @@
- {#if $currentState === 'new'} - {:else if $currentState === 'waiting-for-client'} + {:else if $currentState === 'endpoint-created'} - {:else if $currentState === 'waiting-for-ice'} + {:else if $currentState === 'webrtc:waiting-for-ice'} Trying to establish a connection with your friend - {:else if $currentState === 'waiting-for-file'} + {:else if $currentState === 'connected'} Waiting for friend to send their location - {:else if $currentState === 'receiving-data'} - Receiving location {:else if $currentState === 'done'}
diff --git a/flottform/where-are-you-at/src/routes/now/[endpointId]/+page.svelte b/flottform/where-are-you-at/src/routes/now/[endpointId]/+page.svelte index 27ea7d0..4b272fe 100644 --- a/flottform/where-are-you-at/src/routes/now/[endpointId]/+page.svelte +++ b/flottform/where-are-you-at/src/routes/now/[endpointId]/+page.svelte @@ -1,83 +1,75 @@
- {#if currentState === 'init'}

Trying to connect to host

- {:else if currentState === 'start'} + {:else if currentState === 'connected'}

Let me know your location, please!