├── public ├── robots.txt ├── img │ ├── background.png │ ├── logo.svg │ └── logo-inversed.svg ├── fonts │ └── Fredoka_One │ │ ├── FredokaOne-Regular.ttf │ │ └── OFL.txt └── index.html ├── .prettierrc ├── babel.config.js ├── vue.config.js ├── assets └── demo.png ├── src ├── shims-vue.d.ts ├── main.ts ├── webRTC │ ├── EventDispatcher.ts │ ├── Message.ts │ ├── SignalingMessage.ts │ ├── PeerConnection.ts │ ├── Sender.ts │ ├── Reciever.ts │ └── SignalingClient.ts ├── components │ ├── QrCode.vue │ ├── QrScanner.vue │ ├── Loader.vue │ └── MessageCard.vue └── App.vue ├── .gitignore ├── .eslintrc.js ├── tsconfig.json ├── LICENSE ├── package.json └── README.md /public/robots.txt: -------------------------------------------------------------------------------- 1 | User-agent: * 2 | Disallow: 3 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "trailingComma": "none", 3 | "arrowParens": "avoid" 4 | } 5 | -------------------------------------------------------------------------------- /babel.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | presets: ["@vue/cli-plugin-babel/preset"] 3 | }; 4 | -------------------------------------------------------------------------------- /vue.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | devServer: { 3 | https: false 4 | } 5 | }; 6 | -------------------------------------------------------------------------------- /assets/demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schlangguru/clip-beam-client/HEAD/assets/demo.png -------------------------------------------------------------------------------- /public/img/background.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schlangguru/clip-beam-client/HEAD/public/img/background.png -------------------------------------------------------------------------------- /public/fonts/Fredoka_One/FredokaOne-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/schlangguru/clip-beam-client/HEAD/public/fonts/Fredoka_One/FredokaOne-Regular.ttf -------------------------------------------------------------------------------- /src/shims-vue.d.ts: -------------------------------------------------------------------------------- 1 | /* eslint-disable */ 2 | declare module '*.vue' { 3 | import type { DefineComponent } from 'vue' 4 | const component: DefineComponent<{}, {}, any> 5 | export default component 6 | } 7 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { createApp } from "vue"; 2 | import PrimeVue from "primevue/config"; 3 | import ToastService from "primevue/toastservice"; 4 | import App from "./App.vue"; 5 | 6 | createApp(App) 7 | .use(PrimeVue) 8 | .use(ToastService) 9 | .mount("#app"); 10 | -------------------------------------------------------------------------------- /src/webRTC/EventDispatcher.ts: -------------------------------------------------------------------------------- 1 | export class EventDispatcher { 2 | private listeners: ((data: T) => void)[] = []; 3 | 4 | addListener(listener: (data: T) => void) { 5 | this.listeners.push(listener); 6 | } 7 | 8 | dispatch(data: T) { 9 | this.listeners.forEach(l => l(data)); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | /dist 4 | 5 | 6 | # local env files 7 | .env.local 8 | .env.*.local 9 | 10 | # Log files 11 | npm-debug.log* 12 | yarn-debug.log* 13 | yarn-error.log* 14 | pnpm-debug.log* 15 | 16 | # Editor directories and files 17 | .idea 18 | .vscode 19 | *.suo 20 | *.ntvs* 21 | *.njsproj 22 | *.sln 23 | *.sw? 24 | -------------------------------------------------------------------------------- /src/webRTC/Message.ts: -------------------------------------------------------------------------------- 1 | export enum MessageType { 2 | TEXT = "TEXT", 3 | FILE = "FILE" 4 | } 5 | 6 | export interface MessageHeader { 7 | type: MessageType; 8 | name?: string; 9 | size: number; 10 | } 11 | 12 | export interface Message { 13 | header: MessageHeader; 14 | timestamp?: Date; 15 | transferCompleted: boolean; 16 | transferProgress: number; 17 | payload?: string | File; 18 | } 19 | -------------------------------------------------------------------------------- /src/components/QrCode.vue: -------------------------------------------------------------------------------- 1 | 4 | 5 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | env: { 4 | node: true 5 | }, 6 | extends: [ 7 | "plugin:vue/vue3-essential", 8 | "eslint:recommended", 9 | "@vue/typescript/recommended", 10 | "@vue/prettier", 11 | "@vue/prettier/@typescript-eslint" 12 | ], 13 | parserOptions: { 14 | ecmaVersion: 2020 15 | }, 16 | rules: { 17 | "no-console": process.env.NODE_ENV === "production" ? "warn" : "off", 18 | "no-debugger": process.env.NODE_ENV === "production" ? "warn" : "off", 19 | '@typescript-eslint/interface-name-prefix': 0 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | <%= htmlWebpackPlugin.options.title %> 9 | 10 | 11 | 14 |
15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/webRTC/SignalingMessage.ts: -------------------------------------------------------------------------------- 1 | export enum SignalingType { 2 | REGISTER = "REGISTER", 3 | OFFER = "OFFER", 4 | ERROR = "ERROR", 5 | ANSWER = "ANSWER", 6 | ICE_CANDIDATE = "ICE_CANDIDATE" 7 | } 8 | 9 | export interface OfferPayload { 10 | offer: RTCSessionDescriptionInit; 11 | peerUuid: string; 12 | } 13 | 14 | export interface AnswerPayload { 15 | answer: RTCSessionDescriptionInit; 16 | peerUuid: string; 17 | } 18 | 19 | export interface ICECandidatePayload { 20 | candidate: RTCIceCandidateInit; 21 | peerUuid: string; 22 | } 23 | 24 | export interface SignalingMsg { 25 | type: SignalingType; 26 | payload: string | OfferPayload | AnswerPayload | ICECandidatePayload; 27 | } 28 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "esnext", 4 | "module": "esnext", 5 | "strict": true, 6 | "noImplicitAny": false, 7 | "jsx": "preserve", 8 | "importHelpers": true, 9 | "moduleResolution": "node", 10 | "skipLibCheck": true, 11 | "esModuleInterop": true, 12 | "allowSyntheticDefaultImports": true, 13 | "sourceMap": true, 14 | "baseUrl": ".", 15 | "types": [ 16 | "webpack-env" 17 | ], 18 | "paths": { 19 | "@/*": [ 20 | "src/*" 21 | ] 22 | }, 23 | "lib": [ 24 | "esnext", 25 | "dom", 26 | "dom.iterable", 27 | "scripthost" 28 | ], 29 | }, 30 | "include": [ 31 | "src/**/*.ts", 32 | "src/**/*.tsx", 33 | "src/**/*.vue", 34 | "tests/**/*.ts", 35 | "tests/**/*.tsx" 36 | ], 37 | "exclude": [ 38 | "node_modules" 39 | ] 40 | } 41 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Sebastian Seidl 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "client", 3 | "version": "0.1.0", 4 | "private": true, 5 | "scripts": { 6 | "serve": "vue-cli-service serve", 7 | "build": "vue-cli-service build", 8 | "lint": "vue-cli-service lint" 9 | }, 10 | "dependencies": { 11 | "@zxing/library": "^0.18.3", 12 | "core-js": "^3.6.5", 13 | "mitt": "^2.1.0", 14 | "primeicons": "^4.1.0", 15 | "primevue": "^3.1.1", 16 | "uuid": "^8.3.2", 17 | "vue": "^3.0.0", 18 | "vue-router": "^4.0.0-0" 19 | }, 20 | "devDependencies": { 21 | "@types/uuid": "^8.3.0", 22 | "@typescript-eslint/eslint-plugin": "^2.33.0", 23 | "@typescript-eslint/parser": "^2.33.0", 24 | "@vue/cli-plugin-babel": "~4.5.0", 25 | "@vue/cli-plugin-eslint": "~4.5.0", 26 | "@vue/cli-plugin-router": "~4.5.0", 27 | "@vue/cli-plugin-typescript": "~4.5.0", 28 | "@vue/cli-service": "~4.5.0", 29 | "@vue/compiler-sfc": "^3.0.0", 30 | "@vue/eslint-config-prettier": "^6.0.0", 31 | "@vue/eslint-config-typescript": "^5.0.2", 32 | "eslint": "^6.7.2", 33 | "eslint-plugin-prettier": "^3.1.3", 34 | "eslint-plugin-vue": "^7.0.0-0", 35 | "prettier": "^1.19.1", 36 | "typescript": "~3.9.3" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/webRTC/PeerConnection.ts: -------------------------------------------------------------------------------- 1 | import { v4 as uuidv4 } from "uuid"; 2 | import { EventDispatcher } from "./EventDispatcher"; 3 | import { MessageType, Message } from "./Message"; 4 | import { TextSender, FileSender } from "./Sender"; 5 | import { Reciever, TextReciever, FileReciever } from "./Reciever"; 6 | 7 | type ProgressCallback = (progress: number) => void; 8 | export class PeerConnection { 9 | private readonly rtcConnection: RTCPeerConnection; 10 | private readonly heartBeatChannel: RTCDataChannel; 11 | 12 | public readonly onRecievingMessage = new EventDispatcher<{ 13 | id: string; 14 | msg: Message; 15 | }>(); 16 | public readonly onClose = new EventDispatcher(); 17 | 18 | constructor( 19 | rtcConnection: RTCPeerConnection, 20 | heartBeatChannel: RTCDataChannel 21 | ) { 22 | this.heartBeatChannel = heartBeatChannel; 23 | this.heartBeatChannel.onclose = () => { 24 | this.onClose.dispatch(); 25 | }; 26 | 27 | this.rtcConnection = rtcConnection; 28 | this.rtcConnection.ondatachannel = event => { 29 | this.onDataChannel(event); 30 | }; 31 | } 32 | 33 | public async sendFile(file: File, onProgress?: ProgressCallback) { 34 | const sender = new FileSender(this.rtcConnection, file); 35 | if (onProgress) { 36 | sender.onProgress.addListener(onProgress); 37 | } 38 | sender.send(); 39 | } 40 | 41 | public async sendText(text: string, onProgress?: ProgressCallback) { 42 | const sender = new TextSender(this.rtcConnection, text); 43 | if (onProgress) { 44 | sender.onProgress.addListener(onProgress); 45 | } 46 | sender.send(); 47 | } 48 | 49 | private onDataChannel(event: RTCDataChannelEvent) { 50 | const dataChannel = event.channel || event.target; 51 | const label = dataChannel.label; 52 | let reciever: Reciever; 53 | if (label.startsWith(MessageType.FILE)) { 54 | reciever = new FileReciever(dataChannel); 55 | } else if (label.startsWith(MessageType.TEXT)) { 56 | reciever = new TextReciever(dataChannel); 57 | } else { 58 | throw `Unknown channel type ${label}`; 59 | } 60 | 61 | const id = uuidv4(); 62 | reciever.onRecieveMessage.addListener(msg => { 63 | this.onRecievingMessage.dispatch({ id, msg }); 64 | }); 65 | reciever.recieve(); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/components/QrScanner.vue: -------------------------------------------------------------------------------- 1 | 16 | 17 | 84 | 85 | 98 | -------------------------------------------------------------------------------- /src/components/Loader.vue: -------------------------------------------------------------------------------- 1 | 16 | 17 | 28 | 29 | 120 | -------------------------------------------------------------------------------- /src/webRTC/Sender.ts: -------------------------------------------------------------------------------- 1 | import { v4 as uuidv4 } from "uuid"; 2 | import { EventDispatcher } from "./EventDispatcher"; 3 | import { MessageType, MessageHeader } from "./Message"; 4 | 5 | abstract class Sender { 6 | protected readonly dataChannel: RTCDataChannel; 7 | public readonly onProgress = new EventDispatcher(); 8 | 9 | constructor(rtcConnection: RTCPeerConnection) { 10 | const label = `${this.type()}:${uuidv4()}`; 11 | this.dataChannel = rtcConnection.createDataChannel(label, { 12 | ordered: true 13 | }); 14 | this.dataChannel.binaryType = "arraybuffer"; 15 | } 16 | 17 | protected abstract sendData(): void; 18 | 19 | public abstract type(): string; 20 | 21 | public async send() { 22 | this.dataChannel.onopen = () => { 23 | this.sendData(); 24 | }; 25 | } 26 | } 27 | 28 | export class FileSender extends Sender { 29 | private static readonly CHUNK_SIZE = 16384; 30 | private readonly file: File; 31 | 32 | constructor(rtcConnection: RTCPeerConnection, file: File) { 33 | super(rtcConnection); 34 | this.file = file; 35 | } 36 | 37 | public type() { 38 | return MessageType.FILE; 39 | } 40 | 41 | protected async sendData() { 42 | const payload = await this.file.arrayBuffer(); 43 | const header = { 44 | type: MessageType.FILE, 45 | name: this.file.name, 46 | size: payload.byteLength 47 | } as MessageHeader; 48 | 49 | this.dataChannel.send(JSON.stringify(header)); 50 | this.sendFileChunks(); 51 | } 52 | 53 | private async sendFileChunks() { 54 | let bytesSent = 0; 55 | const fileReader = new FileReader(); 56 | let offset = 0; 57 | const readSlice = o => { 58 | const slice = this.file.slice(offset, o + FileSender.CHUNK_SIZE); 59 | fileReader.readAsArrayBuffer(slice); 60 | }; 61 | fileReader.onload = e => { 62 | const buffer = e.target?.result as ArrayBuffer; 63 | this.dataChannel.send(buffer); 64 | bytesSent += buffer.byteLength; 65 | offset += buffer.byteLength; 66 | this.onProgress.dispatch(100 * (bytesSent / this.file.size)); 67 | if (offset < this.file.size) { 68 | readSlice(offset); 69 | } 70 | }; 71 | readSlice(0); 72 | } 73 | } 74 | 75 | export class TextSender extends Sender { 76 | private readonly text: string; 77 | 78 | constructor(rtcConnection: RTCPeerConnection, text: string) { 79 | super(rtcConnection); 80 | this.text = text; 81 | } 82 | 83 | public type() { 84 | return MessageType.TEXT; 85 | } 86 | 87 | protected async sendData() { 88 | const header = { 89 | type: MessageType.TEXT, 90 | size: this.text.length 91 | } as MessageHeader; 92 | 93 | this.dataChannel.send(JSON.stringify(header)); 94 | this.dataChannel.send(this.text); 95 | this.onProgress.dispatch(100); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | clip-beam logo 2 | 3 | # Clip Beam Client 4 | 5 | ![GitHub](https://img.shields.io/github/license/schlangguru/clip-beam-client) 6 | 7 | Clip Beam lets you transfer your data easily between two devices on the same network. Just connect the devices by scanning a QR code and start sharing. 8 | 9 | Clip Beam uses [WebRTC](https://webrtc.org/) to transfer your data, meaning all messages are transfered directly between devices without sending them to any server. 10 | 11 | ![Clip Beam](./assets/demo.png) 12 | 13 | ## Table of content 14 | 15 | - [Quick Start](#quick-setup) 16 | - [Setup](#setup) 17 | - [Client](#setup-the-client) 18 | - [Signaling Server](#setup-the-signaling-server) 19 | - [Inspiration and Alternatives](#inspiration-and-alternatives) 20 | 21 | ## Quick Start 22 | 23 | 1. Clone the [signaling server](https://github.com/schlangguru/clip-beam-server) 24 | 25 | - `cd clip-beam-server` 26 | - `npm install` 27 | - `npm run dev` 28 | 29 | 2. Clone the [client](https://github.com/schlangguru/clip-beam-client) 30 | 31 | - `cd clip-beam-client` 32 | - `npm install` 33 | - `npm run serve` 34 | - Go to [localhost:8080](localhost:8080) 35 | 36 | ## Setup 37 | 38 | To setup your own instance of Clip Beam you need to setup the [signaling server](https://github.com/schlangguru/clip-beam-server) and host the frontend as simple HTML website. 39 | 40 | ### Setup the client 41 | 42 | Clone this repository and use the following commands: 43 | 44 | ```bash 45 | # install dependencies 46 | npm install 47 | # serve locally 48 | npm run serve 49 | # build for the distribution 50 | npm run build 51 | ``` 52 | 53 | ### Client Configuration 54 | 55 | You can configure the url of the signaling server. Per default the client assumes to reach the signaling server on localhost. To change this you need to create a `.env.local` file with the following content. 56 | 57 | ``` 58 | VUE_APP_SERVER_URL=wss://my-url.com:9090 59 | ``` 60 | 61 | Rever to the Vue.js documentation [on environment variables](https://cli.vuejs.org/guide/mode-and-env.html#environment-variables) for more details. 62 | 63 | ### Setup the signaling server 64 | 65 | See installation instructions for the [signaling server](https://github.com/schlangguru/clip-beam-server) 66 | 67 | ## Inspiration and Alternatives 68 | 69 | I used to use [Threema Web](https://github.com/threema-ch/threema-web) and its echo chat to quickly send web links from my smartphone to my computer. Clip Beam works exactly the same way, but unlike Threema it is not a messenger, but only designed for sharing data between two devices. 70 | 71 | Alternatives i've found are 72 | 73 | - [Sharedrop](https://github.com/cowbell/sharedrop) 74 | - [Snapdrop](https://github.com/RobinLinus/snapdrop) 75 | 76 | but unlike these apps, Clip Beam does not allow connections between any devices on the same network. It rather connects two devices directly by scanning its QR code, so no one can send you unwanted data. 77 | -------------------------------------------------------------------------------- /src/webRTC/Reciever.ts: -------------------------------------------------------------------------------- 1 | import { MessageHeader, Message } from "./Message"; 2 | import { EventDispatcher } from "./EventDispatcher"; 3 | 4 | export abstract class Reciever { 5 | protected readonly dataChannel: RTCDataChannel; 6 | private _header?: MessageHeader; 7 | public readonly onRecieveMessage = new EventDispatcher(); 8 | 9 | constructor(dataChannel: RTCDataChannel) { 10 | this.dataChannel = dataChannel; 11 | this.dataChannel.binaryType = "arraybuffer"; 12 | } 13 | 14 | public recieve() { 15 | this.dataChannel.onmessage = event => { 16 | this.onMessage(event); 17 | }; 18 | } 19 | 20 | protected header(): MessageHeader { 21 | if (!this._header) { 22 | throw "Header not yet recieved."; 23 | } 24 | return this._header; 25 | } 26 | 27 | protected closeChannel() { 28 | this.dataChannel.close(); 29 | } 30 | 31 | protected abstract onData(payload: MessageEvent); 32 | 33 | private onMessage(event: MessageEvent) { 34 | if (!this._header) { 35 | const header = JSON.parse(event.data as string) as MessageHeader; 36 | this._header = header; 37 | this.onRecieveMessage.dispatch({ 38 | header: header, 39 | transferCompleted: false, 40 | transferProgress: 0 41 | }); 42 | } else { 43 | this.onData(event); 44 | } 45 | } 46 | } 47 | 48 | export class TextReciever extends Reciever { 49 | protected onData(event: MessageEvent) { 50 | const text = event.data as string; 51 | this.closeChannel(); 52 | this.onRecieveMessage.dispatch({ 53 | header: this.header(), 54 | timestamp: new Date(), 55 | transferCompleted: true, 56 | transferProgress: 100, 57 | payload: text 58 | }); 59 | } 60 | } 61 | 62 | export class FileReciever extends Reciever { 63 | private receivedChunks: ArrayBuffer[] = []; 64 | private bytesRecieved = 0; 65 | private lastProgress = 0; 66 | 67 | protected onData(event: MessageEvent) { 68 | const buffer = event.data as ArrayBuffer; 69 | this.receivedChunks.push(buffer); 70 | this.bytesRecieved += buffer.byteLength; 71 | 72 | const progress = 100 * (this.bytesRecieved / this.header().size); 73 | if (progress - this.lastProgress >= 1) { 74 | this.lastProgress = progress; 75 | this.onRecieveMessage.dispatch({ 76 | header: this.header(), 77 | transferCompleted: false, 78 | transferProgress: progress 79 | }); 80 | } 81 | 82 | if (this.header().size === this.recievedByteLength()) { 83 | const fileName = this.header().name || "Unknown File"; 84 | const file = new File(this.receivedChunks, fileName); 85 | this.closeChannel(); 86 | this.onRecieveMessage.dispatch({ 87 | header: this.header(), 88 | timestamp: new Date(), 89 | transferCompleted: true, 90 | transferProgress: 100, 91 | payload: file 92 | }); 93 | } 94 | } 95 | 96 | private recievedByteLength(): number { 97 | if (this.receivedChunks) { 98 | return this.receivedChunks 99 | .map(buffer => buffer.byteLength) 100 | .reduce((a, b) => a + b, 0); 101 | } 102 | 103 | return 0; 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /public/fonts/Fredoka_One/OFL.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2011, Milena Brandao (milenabbrandao@gmail.com), with Reserved Font Name Fredoka. 2 | 3 | This Font Software is licensed under the SIL Open Font License, Version 1.1. 4 | This license is copied below, and is also available with a FAQ at: 5 | http://scripts.sil.org/OFL 6 | 7 | 8 | ----------------------------------------------------------- 9 | SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 10 | ----------------------------------------------------------- 11 | 12 | PREAMBLE 13 | The goals of the Open Font License (OFL) are to stimulate worldwide 14 | development of collaborative font projects, to support the font creation 15 | efforts of academic and linguistic communities, and to provide a free and 16 | open framework in which fonts may be shared and improved in partnership 17 | with others. 18 | 19 | The OFL allows the licensed fonts to be used, studied, modified and 20 | redistributed freely as long as they are not sold by themselves. The 21 | fonts, including any derivative works, can be bundled, embedded, 22 | redistributed and/or sold with any software provided that any reserved 23 | names are not used by derivative works. The fonts and derivatives, 24 | however, cannot be released under any other type of license. The 25 | requirement for fonts to remain under this license does not apply 26 | to any document created using the fonts or their derivatives. 27 | 28 | DEFINITIONS 29 | "Font Software" refers to the set of files released by the Copyright 30 | Holder(s) under this license and clearly marked as such. This may 31 | include source files, build scripts and documentation. 32 | 33 | "Reserved Font Name" refers to any names specified as such after the 34 | copyright statement(s). 35 | 36 | "Original Version" refers to the collection of Font Software components as 37 | distributed by the Copyright Holder(s). 38 | 39 | "Modified Version" refers to any derivative made by adding to, deleting, 40 | or substituting -- in part or in whole -- any of the components of the 41 | Original Version, by changing formats or by porting the Font Software to a 42 | new environment. 43 | 44 | "Author" refers to any designer, engineer, programmer, technical 45 | writer or other person who contributed to the Font Software. 46 | 47 | PERMISSION & CONDITIONS 48 | Permission is hereby granted, free of charge, to any person obtaining 49 | a copy of the Font Software, to use, study, copy, merge, embed, modify, 50 | redistribute, and sell modified and unmodified copies of the Font 51 | Software, subject to the following conditions: 52 | 53 | 1) Neither the Font Software nor any of its individual components, 54 | in Original or Modified Versions, may be sold by itself. 55 | 56 | 2) Original or Modified Versions of the Font Software may be bundled, 57 | redistributed and/or sold with any software, provided that each copy 58 | contains the above copyright notice and this license. These can be 59 | included either as stand-alone text files, human-readable headers or 60 | in the appropriate machine-readable metadata fields within text or 61 | binary files as long as those fields can be easily viewed by the user. 62 | 63 | 3) No Modified Version of the Font Software may use the Reserved Font 64 | Name(s) unless explicit written permission is granted by the corresponding 65 | Copyright Holder. This restriction only applies to the primary font name as 66 | presented to the users. 67 | 68 | 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font 69 | Software shall not be used to promote, endorse or advertise any 70 | Modified Version, except to acknowledge the contribution(s) of the 71 | Copyright Holder(s) and the Author(s) or with their explicit written 72 | permission. 73 | 74 | 5) The Font Software, modified or unmodified, in part or in whole, 75 | must be distributed entirely under this license, and must not be 76 | distributed under any other license. The requirement for fonts to 77 | remain under this license does not apply to any document created 78 | using the Font Software. 79 | 80 | TERMINATION 81 | This license becomes null and void if any of the above conditions are 82 | not met. 83 | 84 | DISCLAIMER 85 | THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 86 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF 87 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT 88 | OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE 89 | COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 90 | INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL 91 | DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 92 | FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM 93 | OTHER DEALINGS IN THE FONT SOFTWARE. 94 | -------------------------------------------------------------------------------- /src/components/MessageCard.vue: -------------------------------------------------------------------------------- 1 | 54 | 55 | 115 | 116 | 175 | -------------------------------------------------------------------------------- /src/webRTC/SignalingClient.ts: -------------------------------------------------------------------------------- 1 | import { 2 | SignalingType, 3 | SignalingMsg, 4 | OfferPayload, 5 | AnswerPayload, 6 | ICECandidatePayload 7 | } from "./SignalingMessage"; 8 | import { PeerConnection } from "./PeerConnection"; 9 | import { EventDispatcher } from "./EventDispatcher"; 10 | 11 | const SIGNALING_SERVER = 12 | process.env.VUE_APP_SERVER_URL || "ws://localhost:9090"; 13 | const RTC_CONNECTION_CONFIG = { 14 | iceServers: [ 15 | { 16 | urls: "stun:stun.l.google.com:19302" 17 | } 18 | ] 19 | }; 20 | const DATA_CHANNEL_NAME = "heartbeat"; 21 | 22 | export class SignalingClient { 23 | private readonly signalingSocket: WebSocket; 24 | private readonly rtcPeerConnection: RTCPeerConnection; 25 | 26 | public readonly onInitConnection = new EventDispatcher(); 27 | public readonly onConnectionFailed = new EventDispatcher(); 28 | public readonly onConnectionEstablished = new EventDispatcher< 29 | PeerConnection 30 | >(); 31 | 32 | constructor() { 33 | this.signalingSocket = new WebSocket(SIGNALING_SERVER); 34 | this.signalingSocket.addEventListener("message", event => 35 | this.onSignalingMessage(JSON.parse(event.data) as SignalingMsg) 36 | ); 37 | 38 | this.rtcPeerConnection = new RTCPeerConnection(RTC_CONNECTION_CONFIG); 39 | this.rtcPeerConnection.oniceconnectionstatechange = () => { 40 | if (this.rtcPeerConnection.iceConnectionState === "failed") { 41 | this.onConnectionFailed.dispatch(); 42 | } 43 | }; 44 | } 45 | 46 | public registerClient(uuid: string) { 47 | this.signalingSocket.addEventListener("open", () => { 48 | this.sendSignal({ 49 | type: "REGISTER", 50 | payload: uuid 51 | }); 52 | }); 53 | } 54 | 55 | public async connectToDevice(peerUuid: string) { 56 | this.rtcPeerConnection.onicecandidate = event => { 57 | if (event.candidate) { 58 | this.sendSignal({ 59 | type: "ICE_CANDIDATE", 60 | payload: { 61 | candidate: event.candidate, 62 | peerUuid: peerUuid 63 | } 64 | }); 65 | } 66 | }; 67 | 68 | this.initDataChannel(); 69 | 70 | const offer = await this.rtcPeerConnection.createOffer(); 71 | this.rtcPeerConnection.setLocalDescription(offer); 72 | this.sendSignal({ 73 | type: "OFFER", 74 | payload: { 75 | offer: offer, 76 | peerUuid: peerUuid 77 | } 78 | }); 79 | } 80 | 81 | private onSignalingMessage(message: SignalingMsg) { 82 | if (message.type == SignalingType.ERROR) { 83 | console.error(message.payload); 84 | } else if (message.type === SignalingType.OFFER) { 85 | const payload = message.payload as OfferPayload; 86 | this.onOffer(payload.peerUuid, payload.offer); 87 | } else if (message.type === SignalingType.ANSWER) { 88 | const payload = message.payload as AnswerPayload; 89 | this.onAnswer(payload.answer); 90 | } else if (message.type === SignalingType.ICE_CANDIDATE) { 91 | const payload = message.payload as ICECandidatePayload; 92 | this.onIceCandidate(payload.candidate); 93 | } 94 | } 95 | 96 | private async onOffer(peerUuid: string, offer: RTCSessionDescriptionInit) { 97 | this.onInitConnection.dispatch(); 98 | this.rtcPeerConnection.setRemoteDescription( 99 | new RTCSessionDescription(offer) 100 | ); 101 | this.rtcPeerConnection.ondatachannel = event => 102 | this.onDataChannelOpened(event); 103 | const answer = await this.rtcPeerConnection.createAnswer(); 104 | this.rtcPeerConnection.setLocalDescription(answer); 105 | this.sendSignal({ 106 | type: SignalingType.ANSWER, 107 | payload: { 108 | answer: answer, 109 | peerUuid: peerUuid 110 | } 111 | }); 112 | } 113 | 114 | private async onAnswer(answer: RTCSessionDescriptionInit) { 115 | this.rtcPeerConnection.setRemoteDescription(answer); 116 | } 117 | 118 | private async onIceCandidate(candidate: RTCIceCandidateInit) { 119 | this.rtcPeerConnection.addIceCandidate(new RTCIceCandidate(candidate)); 120 | } 121 | 122 | private sendSignal(signal: object) { 123 | this.signalingSocket.send(JSON.stringify(signal)); 124 | } 125 | 126 | private initDataChannel() { 127 | const options = { ordered: true }; 128 | const dataChannel = this.rtcPeerConnection.createDataChannel( 129 | DATA_CHANNEL_NAME, 130 | options 131 | ); 132 | dataChannel.onopen = event => 133 | this.onDataChannelOpened(event as RTCDataChannelEvent); 134 | } 135 | 136 | private onDataChannelOpened(event: RTCDataChannelEvent) { 137 | const dataChannel = event.channel || event.target; 138 | this.onConnectionEstablished.dispatch( 139 | new PeerConnection(this.rtcPeerConnection, dataChannel) 140 | ); 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /src/App.vue: -------------------------------------------------------------------------------- 1 |