PHP code example of workbunny / webman-push-server
1. Go to this page and download the library: Download workbunny/webman-push-server library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
workbunny / webman-push-server example snippets
use Workbunny\WebmanPushServer\WsClient;
use Workerman\Connection\AsyncTcpConnection;
use Workbunny\WebmanPushServer\EVENT_SUBSCRIBE;
use Workbunny\WebmanPushServer\EVENT_SUBSCRIPTION_SUCCEEDED;
// 创建连接
$client = WsClient::instance('127.0.0.1:8001', [
'app_key' => 'workbunny',
'heartbeat' => 60,
'auth' => 'http://127.0.0.1:8002/subscribe/auth',
'channel_data' => [] // channel_data
'query' => [], // query
'context_option' => []
])
// 建立连接
$client->connect();
// 关闭连接
$client->disconnect();
use Workbunny\WebmanPushServer\WsClient;
use Workerman\Connection\AsyncTcpConnection;
// 创建连接
$client = WsClient::instance('127.0.0.1:8001', [
'app_key' => 'workbunny',
'heartbeat' => 60,
'auth' => 'http://127.0.0.1:8002/subscribe/auth',
'channel_data' => [] // channel_data
'query' => [], // query
'context_option' => []
])
// 订阅一个私有通道,订阅成功后会执行回调函数
$client->subscribe('private-test', function (AsyncTcpConnection $connection, array $data) {
// 订阅成功后打印
dump($data);
});
// 订阅一个私有通道,不注册订阅成功后的回调
$client->subscribe('private-test');
// 取消订阅一个私有通道
$client->unsubscribe('private-test', function (AsyncTcpConnection $connection, array $data) {
// 取消订阅成功后打印
dump($data);
});
// 取消订阅一个私有通道,不注册订阅成功后的回调
$client->unsubscribe('private-test');
// 取消全部订阅
$client->unsubscribeAll();
// 向 private-test 通道发送 client-test 事件消息
$client->trigger('private-test', 'client-test', [
'message' => 'hello workbunny!'
]);
// 向 presence-test 通道发送 client-test 事件消息
$client->trigger('presence-test', 'client-test', [
'message' => 'hello workbunny!'
]);
// 事件不带 client- 前缀会抛出RuntimeException
try {
$client->trigger('presence-test', 'test', [
'message' => 'hello workbunny!'
]);
} catch (RuntimeException $exception){
dump($exception);
}
use Workerman\Connection\AsyncTcpConnection;
// 注册关注private-test通道的client-test事件
$client->eventOn('private-test', 'client-test', function(AsyncTcpConnection $connection, array $data) {
// 打印事件数据
dump($data);
});
// 取消关注private-test通道的client-test事件
$client->eventOff('private-test', 'client-test');
// 获取所有注册事件回调
$client->getEvents();
// 获取客户端id,当连接创建前该方法返回null
$client->getSocketId();
// 获取已订阅通道,订阅触发前该方法返回空数组
$client->getChannels();
// 发布消息
$client->publish();
// 更多详见 WsClient.php
use Workbunny\WebmanPushServer\ApiClient;
try {
$pusher = new ApiClient(
'APP_KEY',
'APP_SECRET',
'APP_ID',
[
'host' =>"http://127.0.0.1:8001",
'timeout' => 60,
'keep-alive' => true
]
);
$pusher->trigger(
// 频道(channel)支持多个通道
["private-d"],
// 事件
"client-a",
// 消息体
[
'message' => 'hello workbunny!'
],
// query
[]
);
} catch (GuzzleException|ApiErrorException|PusherException $e) {
dump($e);
}
declare(strict_types=1);
namespace YourNamespace;
use Workbunny\WebmanPushServer\Traits\ChannelMethods;
class PushServerMiddleware
{
use ChannelMethods;
/** @inheritDoc */
public static function _subscribeResponse(string $type, array $data): void
{
// TODO
}
}
// push-server-middleware
'push-server-middleware' => [
'handler' => YourNamespace\PushServerMiddleware::class,
'count' => 1,
],
// push server root middlewares
'push-server' => [
// 以拦截非websocket消息举例
function (Closure $next, TcpConnection $connection, $data): void
{
// 拦截非websocket服务消息
if (!$connection->protocol instanceof \Workerman\Protocols\Websocket) {
$connection->close('Not Websocket');
return;
}
$next($connection, $data);
}
],
declare(strict_types=1);
namespace Tests\Examples;
use Workbunny\WebmanPushServer\Events\AbstractEvent;
use Workerman\Connection\TcpConnection;
class OtherEvent extends AbstractEvent
{
/**
* @inheritDoc
*/
public function response(TcpConnection $connection, array $request): void
{
// todo
}
}
\Workbunny\WebmanPushServer\Events\AbstractEvent::register('other', \Tests\Examples\OtherEvent::class);
declare(strict_types=1);
namespace Tests\Examples;
use Workbunny\WebmanPushServer\PublishTypes\AbstractPublishType;
class OtherType extends AbstractPublishType
{
/** @inheritDoc */
public static function response(array $data): void
{
static::verify($data, [
['appKey', 'is_string', true],
['channel', 'is_string', true],
['event', 'is_string', true],
['socketId', 'is_string', false]
]);
// todo
}
}
\Workbunny\WebmanPushServer\PublishTypes\AbstractPublishType::register('other', \Tests\Examples\OtherType::class);
\Workbunny\WebmanPushServer\PushServer::publish('other', [
'a' => 'a'
])
|-- config
|-- plugin
|-- webman-push-server
|-- app.php # 主配置信息
|-- bootstrap.php # 自动加载
|-- command.php # 支持命令
|-- log.php # 日志配置
|-- middlewares.php # 基础中间件
|-- process.php # 启动进程
|-- redis.php # redis配置
|-- route.php # APIs路由信息
|-- registrar.php # 分布式服务注册器配置
server {
# .... 这里省略了其它配置 ...
location /app
{
proxy_pass http://127.0.0.1:3131;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Real-IP $remote_addr;
}
}
composer