Skip to content

Commit

Permalink
Issue 531
Browse files Browse the repository at this point in the history
  • Loading branch information
shamblett committed Jul 9, 2024
1 parent 89857f1 commit 5fa1b56
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
6 changes: 5 additions & 1 deletion example/mqtt_server_client_websocket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ final client = MqttServerClient('ws://test.mosquitto.org', '');
Future<int> main() async {
client.useWebSocket = true;
client.port = 8080; // ( or whatever your ws port is)
/// You can also supply your own websocket protocol list or disable this feature using the websocketProtocols
/// You can supply your own websocket protocol list or disable this feature using the websocketProtocols
/// setter, read the API docs for further details here, the vast majority of brokers will support the client default
/// list so in most cases you can ignore this. Mosquito needs the single default setting.
client.websocketProtocols = MqttClientConstants.protocolsSingleDefault;

/// You can supply a list of headers to send with the websocket request.
/// Some brokers are known to need their own special headers for auth etc.
client.websocketHeader = {'sjh-test' : 'SJH'};

/// Set logging on if needed, defaults to off
client.logging(on: false);

Expand Down
4 changes: 4 additions & 0 deletions example/mqtt_server_client_websocket_secure.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ Future<int> main() async {
/// list so in most cases you can ignore this. Mosquito needs the single default setting.
client.websocketProtocols = MqttClientConstants.protocolsSingleDefault;

/// You can supply a list of headers to send with the websocket request.
/// Some brokers are known to need their own special headers for auth etc.
client.websocketHeader = {'sjh-test': 'SJH'};

/// Set logging on if needed, defaults to off
client.logging(on: false);

Expand Down
3 changes: 2 additions & 1 deletion test/mqtt_client_connection_ws_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ void main() {
final clientEventBus = events.EventBus();
final ch = SynchronousMqttServerConnectionHandler(clientEventBus,
maxConnectionAttempts: 3, socketOptions: socketOptions);
MqttLogger.loggingOn = true;
MqttLogger.loggingOn = false;
ch.useWebSocket = true;
ch.websocketProtocols = <String>['SJHprotocol'];
ch.websocketHeaders = {'Origin': 'SJH'};
brokerWs.setMessageHandler = messageHandlerConnect;
await ch.connect(mockBrokerAddressWs, mockBrokerPortWs,
MqttConnectMessage().withClientIdentifier(testClientId));
Expand Down
15 changes: 15 additions & 0 deletions test/support/mqtt_client_mockbroker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ class MockBrokerWs {
'Mockbroker WS server is running on http://${server.address.address}:$port/');
server.listen((HttpRequest request) {
if (request.uri.path == '/ws') {
final websocketHeader = request.headers.value('Origin');
if (websocketHeader != 'SJH') {
return completer.completeError((request) => websocketHeader);
} else {
print(
'Mockbroker WS server::listen - Origin header is correctly set');
}
final websocketProtocol =
request.headers.value('sec-websocket-protocol');
if (websocketProtocol != 'SJHprotocol') {
return completer.completeError((request) => websocketProtocol);
} else {
print(
'Mockbroker WS server::listen - WS protocol is correctly set');
}
WebSocketTransformer.upgrade(request).then((WebSocket websocket) {
_webSocket = websocket;
websocket.listen(_handleMessage);
Expand Down
15 changes: 15 additions & 0 deletions test/support/mqtt_client_ws_broker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ class MockBrokerWs {
print(
'Mockbroker WS server::listen - request received ${request.uri.path}');
if (request.uri.path == '/ws') {
final websocketHeader = request.headers.value('Origin');
if (websocketHeader != 'SJH') {
return completer.completeError((request) => websocketHeader);
} else {
print(
'Mockbroker WS server::listen - Origin header is correctly set');
}
final websocketProtocol =
request.headers.value('sec-websocket-protocol');
if (websocketProtocol != 'SJHprotocol') {
return completer.completeError((request) => websocketProtocol);
} else {
print(
'Mockbroker WS server::listen - WS protocol is correctly set');
}
print('Mockbroker WS server::listen - upgrading');
WebSocketTransformer.upgrade(request).then((WebSocket websocket) {
_webSocket = websocket;
Expand Down

0 comments on commit 5fa1b56

Please sign in to comment.