-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtestclass.pas
69 lines (56 loc) · 1.46 KB
/
testclass.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
{
Subscribing to all topics and printing out incoming messages
Simple test code for the mqttclass unit
Copyright (c) 2019 Karoly Balogh <[email protected]>
See the LICENSE file for licensing details.
}
{$MODE OBJFPC}
program testclass;
uses
{$IFDEF HASUNIX}
cthreads,
{$ENDIF}
ctypes, mosquitto, mqttclass;
type
TMyMQTTConnection = class(TMQTTConnection)
procedure MyOnMessage(const payload: Pmosquitto_message);
end;
procedure TMyMQTTConnection.MyOnMessage(const payload: Pmosquitto_message);
var
msg: ansistring;
begin
msg:='';
with payload^ do
begin
{ Note that MQTT messages can be binary, but for this test case we just
assume they're printable text, as a test }
SetLength(msg,payloadlen);
Move(payload^,msg[1],payloadlen);
writeln('Topic: [',topic,'] - Message: [',msg,']');
end;
end;
var
mqtt: TMyMQTTConnection;
config: TMQTTConfig;
begin
writeln('Press ENTER to quit.');
FillChar(config, sizeof(config), 0);
with config do
begin
port:=1883;
hostname:='localhost';
keepalives:=60;
end;
{ use MOSQ_LOG_NODEBUG to disable debug logging }
mqtt:=TMyMQTTConnection.Create('TEST',config,MOSQ_LOG_ALL);
try
{ This could also go to a custom constructor of the class,
for more complicated setups. }
mqtt.OnMessage:[email protected];
mqtt.Connect;
mqtt.Subscribe('#',0); { Subscribe to all topics }
readln;
except
end;
mqtt.Free;
end.