@@ -7,6 +7,8 @@ import { deserializeUplink, serializeDownlink, UpdatePeersAction } from './proto
77export interface WebSocketData {
88 peerId : string
99 roomId : string
10+ uplinkTraffic : number
11+ downlinkTraffic : number
1012}
1113
1214export interface ServerOptions {
@@ -83,7 +85,7 @@ class WebSocketSignalingServer {
8385 }
8486
8587 this . wss ! . handleUpgrade ( request , socket , head , ( ws : WebSocketWithData ) => {
86- ws . data = { roomId, peerId }
88+ ws . data = { roomId, peerId, uplinkTraffic : 0 , downlinkTraffic : 0 }
8789 this . wss ! . emit ( 'connection' , ws , request )
8890 } )
8991 } )
@@ -104,6 +106,7 @@ class WebSocketSignalingServer {
104106
105107 ws . onmessage = ( { data } ) => {
106108 try {
109+ ws . data ! . uplinkTraffic += sizeOf ( data as ArrayBuffer | string )
107110 const uplink = deserializeUplink ( data as ArrayBuffer | string )
108111 this . handleMessage ( uplink , roomId , peerId )
109112 }
@@ -179,8 +182,10 @@ class WebSocketSignalingServer {
179182 : Array . from ( roomClients . values ( ) )
180183 if ( wsTargets . length ) {
181184 const downlinkMessage = serializeDownlink ( downlinkPayload )
185+ const size = sizeOf ( downlinkMessage )
182186 wsTargets . forEach ( ( client ) => {
183187 if ( client && client . data ?. peerId !== senderId && client . readyState === WebSocket . OPEN ) {
188+ client . data ! . downlinkTraffic += size
184189 if ( this . options . manualDelay ) {
185190 setTimeout ( ( ) => {
186191 client . send ( downlinkMessage )
@@ -243,6 +248,10 @@ class WebSocketSignalingServer {
243248 }
244249}
245250
251+ function sizeOf ( data : string | ArrayBuffer ) : number {
252+ return typeof data === 'string' ? data . length : data . byteLength
253+ }
254+
246255export function createServer ( options : ServerOptions ) : WebSocketSignalingServer {
247256 return new WebSocketSignalingServer ( options )
248257}
0 commit comments