-
Notifications
You must be signed in to change notification settings - Fork 8
/
lwt.php
47 lines (42 loc) · 1.29 KB
/
lwt.php
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
<?php
/**
* Created by PhpStorm.
* Company Anka Technologies
* User: TNChalise <[email protected]>
* Time: 2:22 PM
*/
$client = new Mosquitto\Client();
$client->onConnect('connect');
$client->onDisconnect('disconnect');
$client->onSubscribe('subscribe');
$client->onMessage('message');
/*
*//*
|--------------------------------------------------------------------------
| set will before connection.
|--------------------------------------------------------------------------
|
| Will message are crucial while detecting client status. See, LWT for more.
|
*/
$client->setWill('mqtt://myapp/clients/123', "Client died :-(", 1, 0);
$client->connect("192.168.0.130", 1883, 5);
$client->subscribe('mqtt://mpapp/chat-messages/123/#', 1);
$client->subscribe('mqtt://myapp/clients/#', 1);
$client->loopForever();
function connect($r)
{
echo "I got code {$r}\n";
}
function subscribe()
{
echo "Subscribed to a topic\n";
}
function message($message)
{
printf("Got a message on topic %s with payload:\n%s\n", $message->topic, $message->payload);
}
function disconnect()
{
echo "Disconnected cleanly\n";
}