@@ -9,6 +9,8 @@ import 'package:vm_service/vm_service.dart';
99import '../../shared/primitives/utils.dart' ;
1010import '../../shared/ui/search.dart' ;
1111
12+ export 'package:vm_service/vm_service.dart' show WebSocketEvent;
13+
1214abstract class NetworkRequest
1315 with ChangeNotifier , SearchableDataMixin , Serializable {
1416 String get method;
@@ -239,3 +241,153 @@ extension SocketExtension on List<Socket> {
239241 return map ((socket) => socket._socket).toList ();
240242 }
241243}
244+
245+ class WebSocket extends NetworkRequest {
246+ WebSocket (this ._connection);
247+
248+ factory WebSocket .fromJson (Map <String , Object ?> json) {
249+ return WebSocket (
250+ WebSocketConnection (
251+ isolateId: json['isolateId' ] as String ,
252+ id: json['connectionId' ] as String ,
253+ uri: Uri .parse (json['uri' ] as String ),
254+ protocol: json['protocol' ] as String ? ,
255+ state: json['state' ] as String ,
256+ connectTimestamp: DateTime .parse (json['connectTimestamp' ] as String ),
257+ openTimestamp: json['openTimestamp' ] != null
258+ ? DateTime .parse (json['openTimestamp' ] as String )
259+ : null ,
260+ closeTimestamp: json['closeTimestamp' ] != null
261+ ? DateTime .parse (json['closeTimestamp' ] as String )
262+ : null ,
263+ bytesSent: json['bytesSent' ] as int ,
264+ bytesReceived: json['bytesReceived' ] as int ,
265+ framesSent: json['framesSent' ] as int ,
266+ framesReceived: json['framesReceived' ] as int ,
267+ pingCount: json['pingCount' ] as int ,
268+ pongCount: json['pongCount' ] as int ,
269+ closeCode: json['closeCode' ] as int ? ,
270+ closeReason: json['closeReason' ] as String ? ,
271+ error: json['error' ] as String ? ,
272+ lastUpdated: DateTime .parse (json['lastUpdated' ] as String ),
273+ events: (json['events' ] as List <Object ?>? ?? [])
274+ .whereType <Map <String , Object ?>>()
275+ .map (WebSocketEvent .parse)
276+ .whereType <WebSocketEvent >()
277+ .toList (),
278+ ),
279+ );
280+ }
281+
282+ WebSocketConnection _connection;
283+ List <WebSocketEvent > get events => _connection.events;
284+
285+ String get connectionId => _connection.id;
286+ String ? get protocol => _connection.protocol;
287+ String get state => _connection.state;
288+
289+ DateTime get lastUpdated => _connection.lastUpdated;
290+
291+ int get bytesSent => _connection.bytesSent;
292+ int get bytesReceived => _connection.bytesReceived;
293+ int get framesSent => _connection.framesSent;
294+ int get framesReceived => _connection.framesReceived;
295+ int get pingCount => _connection.pingCount;
296+ int get pongCount => _connection.pongCount;
297+ int ? get closeCode => _connection.closeCode;
298+ String ? get closeReason => _connection.closeReason;
299+ String ? get error => _connection.error;
300+
301+ void update (WebSocket other) {
302+ _connection = other._connection;
303+ notifyListeners ();
304+ }
305+
306+ @override
307+ String get id => 'websocket:${_connection .isolateId }:${_connection .id }' ;
308+
309+ @override
310+ String get method => 'WEBSOCKET' ;
311+
312+ @override
313+ String get uri => _connection.uri.toString ();
314+
315+ @override
316+ String ? get contentType => 'websocket' ;
317+
318+ @override
319+ String get type => 'WebSocket' ;
320+
321+ @override
322+ Duration ? get duration =>
323+ _connection.closeTimestamp? .difference (_connection.connectTimestamp);
324+
325+ @override
326+ DateTime get startTimestamp => _connection.connectTimestamp;
327+
328+ @override
329+ DateTime ? get endTimestamp => _connection.closeTimestamp;
330+
331+ @override
332+ String get status => _connection.state;
333+
334+ @override
335+ int ? get port => _connection.uri.hasPort ? _connection.uri.port : null ;
336+
337+ @override
338+ int get requestBytes => _connection.bytesSent;
339+
340+ @override
341+ int get responseBytes => _connection.bytesReceived;
342+
343+ @override
344+ bool get didFail => _connection.state == 'error' ;
345+
346+ @override
347+ bool get inProgress =>
348+ _connection.state != 'closed' && _connection.state != 'error' ;
349+
350+ @override
351+ bool operator == (Object other) => other is WebSocket && id == other.id;
352+
353+ @override
354+ int get hashCode => id.hashCode;
355+
356+ @override
357+ Map <String , Object ?> toJson () {
358+ return {
359+ 'isolateId' : _connection.isolateId,
360+ 'connectionId' : connectionId,
361+ 'uri' : uri,
362+ 'protocol' : protocol,
363+ 'state' : state,
364+ 'connectTimestamp' : startTimestamp.toIso8601String (),
365+ 'openTimestamp' : _connection.openTimestamp? .toIso8601String (),
366+ 'closeTimestamp' : endTimestamp? .toIso8601String (),
367+ 'bytesSent' : bytesSent,
368+ 'bytesReceived' : bytesReceived,
369+ 'framesSent' : framesSent,
370+ 'framesReceived' : framesReceived,
371+ 'pingCount' : pingCount,
372+ 'pongCount' : pongCount,
373+ 'closeCode' : closeCode,
374+ 'closeReason' : closeReason,
375+ 'error' : error,
376+ 'lastUpdated' : lastUpdated.toIso8601String (),
377+ 'events' : events
378+ .map (
379+ (event) => {
380+ 'timestamp' : event.timestamp.microsecondsSinceEpoch,
381+ 'event' : event.event,
382+ 'frameNumber' : event.frameNumber,
383+ 'direction' : event.direction,
384+ 'opcode' : event.opcode,
385+ 'payloadSize' : event.payloadSize,
386+ 'errorType' : event.errorType,
387+ 'errorMessage' : event.errorMessage,
388+ },
389+ )
390+ .toList (),
391+ };
392+ }
393+ }
0 commit comments