-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
server.php
147 lines (134 loc) · 4.82 KB
/
server.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/**
* This file is part of Simps.
*
* @link https://github.com/simps/mqtt
* @contact Lu Fei <[email protected]>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/
include_once __DIR__ . '/bootstrap.php';
use Simps\MQTT\Protocol\Types;
use Simps\MQTT\Protocol\V3;
use Simps\MQTT\Tools\Common;
$server = new Swoole\Server('127.0.0.1', 1883, SWOOLE_BASE);
$server->set(
[
'open_mqtt_protocol' => true,
'worker_num' => 2,
'package_max_length' => 2 * 1024 * 1024,
]
);
$server->on('connect', function ($server, $fd) {
echo "Client #{$fd}: Connect.\n";
});
$server->on('receive', function (Swoole\Server $server, $fd, $reactorId, $data) {
try {
// debug
// Common::printf($data);
$data = V3::unpack($data);
if (is_array($data) && isset($data['type'])) {
switch ($data['type']) {
case Types::CONNECT:
// Check protocol_name
if ($data['protocol_name'] != 'MQTT') {
$server->close($fd);
return false;
}
// Check connection information, etc.
$server->send(
$fd,
V3::pack(
[
'type' => Types::CONNACK,
'code' => 0,
'session_present' => 0,
]
)
);
break;
case Types::PINGREQ:
$server->send($fd, V3::pack(['type' => Types::PINGRESP]));
break;
case Types::DISCONNECT:
if ($server->exist($fd)) {
$server->close($fd);
}
break;
case Types::PUBLISH:
// Send to subscribers
foreach ($server->connections as $sub_fd) {
if ($sub_fd != $fd) {
$server->send(
$sub_fd,
V3::pack(
[
'type' => $data['type'],
'topic' => $data['topic'],
'message' => $data['message'],
'dup' => $data['dup'],
'qos' => $data['qos'],
'retain' => $data['retain'],
'message_id' => $data['message_id'] ?? 0,
]
)
);
}
}
if ($data['qos'] === 1) {
$server->send(
$fd,
V3::pack(
[
'type' => Types::PUBACK,
'message_id' => $data['message_id'] ?? 0,
]
)
);
}
break;
case Types::SUBSCRIBE:
$payload = [];
foreach ($data['topics'] as $qos) {
if (is_numeric($qos) && $qos < 3) {
$payload[] = $qos;
} else {
$payload[] = 0x80;
}
}
$server->send(
$fd,
V3::pack(
[
'type' => Types::SUBACK,
'message_id' => $data['message_id'] ?? 0,
'codes' => $payload,
]
)
);
break;
case Types::UNSUBSCRIBE:
$server->send(
$fd,
V3::pack(
[
'type' => Types::UNSUBACK,
'message_id' => $data['message_id'] ?? 0,
]
)
);
break;
}
} else {
$server->close($fd);
}
} catch (\Throwable $e) {
echo "\033[0;31mError: {$e->getMessage()}\033[0m\r\n";
$server->close($fd);
}
});
$server->on('close', function ($server, $fd) {
echo "Client #{$fd}: Close.\n";
});
$server->start();