ollyxar/websockets
Composer 安装命令:
composer require ollyxar/websockets
包简介
PHP WebSocket server
README 文档
README
Simple and multifunctional PHP WebSocket server
Live chat example
Performance
Installing WebSockets
The recommended way to install WebSockets is through Composer.
# Install Composer curl -sS https://getcomposer.org/installer | php
Next, run the Composer command to install the latest stable version of WebSockets:
php composer.phar require ollyxar/websockets
After installing, you need to require Composer's autoloader:
require 'vendor/autoload.php';
Simple WebSocket server
MyHandler.php
use Generator; use Ollyxar\LaravelAuth\FileAuth; use Ollyxar\WebSockets\{ Frame, Handler as BaseHandler, Dispatcher }; class MyHandler extends BaseHandler { /** * MyHandler constructor. * @param $server * @param $master */ public function __construct($server, $master) { parent::__construct($server, $master); echo "I'm: #{$this->pid}\n"; } /** * @param $client * @return Generator */ protected function onConnect($client): Generator { yield Dispatcher::async($this->broadcast(Frame::encode(json_encode([ 'type' => 'system', 'message' => 'User {' . (int)$client . '} Connected.' ])))); } /** * @param $clientNumber * @return Generator */ protected function onClose($clientNumber): Generator { yield Dispatcher::async($this->broadcast(Frame::encode(json_encode([ 'type' => 'system', 'message' => "User {$clientNumber} disconnected." ])))); } /** * @param string $message * @param int $socketId * @return Generator */ protected function onClientMessage(string $message, int $socketId): Generator { $message = json_decode($message); if (!empty($message)) { $userName = $message->name; $userMessage = $message->message; $response = Frame::encode(json_encode([ 'type' => 'usermsg', 'name' => $userName, 'message' => $userMessage ])); yield Dispatcher::async($this->broadcast($response)); } } }
User validation
Base Handler has own method to validate user to be logged in. By default it is always return true. You should provide your own implementation of method to authorize users.
/** * Use this method for custom user validation * * @param array $headers * @param $socket * @return bool */ protected function validateClient(array $headers, $socket): bool { return true; }
ws-server.php
/** * Lets start our server */ (new Server('0.0.0.0', 2083, 6, true)) ->setCert() ->setPassPhrase() ->setHandler(MyHandler::class) ->run();
Nginx reverse proxy
# choose location path location /websocket/ { # use same host and port from your settings! proxy_pass http://0.0.0.0:2082/; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_read_timeout 86400; }
Realtime testing (logging)
The server has internal logger that can output info into console. All that you need is just to enable logging before launching server.
Logger::enable();
Communicate with server outside the wss protocol
use Ollyxar\WebSockets\Server as WServer; use Ollyxar\WebSockets\Frame; $socket = socket_create(AF_UNIX, SOCK_STREAM, 0); socket_connect($socket, WServer::$connector); $data = new stdClass(); $data->type = 'system'; $data->message = 'hello World!'; $msg = Frame::encode(json_encode($data)); socket_write($socket, $msg); socket_close($socket);
ollyxar/websockets 适用场景与选型建议
ollyxar/websockets 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.89k 次下载、GitHub Stars 达 31, 最近一次更新时间为 2017 年 10 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「php」 「library」 「unix」 「Web Sockets」 「websocket server」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ollyxar/websockets 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ollyxar/websockets 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ollyxar/websockets 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
Inbox pattern process implementation for your Laravel Applications
Core library that defines common interfaces used by the rest of the intahwebz..
Amqp classes
Provides command to manage a web library directory
Alfabank REST API integration
统计信息
- 总下载量: 1.89k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 31
- 点击次数: 10
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-10-16