oktopost/tattler-php

PHP Tattler 客户端

1.0.11 2018-08-12 12:18 UTC

README

Build Status Coverage Status License MIT

使用 Tattler 向您的用户发送异步消息

安装

$ composer require oktopost/tattler-php

或将它添加到 composer.json

"require": {
    "oktopost/tattler-php": "^1.0"
}

然后运行 composer update

设置

$config = new TattlerConfig();
$tattlerConfig->fromArray([
        'WsAddress'         => 'TATTLER_WEBSOCKET_ADDRESS',
        'ApiAddress'        => 'TATTLER_API_ADDRESS',
        'Namespace'         => 'YOUR APPLICATION_NAME',
        'Secret'            => 'TATTLER_SECRET',
        'TokenTTL'          => 'USER_TOKEN_TTL',
        'DBDecorator'       => new RedisDecorator(),
        'NetworkDecorator'  => new CurlDecorator()
]);

/** @var ITattlerModule::class $tattler */
$tattler = Tattler::getInstance($tattlerConfig);

注意:使用 redis db 装饰器需要安装 predis

  • TATTLER_WEBSOCKET_ADDRESS - websocket 传输地址,例如 ws://websocket.domain.tld:80 或 wss://websocket.domain.tld:443
  • TATTLER_API_ADDRESS - API 地址,例如 http://websocket.domain.tld:80https://websocket.domain.tld:443
  • YOUR_APPLICATION_NAME - 您应用程序的命名空间。您可以使用相同的 tattler 服务器处理多个应用程序。
  • TATTLER_SECRET - 与 tattler-server 配置中定义的相同密钥
  • USER_TOKEN_TTL - 用户认证令牌在 tattler-server 中使用的时间(秒)

然后创建从您的网站可访问的 TattlerController。参见 DummyControllerExample
注意:该控制器中的所有方法都应该以 JSON 格式响应

当 PHP 配置完成时,将 tattler.min.js 包含到您的 HTML 中并初始化 tattler

window.tattler = TattlerFactory.create();

用法

在您的 JS 代码中设置监听器

window.tattler.addHandler('myMessage', 'globalNamespace', function(data){
	alert(data.message);
});

从 PHP 向所有用户发送负载

/** var ITattlerMessage::class $message */
$message = new TattlerMessage();
$message->setHandler('myMessage')->setNamespace('globalNamespace')->setPayload(['message' => 'Hello world']]);

$tattler->message($message)->broadcast()->say();

docs/ 中查看更多文档