0

我对https://github.com/adamvr/MQTT.js的服务器做错了吗?我看到client.id进来很好。其他一切都是未定义的;我使用 3.1 用户/通行证与另一个代理验证了 MQTT 客户端的信息,所以我知道问题不存在。

    clients = { }

    server = mqtt.createServer (client) =>
      console.log 'Broker:mqtt:createServer'

      # Catch when client connects
      client.on 'connect', (packet) =>
        console.log 'Broker:connect'

        client.connack
          returnCode: 0

        client.id = packet.client

        console.log 'version: ' + client.versionNum # undefined
        console.log 'client: ' + client.id # COMES THROUGH FINE!
        console.log 'username: ' + client.username # undefined
        console.log 'password: ' + client.password # udefined

        clients[client.id] = client

        console.log 'clients: ' + JSON.stringify clients

   ...

    server.listen 1883

我还在项目下创建了一个问题:https ://github.com/adamvr/MQTT.js/issues/22

4

1 回答 1

1

您在事件中收到的客户端对象connect不包含您正在寻找的任何参数。具有正确值的唯一原因client.id是您在上面的行中设置了它。与事件一起传递的packet包含属性。

clients = { }

server = mqtt.createServer (client) =>
  console.log 'Broker:mqtt:createServer'

  # Catch when client connects
  client.on 'connect', (packet) =>
    console.log 'Broker:connect'

    client.connack
      returnCode: 0

    client.id = packet.client

    console.log 'version: ' + packet.versionNum
    console.log 'client: ' + packet.client
    console.log 'username: ' + packet.username
    console.log 'password: ' + packet.password

    clients[client.id] = client

    console.log 'clients: ' + JSON.stringify clients

 ...

server.listen 1883
于 2012-07-29T09:07:07.317 回答