sinclairzx81 / smoke

Run Web Servers in Web Browsers over WebRTC
Other
547 stars 40 forks source link

Revision 0.8.8 #31

Closed sinclairzx81 closed 2 months ago

sinclairzx81 commented 2 months ago

This PR adds a .stats() function to return current WebRTC network statistics. This is a synchronous function that can be called at relatively high frequency. The following is the API.

import { Network } from '@sinclair/smoke'

const { WebRtc } = new Network({ ... })

const stats = WebRtc.stats()

This function will return an array of WebRtcPeerStatistics structures of the following form.

// ------------------------------------------------------------------
// WebRtcPeerStatistic
// ------------------------------------------------------------------
export interface WebRtcPeerStatistic {
  /** The local address of this peer */
  localAddress: string
  /** The remote address of this peer */
  remoteAddress: string
  /** If this peer is making an offer */
  makingOffer: boolean
  /** If this peer is ignoring offers */
  ignoreOffer: boolean
  /** The number of bytes sent to this peer */
  bytesSent: number
  /** The number of bytes received from this peer */
  bytesReceived: number
  /** The number of send bytes being buffered for this peer */
  bytesBuffered: number
  /** The number of data channels being managed by this peer */
  channelCount: number
}

Note: As of this PR, the bytesReceived and bytesSent totals are accumulated inside the NetModule (not inside the WebRtcModule). While the bytesReceived could technically be handled via the WebRtcModule, this update holds that all interactions with a channel (including subscribing to message events) should be handled exterior to the WebRtcModule (where the WebRtcModule is limited to peer connection establishment only, not interactions with channels or media on that connection).