hemiframe/php-websocket 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hemiframe/php-websocket

Composer 安装命令:

composer require hemiframe/php-websocket

包简介

PHP WebSocket server and client library

README 文档

README

Simple and powerful PHP WebSocket implementation for PHP

Supports

  • multiple paths
  • check-origin
  • limit clients
  • sends status codes on close connections
  • sends custom headers

Requirements

PHP >= 7.0

Installation

Enter in composer.json

{
    "require": {
        "hemiframe/php-websocket": "~1.7"
    }
}

or run in console: php composer.phar require hemiframe/php-websocket

How to run demo?

Open CMD or terminal and enter

php demo/server.php

After that you can open demo/index.html in your browser

Server example

require_once('vendor/autoload.php');

$socket = new \HemiFrame\Lib\WebSocket\WebSocket("localhost", 8080);
$socket->on("receive", function($client, $data) use($socket) {
	foreach ($socket->getClients() as $item) {
        /* @var $item \HemiFrame\Lib\WebSocket\Client */
		if ($item->id != $client->id) {
			$socket->sendData($item, $data);
		}
	}
});
$socket->startServer();

Client example

require_once('vendor/autoload.php');

$socket = new \HemiFrame\Lib\WebSocket\WebSocket('localhost', 8080);
$socket->on("receive", function($client, $data) use($socket) {
});
$client = $socket->connect();
if ($client) {
	$socket->sendData($client, "Message");
	$socket->disconnectClient($client);
}

Documentation

Events:

$socket->on("connect", function(\HemiFrame\Lib\WebSocket\Client $client) {
});

$socket->on("receive", function(\HemiFrame\Lib\WebSocket\Client $client, $data) {
});

$socket->on("send", function(\HemiFrame\Lib\WebSocket\Client $client, $data) {
});

$socket->on("ping", function(\HemiFrame\Lib\WebSocket\Client $client, $data) {
});

$socket->on("pong", function(\HemiFrame\Lib\WebSocket\Client $client, $data) {
});

$socket->on("disconnect", function(\HemiFrame\Lib\WebSocket\Client $client, $statusCode, $reason) {
});

$socket->on("error", function($socket, $client, $phpError, $errorMessage, $errorCode) {
});

Methods

$socket->getSocket();

/**
* Get connected clients
* @return array
*/
$socket->getClients();

/**
* Get connected clients, filtered by path
* @return array
*/
$socket->getClientsByPath($path = "/");

$socket->getAllowedOrigins();

$socket->setAllowedOrigins($allowedOrigins);

$socket->getMaxClients();

$socket->setMaxClients($maxClients);

$socket->getBufferSize();

$socket->setBufferSize($bufferSize);

$socket->getUserAgent();

$socket->setUserAgent($userAgent);

$socket->setEnableLogging($enableLogging);

$socket->create();

$socket->close();

$socket->setOption($socket, $level, $optname, $optval);

$socket->bind($address, $port = 0);

$socket->listen($backlog = 0);

$socket->select(&$read, &$write, &$except, $backlog, $tv_usec = 0);

$socket->accept();

$socket->write($socket, $message = null);

$socket->recv($socket);

$socket->read($socket);

/**
* Get last socket error code
* Alias of socket_last_error
* @return int
*/
$socket->getLastErrorCode();

/**
* Get last socket error message
* @return string
*/
$socket->getLastErrorMessage();

/**
* Connect to websocket server. Send and validate handshake request.
* @param string $path Send custom path to server
* @param string $origin Send custom origin host to server
* @return mixed Return client object when is successfully connected to server or FALSE when is not.
*/
$socket->connect($path = "/", $origin = null);

/**
* Run WebSocket server.
* Create main socket.
* Bind address and port.
* Listens for a connection on a socket.
* Call main processing loop
*/
$socket->startServer();

/**
* Disconnect all clients and close main socket
*/
$socket->stopServer();

/**
* Main processing loop. Check for new clients, incomming data, diconnected clients
*/
$socket->loop();

/**
* Trigger event
* @param string $name Event name
* @param array $arguments Arguments for sending
*/
$socket->trigger($name, $arguments);

/**
* Subscribe for event.
* @param string $name Event name
* @param function $function Callback function
*/
$socket->on($name, $function);

/**
* Subscribe on event. Call only first time
* @param string $name Event name
* @param function $function Callback function
*/
$socket->one($name, $function);

/**
* Send data to client
* @param object $client
* @param string $data
*/
$socket->sendData($client, $data);

/**
* Disconnect client. You can send custom status code and reason
* @param object $client
* @param int $statusCode
* @param string $reason
* @return self
*/
$socket->disconnectClient($client, $statusCode = self::STATUS_CLOSE_NORMAL, $reason = null);

hemiframe/php-websocket 适用场景与选型建议

hemiframe/php-websocket 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.2k 次下载、GitHub Stars 达 48, 最近一次更新时间为 2017 年 12 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「websocket」 「client」 「server」 「WebSockets」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 hemiframe/php-websocket 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 hemiframe/php-websocket 我们能提供哪些服务?
定制开发 / 二次开发

基于 hemiframe/php-websocket 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 11.2k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 49
  • 点击次数: 20
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 48
  • Watchers: 4
  • Forks: 13
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-12-24