oktopost / tattler-php
PHP Tattler 客户端
1.0.11
2018-08-12 12:18 UTC
Requires
- php: >=7.1-stable
- 00f100/uuid: 4.0.0
- firebase/php-jwt: ^4.0
- oktopost/objection: ^1.0
- oktopost/skeleton: ^1.0.10
- satooshi/php-coveralls: ^1.0
Requires (Dev)
- guzzlehttp/guzzle: ^6.2
- nategood/httpful: ^0.2.20
- oktopost/squanch: ^1.1
- oktopost/squid: ^1.0
- phpunit/phpunit: ^6.2
- predis/predis: ^1.1
Suggests
- guzzlehttp/guzzle: Communicate with Tattler backend through guzzle (optional, by default CURL will be used)
- nategood/httpful: Communicate with Tattler backend through httpful (optional, by default CURL will be used)
- oktopost/squanch: Store users/channels tokens with Squanch client
- oktopost/squid: Store users/channels tokens in MySQL DB
- predis/predis: Store users/channels tokens in Redis DB
README
使用 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:80 或 https://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/ 中查看更多文档