jesusslim / mqttclient Goto Github PK
View Code? Open in Web Editor NEWphp mqtt client
License: MIT License
php mqtt client
License: MIT License
4g的内存,这情况的话,基本上3天就得重启一次
调用代码如下:
`
public function handle()
{
$server = config('mqtt.host');
$port = config('mqtt.port');
$username = config('mqtt.username');
$password = config('mqtt.password');
$client_id = "backendService";
ini_set('memory_limit', '-1');
$client = new Client($server, $port, $client_id);
$client->setMqttVersion(MqttVersion::V311);
$client->setAuth($username, $password);
$client->setKeepAlive(60);
$client->setStore(new Store());
$client->setLogger(new Logger());
$client->setMaxReconnectTimesWhenError(0);
$client->setPackageMaxLength(1024*1024*256);
$client->setTopics([
new Topic('$sys/client/connect', function ($msg) {
$this->clientConnect($msg);
}, 1),
new Topic('$sys/client/disconnect', function ($msg) {
$this->clientDisconnect($msg);
}, 1),
new Topic('$sys/app/callback', function ($msg) use ($client) {
$this->orderCallback($msg, $client);
}, 0),
]);
$client->setTimer(1000, function ($timer_id, $mqtt) {
$this->publishOrderStartData($mqtt);
});
$client->setTimer(1100, function ($timer_id, $mqtt) {
$this->publishOrderStopData($mqtt);
});
$client->setTimer(1, function ($timer_id, $mqtt) use ($client) {
$client->on(ClientTriggers::SOCKET_FIRST_CLOSE, function () use ($client) {
$this->resetAllDevice();
Cache::forever('mqtt_server_is_connected', false);
Log::debug('mqtt_server_is_connected', [cache('mqtt_server_is_connected')]);
});
$client->on(ClientTriggers::RECEIVE_CONNACK, function () use ($client) {
Cache::forever('mqtt_server_is_connected', true);
Log::debug('mqtt_server_is_connected', [cache('mqtt_server_is_connected')]);
});
$client->on(ClientTriggers::DISCONNECT, function () use ($client) {
Cache::forever('mqtt_server_is_connected', false);
Log::debug('mqtt_server_is_connected', [cache('mqtt_server_is_connected')]);
});
$client->on(ClientTriggers::RECEIVE_PINGRESP, function () {
Cache::forever('mqtt_server_is_connected', true);
});
});
$client->connect();
}`
Using the example provided:
$r = new \mqttclient\src\swoole\MqttClient('127.0.0.1', 1883, 10017);
$r->setAuth('username','password');
...
$r->connect();
The 'username' and 'password' are never sent to the MQTT server. With using Wireshark, this can be confirmed. Message payload never has 'username' nor 'password' in it.
Mosquitto then reports on connect:
1525541579: New connection from 127.0.0.1 on port 1883.
1525541579: Socket error on client <unknown>, disconnecting.
Connecting with some other client does work when using same username and password. With Wireshark, username and password can be seen in the payload.
How can you authenticate using username and password?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.