Skip to content

Commit

Permalink
Support WebAssembly compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
NotTsunami committed Aug 8, 2024
1 parent c50e39e commit e22ea08
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 269 deletions.
236 changes: 0 additions & 236 deletions example/aws_iot_cognito.dart

This file was deleted.

52 changes: 28 additions & 24 deletions example/aws_iot_cognito_amplify.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import 'package:aws_signature_v4/aws_signature_v4.dart';

import 'package:mqtt_client/mqtt_server_client.dart';
import 'package:mqtt_client/mqtt_client.dart';
//HTTP import 'package:http/http.dart';
import 'package:sigv4/sigv4.dart';
import 'package:http/http.dart';

/// An example of connecting to the AWS IoT Core MQTT broker and publishing to a devices topic.
/// This example uses MQTT over Websockets with AWS IAM Credentials
Expand All @@ -25,10 +24,6 @@ import 'package:sigv4/sigv4.dart';
/// https://docs.aws.amazon.com/iot/latest/developerguide/protocols.html, please read this
/// before setting up and running this example.
/// Note the dependency on the http package has been removed from the client, as such lines below
/// depending on this are commented out. If you wish to run this example please re add package http
/// at version 1.2.1 to the pubspec.yaml and uncomment lines starting with HTTP.
// This function is based on the one from package flutter-aws-iot, but adapted slightly
String getWebSocketURL(
{required String accessKey,
Expand Down Expand Up @@ -71,31 +66,40 @@ Future<bool> attachPolicy(
required String iotApiUrl,
required String region,
required String policyName}) async {
final sigv4Client = Sigv4Client(
keyId: accessKey,
accessKey: secretKey,
sessionToken: sessionToken,
region: region,
serviceName: 'execute-api');
final creds = AWSCredentials(accessKey, secretKey, sessionToken);
final signer = AWSSigV4Signer(
credentialsProvider: AWSCredentialsProvider(creds),
);

final scope = AWSCredentialScope(region: region, service: AWSService.iot);

final body = json.encode({'target': identityId});

//HTTP remove the two lines below.
print(sigv4Client);
print(body);
final request = AWSHttpRequest(
method: AWSHttpMethod.put,
uri: Uri.parse('$iotApiUrl/$policyName'),
headers: {
'Content-Type': 'application/json',
},
body: body.codeUnits,
);

//HTTPfinal request =
//HTTPsigv4Client.request('$iotApiUrl/$policyName', method: 'PUT', body: body);
final signedRequest = await signer.sign(
request,
credentialScope: scope,
);

//HTTP var result = await put(request.url, headers: request.headers, body: body);
final result = await put(
signedRequest.uri,
headers: signedRequest.headers,
body: signedRequest.body,
);

//HTTPf (result.statusCode != 200) {
//HTTPprint('Error attaching IoT Policy ${result.body}');
//HTTP}
if (result.statusCode != 200) {
print('Error attaching IoT Policy ${result.body}');
}

//HTTPreturn result.statusCode == 200;
//HTTP remove the line below
return true;
return result.statusCode == 200;
}

Future<int> main() async {
Expand Down
2 changes: 1 addition & 1 deletion example/mqtt_client_universal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// The following scheme can be used conditionally import either the server or browser client
// automatically.
//
// import 'server.dart' if (dart.library.html) 'browser.dart' as mqttsetup;
// import 'server.dart' if (dart.library.js_interop) 'browser.dart' as mqttsetup;
// ...
// var client = mqttsetup.setup(serverAddress, uniqueID, port);
//
Expand Down
3 changes: 2 additions & 1 deletion lib/mqtt_browser_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
library mqtt_browser_client;

import 'dart:async';
import 'package:universal_html/html.dart';
import 'dart:js_interop';
import 'dart:typed_data';
import 'package:event_bus/event_bus.dart' as events;
import 'package:typed_data/typed_data.dart' as typed;
import 'package:web/web.dart';
import 'mqtt_client.dart';

part 'src/mqtt_browser_client.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class MqttBrowserWsConnection extends MqttBrowserConnection<WebSocket> {
MqttLogger.log('MqttBrowserWsConnection::connect - WS URL is $uriString');
try {
// Connect and save the socket.
final client = WebSocket(uriString, protocols);
final client = WebSocket(
uriString, (protocols.toList()).map((e) => e.toJS).toList().toJS);
this.client = client;
client.binaryType = 'arraybuffer';
messageStream = MqttByteBuffer(typed.Uint8Buffer());
Expand Down Expand Up @@ -121,7 +122,8 @@ class MqttBrowserWsConnection extends MqttBrowserConnection<WebSocket> {
'MqttBrowserWsConnection::connectAuto - WS URL is $uriString');
try {
// Connect and save the socket.
final client = WebSocket(uriString, protocols);
final client = WebSocket(
uriString, (protocols.toList()).map((e) => e.toJS).toList().toJS);
this.client = client;
client.binaryType = 'arraybuffer';
messageStream = MqttByteBuffer(typed.Uint8Buffer());
Expand Down Expand Up @@ -221,6 +223,6 @@ class MqttBrowserWsConnection extends MqttBrowserConnection<WebSocket> {
final messageBytes = message.read(message.length);
var buffer = messageBytes.buffer;
var bData = ByteData.view(buffer);
client?.sendTypedData(bData);
client?.send(bData.jsify()!);
}
}
Loading

0 comments on commit e22ea08

Please sign in to comment.