From f44d84b7be5e7aa96326a477dd08e33dda6fbcb8 Mon Sep 17 00:00:00 2001 From: Michael Mainguy Date: Wed, 2 Aug 2023 19:29:54 -0500 Subject: [PATCH] Initial network P2P share code. --- package-lock.json | 1 - src/app.ts | 3 ++ src/integration/peerjsNetworkConnection.ts | 45 +++++++++++++++++++++- vite.config.ts | 1 - 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 43d4afe..6729492 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,7 +7,6 @@ "": { "name": "immersive", "version": "0.0.1", - "hasInstallScript": true, "dependencies": { "@babylonjs/core": "^6.14.0", "@babylonjs/gui": "^6.14.0", diff --git a/src/app.ts b/src/app.ts index 9035332..041dcd3 100644 --- a/src/app.ts +++ b/src/app.ts @@ -23,6 +23,7 @@ import {DualshockEventMapper} from "./util/dualshockEventMapper"; import log from "loglevel"; import {AppConfig} from "./util/appConfig"; import {DiaSounds} from "./util/diaSounds"; +import {PeerjsNetworkConnection} from "./integration/peerjsNetworkConnection"; export class App { //preTasks = [havokModule]; @@ -37,6 +38,7 @@ export class App { const config = AppConfig.config; log.setLevel('info'); log.getLogger('App').setLevel('debug'); + log.getLogger('PeerjsNetworkConnection').setLevel('debug'); const canvas = document.createElement("canvas"); canvas.style.width = "100%"; canvas.style.height = "100%"; @@ -112,6 +114,7 @@ export class App { const diagramManager = new DiagramManager(this.scene, xr.baseExperience); this.rig = new Rigplatform(this.scene, xr, diagramManager); const toolbox = new Toolbox(scene, xr.baseExperience, diagramManager); + //const network = new PeerjsNetworkConnection(); import ('./diagram/indexdbPersistenceManager').then((module) => { const persistenceManager = new module.IndexdbPersistenceManager("diagram"); diff --git a/src/integration/peerjsNetworkConnection.ts b/src/integration/peerjsNetworkConnection.ts index 780e1bb..2f2d539 100644 --- a/src/integration/peerjsNetworkConnection.ts +++ b/src/integration/peerjsNetworkConnection.ts @@ -1,3 +1,46 @@ -export class PeerjsNetworkConnection { +import P2PDataChannel from 'p2p-data-channel'; +import log from "loglevel"; +export class PeerjsNetworkConnection { + private logger: log.Logger = log.getLogger('PeerjsNetworkConnection'); + private dataChannel: P2PDataChannel; + + constructor() { + const config = { + debug: false, + dataChannel: 'default', + connectionTimeout: 5000, + pingInterval: 4000, + pingTimeout: 8000 + } + + + const data = window.location.search.replace('?', '') + .split('&') + .map((x) => x.split('=')); + this.dataChannel = new P2PDataChannel(data[0][1], config); + + this.dataChannel.onConnected((peerId) => { + this.logger.debug(peerId, ' connected'); + }); + this.dataChannel.onMessage((message) => { + this.logger.debug(message.payload, ' received from ', message.sender); + }); + this.connect(); + + } + + private async connect() { + try { + const data = window.location.search.replace('?', '') + .split('&') + .map((x) => x.split('=')); + const connection = await this.dataChannel.connect(data[1][1]).then(() => { + console.log('Connected'); + }); + this.dataChannel.broadcast({payload: 'Hello World'}); + } catch (err) { + this.logger.error(err); + } + } } \ No newline at end of file diff --git a/vite.config.ts b/vite.config.ts index 118e92b..1704725 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -9,7 +9,6 @@ export default defineConfig({ '/callback': 'https://local.immersiveidea.com' } }, - base: "/" }) \ No newline at end of file