JamesBowen
19+ years progress programming and still learning.
I'm writing an a MQTT client and I'm getting stuck at the first hurdle. The problem I am having is the MQTT broker is disconnecting the SOCKET connection without any returning any data first.
Here is my code. You should be able to just copy and past the code into your own Progress editor and run it.
What it should do is perform a socket connection, send 10 bytes of data and expecting some data (should be 4 bytes) to be returned. However the server just disconnects????
I guessing the problem is in the Header I send to the MQTT broker but I can't see what the problem is.
MQTT 3.1.1 technical specifications.
MQTT Version 3.1.1
Here is my code. You should be able to just copy and past the code into your own Progress editor and run it.
What it should do is perform a socket connection, send 10 bytes of data and expecting some data (should be 4 bytes) to be returned. However the server just disconnects????
I guessing the problem is in the Header I send to the MQTT broker but I can't see what the problem is.
Code:
define variable connectHeader as memptr no-undo.
define variable connectFlags as integer no-undo.
define variable timethen as datetime.
SET-BYTE-ORDER(connectHeader ) = LITTLE-ENDIAN.
set-size(connectHeader) = 0.
set-size(connectHeader) = 10. /** connected Header is 10 bytes in size **/
/** 3.1.2.1 Protocol Name **/
put-byte(connectHeader, 1) = 0x00.
put-byte(connectHeader, 2) = 0x04.
put-byte(connectHeader, 3) = asc('M':U).
put-byte(connectHeader, 4) = asc('Q':U).
put-byte(connectHeader, 5) = asc('T':U).
put-byte(connectHeader, 6) = asc('T':U).
/** 3.1.2.2 Protocol Level **/
put-byte(connectHeader, 7) = 0x04. /** MQTT Version 3.1.1**/
/** 3.1.2.3 Connect Flags **/
put-bits(connectFlags, 8, 1) = 0x01. /** Username Flag **/
put-bits(connectFlags, 7, 1) = 0x01. /** Password Flag **/
put-bits(connectFlags, 6, 1) = 0x00. /** Will Retain **/
put-bits(connectFlags, 4, 2) = 0x01. /** Will QoS, 2 Bits **/
put-bits(connectFlags, 3, 1) = 0x01. /** Will Flag **/
put-bits(connectFlags, 2, 1) = 0x01. /** Clean Session **/
put-bits(connectFlags, 1, 1) = 0x00. /** Reserved **/
put-byte(connectHeader, 8) = connectFlags.
/** 3.1.2.10 Keep Alive **/
put-byte(connectHeader, 9) = 0x0.
put-byte(connectHeader, 10) = 0x10.
/* PUT-SHORT(connectHeader, 9) = 0x0A. /** 16-bit/2 bytes short word of the number of seconds. A=10**/ */
copy-lob from object connectHeader to file "./connectHeader.txt".
define variable mqttClinet as handle.
CREATE SOCKET mqttClinet .
mqttClinet:SET-SOCKET-OPTION("SO-KEEPALIVE", "true").
mqttClinet:SET-SOCKET-OPTION("SO-RCVTIMEO", "10").
mqttClinet:connect("-H broker.hivemq.com -S 1883").
if mqttClinet:connected() then
Do:
message "Connected to MQTT server.."
mqttClinet:write(connectHeader, 1, get-size(connectHeader)) /** write the connect header to the MQTT server **/
mqttClinet:bytes-written.
End.
timethen = now.
pause 0.5.
define variable packetLength as integer no-undo.
message
mqttClinet:connected().
WAIT-FOR-SERVER:
do while mqttClinet:connected():
packetLength = mqttClinet:get-bytes-available().
if packetLength > 0 then
do:
message packetLength
view-as alert-box info.
end.
if absolute( interval(timethen, now, 'seconds') ) ge 10 then
leave WAIT-FOR-SERVER.
end.
if not mqttClinet:connected() then
message "Server Disconnected"
view-as alert-box error.
else
mqttClinet:disconnect().
delete object mqttClinet.
MQTT 3.1.1 technical specifications.
MQTT Version 3.1.1