Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PeerJS transports #25

Open
kimmobrunfeldt opened this issue Apr 19, 2022 · 2 comments
Open

PeerJS transports #25

kimmobrunfeldt opened this issue Apr 19, 2022 · 2 comments

Comments

@kimmobrunfeldt
Copy link

kimmobrunfeldt commented Apr 19, 2022

I don't have time now to put this into a proper project with autotester tests, but here's PeerJS transport adapter for anyone in need. License MIT.

PeerJsTransportClient.ts

import Peer from 'peerjs'

export type PeerJsTransportClientOptions = {
  peerConnection: Peer.DataConnection
}

export class PeerJsTransportClient {
  peerConnection: Peer.DataConnection

  constructor({ peerConnection }: PeerJsTransportClientOptions) {
    this.peerConnection = peerConnection
  }

  onData(callback: (data: unknown) => unknown) {
    this.peerConnection.on('data', callback)
  }

  async sendData(data: unknown) {
    return this.peerConnection.send(data)
  }
}

PeerJsTransportServer.ts

import Peer from 'peerjs'

export type PeerJsTransportServerOptions = {
  peerConnection: Peer.DataConnection
}

export class PeerJsTransportServer {
  peerConnection: Peer.DataConnection

  constructor({ peerConnection }: PeerJsTransportServerOptions) {
    this.peerConnection = peerConnection
  }

  onData(callback: (data: unknown) => unknown) {
    this.peerConnection.on('data', async (reqData) => {
      const respData = await callback(reqData)
      if (!respData) return // no data means notification
      this.peerConnection.send(respData)
    })
  }
}

Usage

server

const peer = new Peer('mouse-ghost-779', {
        debug: 10,
      })

      peer.on('open', (id) => {
        console.log('host open', id)
        peer.on('connection', (conn) => {
          console.log('peer on connection')

          conn.on('open', async () => {
            console.log('conn open, start server')
            const server = new MoleServer({
              transports: [
                new PeerJsTransportServer({
                  peerConnection: conn,
                }),
              ],
            })

            server.expose({
              getGreeting(name: string) {
                return new Promise((resolve, reject) => {
                  setTimeout(() => {
                    resolve(`Hi, ${name}`)
                  }, 1000)
                })
              },
            })

            await server.run()
          })
        })
      })

      peer.on('error', (err) => console.error(err))
    }

client

const peer = new Peer({
        debug: 10,
      })
      peer.on('open', () => {
        const conn = peer.connect('mouse-ghost-779')
        conn.on('open', async () => {
          console.log('conn open')

          const client = new MoleClient({
            transport: new PeerJsTransportClient({
              peerConnection: conn,
            }),
          })

          console.log(
            'FROM CLIENT 1',
            await client.callMethod('getGreeting', ['User1'])
          )
        })

        conn.on('close', () => console.log('conn close'))
        conn.on('error', (err) => console.error(err))
      })

      peer.on('error', (err) => console.error(err))
    }
@koorchik
Copy link
Owner

Good idea! WebRTC p2p can be a useful transport. Thank you! Maybe someone will pick up it to finish and publish to npm.

@priyankgandhi0
Copy link

local stream microphone not working while share screen + system audio shared in HTML JS

Screenshot 2023-07-19 182435

Here is code HTML + JS ✅

 function startScreenShare() {
        if (screenSharing) {
            stopScreenSharing()
        }
        navigator.mediaDevices.getDisplayMedia(
            { video: { mediaSource: "screen" }, audio: true }
        ).then((stream) => {
            setScreenSharingStream(stream);
    
            screenStream = stream;
            let videoTrack = screenStream.getAudioTracks()[0];
            videoTrack.onended = () => {
                stopScreenSharing()
            }
            if (peer) {
                let sender = currentPeer.peerConnection.getSenders().find(function (s) {
                    return s.track.kind == videoTrack.kind;
                })
                sender.replaceTrack(videoTrack)
                screenSharing = true
            }
            console.log(screenStream)
        })
    }

We have tried to share screen audio. When sharing the screen, microphone and screen-sharing audio do not work together. Mic does not work when system audio is on. System audio does not working if end mic is on. please explain me what is the about issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants