diff --git a/lib/core/ros.dart b/lib/core/ros.dart index 1f63a5c..ef1c5be 100644 --- a/lib/core/ros.dart +++ b/lib/core/ros.dart @@ -23,13 +23,16 @@ enum TopicStatus { SUBSCRIBED, UNSUBSCRIBED, PUBLISHER, ADVERTISED, UNADVERTISED class Ros { /// Initializes the [_statusController] as a broadcast. /// The [url] of the ROS node can be optionally specified at this point. - Ros({this.url}) { + Ros({this.url, this.headers}) { _statusController = StreamController.broadcast(); } /// The url of ROS node running the rosbridge server. dynamic url; + /// The socket connection header. + Map headers; + /// Total subscribers to ever connect. int subscribers = 0; @@ -64,12 +67,15 @@ class Ros { Status status = Status.NONE; /// Connect to the ROS node, the [url] can override what was provided in the constructor. - void connect({dynamic url}) { + void connect({dynamic url, Map headers}) { this.url = url ?? this.url; url ??= this.url; + this.headers = headers ?? this.headers; + headers ??= this.headers; + try { // Initialize the connection to the ROS node with a Websocket channel. - _channel = initializeWebSocketChannel(url); + _channel = initializeWebSocketChannel(url, headers); stream = _channel.stream.asBroadcastStream().map((raw) => json.decode(raw)); // Update the connection status. status = Status.CONNECTED; diff --git a/lib/core/ros_html.dart b/lib/core/ros_html.dart index 6a4547a..337fde8 100644 --- a/lib/core/ros_html.dart +++ b/lib/core/ros_html.dart @@ -1,6 +1,6 @@ import 'package:web_socket_channel/web_socket_channel.dart'; import 'package:web_socket_channel/html.dart'; -WebSocketChannel initializeWebSocketChannel(String url) { +WebSocketChannel initializeWebSocketChannel(String url, Map headers) { return HtmlWebSocketChannel.connect(url); } diff --git a/lib/core/ros_io.dart b/lib/core/ros_io.dart index e4425fe..ece9b60 100644 --- a/lib/core/ros_io.dart +++ b/lib/core/ros_io.dart @@ -1,6 +1,6 @@ import 'package:web_socket_channel/web_socket_channel.dart'; import 'package:web_socket_channel/io.dart'; -WebSocketChannel initializeWebSocketChannel(String url) { - return IOWebSocketChannel.connect(url); +WebSocketChannel initializeWebSocketChannel(String url, Map headers) { + return IOWebSocketChannel.connect(url, headers: headers); } diff --git a/lib/core/ros_stub.dart b/lib/core/ros_stub.dart index e72c0d2..d09f5c8 100644 --- a/lib/core/ros_stub.dart +++ b/lib/core/ros_stub.dart @@ -1,6 +1,6 @@ import 'package:web_socket_channel/web_socket_channel.dart'; //Implemented in `ros_html.dart` and `ros_io.dart` -WebSocketChannel initializeWebSocketChannel(String url) { +WebSocketChannel initializeWebSocketChannel(String url, Map headers) { throw UnsupportedError('Cannot create a web socket channel without dart:html or dart:io.'); } diff --git a/pubspec.lock b/pubspec.lock index b0a4888..f627a9c 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1,5 +1,5 @@ # Generated by pub -# See https://www.dartlang.org/tools/pub/glossary#lockfile +# See https://dart.dev/tools/pub/glossary#lockfile packages: analyzer: dependency: transitive