我有一个当地的蚊子经纪人。手机上的应用程序(不是模拟器)。我正在尝试通过 WebSockets 连接到本地代理但没有成功。
我在文档中找到了一些示例,但是当我更改 URL 并切换到 WebSockets 时,我无法连接。
一只手被赞赏。
import 'dart:async';
import 'package:mqtt_client/mqtt_client.dart';
import 'package:mqtt_client/mqtt_server_client.dart';
final client = MqttServerClient('ws://192.168.137.231', '');
Future<int> main() async {
/// Set logging on if needed, defaults to off
client.logging(on: false);
client.keepAlivePeriod = 20;
client.port = 8080;
client.useWebSocket = true;
client.onDisconnected = onDisconnected;
client.onConnected = onConnected;
client.onSubscribed = onSubscribed;
client.pongCallback = pong;
final connMess = MqttConnectMessage()
.withClientIdentifier('client_id')
.keepAliveFor(60) // Must agree with the keep alive set above or not set
// .withWillTopic('/busline/201') // If you set this you must set a will message
.withWillMessage('My Will message')
.startClean() // Non persistent session for testing
.withWillQos(MqttQos.atLeastOnce);
print('EXAMPLE::Mosquitto client connecting....');
client.connectionMessage = connMess;
try {
await client.connect();
} on Exception catch (e) {
print('EXAMPLE::client exception - $e');
client.disconnect();
}
/// Check we are connected
if (client.connectionStatus.state == MqttConnectionState.connected) {
print('EXAMPLE::Mosquitto client connected');
} else {
/// Use status here rather than state if you also want the broker return code.
print(
'EXAMPLE::ERROR Mosquitto client connection failed - disconnecting, status is ${client.connectionStatus}');
client.disconnect();
return -1;
}
/// Ok, lets try a subscription
print('EXAMPLE::Subscribing to the test/lol topic');
const topic = '/busline/201'; // Not a wildcard topic
client.subscribe(topic, MqttQos.atMostOnce);
client.updates.listen((List<MqttReceivedMessage<MqttMessage>> c) {
final MqttPublishMessage recMess = c[0].payload;
final pt =
MqttPublishPayload.bytesToStringAsString(recMess.payload.message);
print(
'EXAMPLE::Change notification:: topic is <${c[0].topic}>, payload is <-- $pt -->');
print('');
});
client.published.listen((MqttPublishMessage message) {
print(
'EXAMPLE::Published notification:: topic is ${message.variableHeader.topicName}, with Qos ${message.header.qos}');
});
}
我在这里做错了什么
我回来的错误
I/flutter ( 5031): EXAMPLE::Mosquitto client connecting...
I/flutter ( 5031): EXAMPLE::OnDisconnected client callback - Client disconnection
I/flutter ( 5031): EXAMPLE::client exception - SocketException: OS Error: Connection refused, errno = 111, address = 192.168.43.56, port = 49839
I/flutter ( 5031): EXAMPLE::OnDisconnected client callback - Client disconnection
I/flutter ( 5031): EXAMPLE::OnDisconnected callback is solicited, this is correct
I/flutter ( 5031): EXAMPLE::ERROR Mosquitto client connection failed - disconnecting, status is Connection status is disconnected with return code of noneSpecified and a disconnection origin of solicited
I/flutter ( 5031): EXAMPLE::OnDisconnected client callback - Client disconnection
I/flutter ( 5031): EXAMPLE::OnDisconnected callback is solicited, this is correct