Skip to content

Commit

Permalink
feat: add worldstate websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
SlayerOrnstein committed Jan 28, 2024
1 parent 2e502ed commit bbc201b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/src/clients/worldstate_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:convert';
import 'package:compute/compute.dart';
import 'package:meta/meta.dart';
import 'package:warframestat_client/warframestat_client.dart';
import 'package:web_socket_client/web_socket_client.dart';

/// {@template warframestat_client}
/// Exposes all endpoints pertaining to worldstate.
Expand All @@ -11,6 +12,20 @@ class WorldstateClient extends WarframestatClient {
/// {@macro warframestat_client}
WorldstateClient({super.language, super.ua, super.client});

/// Opens a websocket connection for worldstate updates.
Stream<Worldstate> worldstateWebSocket([WebSocket? webSocket]) {
final ws = Uri.parse('ws://api.warframestat.us/socket');
final socket = webSocket ?? WebSocket(ws);

return socket.messages
.map((event) => json.decode(event as String) as Map<String, dynamic>)
.where((event) => event['event'] == 'ws:update')
.map((event) => event['packet'] as Map<String, dynamic>)
.where((event) => event['language'] == language.name)
.map((event) =>
Worldstate.fromJson(event['data'] as Map<String, dynamic>));
}

/// Retrives a fully translated [Worldstate].
Future<Worldstate> fetchWorldstate() async {
final worldstate = await _get<Map<String, dynamic>>('');
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies:
http: ">=1.1.0 <2.0.0"
json_annotation: ">=4.8.1 <5.0.0"
meta: ">=1.9.0 <2.0.0"
web_socket_client: ^0.1.1

dev_dependencies:
build_runner: ^2.4.8
Expand Down

0 comments on commit bbc201b

Please sign in to comment.