首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用mqtt_client连接到本地主机

无法使用mqtt_client连接到本地主机
EN

Stack Overflow用户
提问于 2020-12-28 03:23:10
回答 1查看 777关注 0票数 2

我在当地有个蚊子经纪人。手机上的应用程序(不是模拟器)。我正在尝试通过WebSockets连接到本地代理,但没有成功。

我在文档中找到了一些示例,但是当我更改URL并切换到WebSockets时,我无法连接。

一只手是值得感谢的。

代码语言:javascript
复制
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}');
  });

}

我在这里做错了什么

我得到的错误

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2021-01-06 09:26:42

我打赌这是因为您没有在行中指定客户端名称和协议前缀ws://

代码语言:javascript
复制
final client = MqttServerClient('ws://192.168.137.231', '');

改为使用以下命令尝试

代码语言:javascript
复制
final client = MqttServerClient('192.168.137.231', 'client01');

我认为这就是错误消息with return code of noneSpecified应该告诉你的部分。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65469592

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档