Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Header support #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/core/ros.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Status>.broadcast();
}

/// The url of ROS node running the rosbridge server.
dynamic url;

/// The socket connection header.
Map<String, dynamic> headers;

/// Total subscribers to ever connect.
int subscribers = 0;

Expand Down Expand Up @@ -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<String, dynamic> 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;
Expand Down
2 changes: 1 addition & 1 deletion lib/core/ros_html.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic> headers) {
return HtmlWebSocketChannel.connect(url);
}
4 changes: 2 additions & 2 deletions lib/core/ros_io.dart
Original file line number Diff line number Diff line change
@@ -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<String,dynamic> headers) {
return IOWebSocketChannel.connect(url, headers: headers);
}
2 changes: 1 addition & 1 deletion lib/core/ros_stub.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic> headers) {
throw UnsupportedError('Cannot create a web socket channel without dart:html or dart:io.');
}
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
@@ -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
Expand Down