Code Monkey home page Code Monkey logo

mqtt-server-incubator's Introduction

MQTT Server

安装

composer require hyperf/mqtt-server-incubator

配置服务

<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  [email protected]
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
use Hyperf\Server\Event;
use Hyperf\Server\Server;

return [
    'mode' => SWOOLE_BASE,
    'servers' => [
        [
            'name' => 'mqtt',
            'type' => Server::SERVER_BASE,
            'host' => '0.0.0.0',
            'port' => 1883,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                Event::ON_RECEIVE => [Hyperf\MqttServer\MQTTServer::class, 'onReceive'],
            ],
        ],
    ],
    'settings' => [
        'enable_coroutine' => true,
        'worker_num' => 4,
        'pid_file' => BASE_PATH . '/runtime/hyperf.pid',
        'open_tcp_nodelay' => true,
        'max_coroutine' => 100000,
        'open_http2_protocol' => true,
        'max_request' => 0,
        'socket_buffer_size' => 2 * 1024 * 1024,
        'package_max_length' => 2 * 1024 * 1024,
    ],
    'callbacks' => [
        Event::ON_BEFORE_START => [Hyperf\Framework\Bootstrap\ServerStartCallback::class, 'beforeStart'],
        Event::ON_WORKER_START => [Hyperf\Framework\Bootstrap\WorkerStartCallback::class, 'onWorkerStart'],
        Event::ON_PIPE_MESSAGE => [Hyperf\Framework\Bootstrap\PipeMessageCallback::class, 'onPipeMessage'],
        Event::ON_WORKER_EXIT => [Hyperf\Framework\Bootstrap\WorkerExitCallback::class, 'onWorkerExit'],
    ],
];

启动服务,我们就可以简单的使用 MQTT 服务了。

自定义事件

组件增加了可以监听 MQTT 服务各个阶段的事件,比如我们写一个 MQTTConnectHandler 用来监听客户端连接。

PUBLISH, SUBSCRIBE 和 UNSUBSCRIBE 三个事件,需要自行实现

<?php

declare(strict_types=1);

namespace App\MQTT\Event;

use Hyperf\HttpMessage\Server\Response;
use Hyperf\MqttServer\Annotation\MQTTConnect;
use Hyperf\MqttServer\Handler\HandlerInterface;
use Psr\Http\Message\ServerRequestInterface;

#[MQTTConnect(priority: 1)]
class MQTTConnectHandler implements HandlerInterface
{
    public function handle(ServerRequestInterface $request, Response $response): Response
    {
        var_dump((string) $request->getBody());
        return $response;
    }
}

重启服务,连接 MQTT 时,便可以得到以下输出。

$ php bin/hyperf.php start
[INFO] TCP Server listening at 0.0.0.0:1883
string(234) "{"type":1,"protocol_name":"MQTT","protocol_level":4,"clean_session":1,"will":{"qos":0,"retain":0,"topic":"simps-mqtt\/user001\/delete","message":"byebye"},"user_name":"","password":"","keep_alive":10,"client_id":"Simps_60e5aa0c4284f"}"

组件支持的事件列表如下:

事件 备注
MQTTConnect 客户端连接时触发
MQTTDisconnect 客户端断开连接时触发
MQTTPingReq
MQTTPublish 客户端发布消息时触发
MQTTSubscribe 客户端订阅时触发
MQTTUnsubscribe 客户端取消订阅时触发

注解支持参数如下

参数 备注
server 指定当前事件对应的服务名
type 事件类型
priority 事件优先级,越大越先执行,默认的事件优先级为 0

mqtt-server-incubator's People

Contributors

limingxinleo avatar sy-records avatar

Stargazers

qypt15 avatar  avatar  avatar Daixs avatar wcz0 avatar  avatar Cloudflying avatar  avatar 继阳 avatar  avatar 千華 avatar chenlei avatar coderZhao avatar Roman Hossain Shaon avatar Lunar Radiance avatar  avatar ketsakda avatar antcorp avatar

Watchers

James Cloos avatar 黄朝晖 avatar  avatar  avatar  avatar

mqtt-server-incubator's Issues

请教

服务开启后,使用三方的mqtt工具,订阅主题,如"ubXg47dA" ,服务端的主题,无法转发给其他订阅的客户端
1709978343318
订阅主题的代码
1709978456642

【内部解析报错】使用mqttx链接无法连上

使用mqttx链接无法连上

[WARNING] Simps\MQTT\Exception\LengthException: unpack remaining length error, get 1297(0) in /www/wwwroot/abc.com/hyperf/vendor/simps/mqtt/src/Tools/UnPackTool.php:30
Stack trace:
#0 /www/wwwroot/abc.com/hyperf/vendor/simps/mqtt/src/Packet/UnPack.php(32): Simps\MQTT\Tools\UnPackTool::string()
#1 /www/wwwroot/abc.com/hyperf/vendor/simps/mqtt/src/Protocol/V3.php(86): Simps\MQTT\Packet\UnPack::connect()
#2 /www/wwwroot/abc.com/hyperf/vendor/hyperf/mqtt-server-incubator/src/MQTTServer.php(174): Simps\MQTT\Protocol\V3::unpack()
#3 /www/wwwroot/abc.com/hyperf/vendor/hyperf/mqtt-server-incubator/src/MQTTServer.php(113): Hyperf\MqttServer\MQTTServer->buildRequest()
#4 {main}

服务启动后 怎么一直无法连接上

服务创建后,启动成功了,客户端(使用mqtt.js)无法连接上,是不是得在route上配置路由,就像websocket服务启动一样?

网页改如何连接这个启动的mqtt服务,望写个demo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.