Delphi-TMQTT2
Delphi-TMQTT2 copied to clipboard
about use mosca mqtt server,cannot subscribe
1、Use mosca mqtt server https://github.com/mcollina/mosca 2、code
create:
MQTT := TMQTT.Create(jsonSetting.S['mip'], jsonSetting.I['mport']); MQTT.WillTopic := 'pubmsg'; MQTT.WillMsg := 'Broker died!'; // Events MQTT.OnConnAck := GotConnAck; MQTT.OnPublish := GotPub; MQTT.OnPingResp := GotPingResp; MQTT.OnSubAck := GotSubAck; MQTT.OnUnSubAck := GotUnSubAck; MQTT.OnPubAck := GotPubAck; if MQTT.Connect then Memo1.Lines.Add(Format('Connected to %s on %d', [jsonSetting.S['mip'], jsonSetting.I['mport']])) else Memo1.Lines.Add('Failed to connect');
Publish:
if (Assigned(MQTT)) then begin MQTT.Publish('pubmsg', 'dddddd'); Memo1.Lines.Add('Published'); end; publish success!
Subscribe
if (Assigned(MQTT)) then begin MQTT.Subscribe('pubmsg', 2); Memo1.Lines.Add('Subscribe'); end; Subscribe fail! mosca error: {"name":"mosca","hostname":"debian","pid":3457,"client":"TMQTT166","level":40,"err":{"message":"wrong subscribe header","name":"Error","stack":"Error: wrong subscribe header\n at Parser._parseSubscribe (/root/msc/node_modules/mosca/node_modules/mqtt-connection/node_modules/mqtt-packet/parser.js:279:31)\n at Parser._parsePayload (/root/msc/node_modules/mosca/node_modules/mqtt-connection/node_modules/mqtt-packet/parser.js:126:14)\n at Parser.parse (/root/msc/node_modules/mosca/node_modules/mqtt-connection/node_modules/mqtt-packet/parser.js:43:48)\n at DestroyableTransform.process as _transform\n at DestroyableTransform.Transform._read (/root/msc/node_modules/mosca/node_modules/mqtt-connection/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:184:10)\n at DestroyableTransform.Transform._write (/root/msc/node_modules/mosca/node_modules/mqtt-connection/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:172:12)\n at doWrite (/root/msc/node_modules/mosca/node_modules/mqtt-connection/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:237:10)\n at writeOrBuffer (/root/msc/node_modules/mosca/node_modules/mqtt-connection/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:227:5)\n at DestroyableTransform.Writable.write (/root/msc/node_modules/mosca/node_modules/mqtt-connection/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:194:11)\n at Socket.ondata (_stream_readable.js:528:20)"},"msg":"wrong subscribe header","time":"2016-04-05T08:20:39.236Z","v":0}