PHPackages                             huaweichenai/web-socket - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. huaweichenai/web-socket

ActiveLibrary[HTTP &amp; Networking](/categories/http)

huaweichenai/web-socket
=======================

This is a service class that implements websocket

1.0(6y ago)19BSD-3-ClausePHP

Since Jan 9Pushed 6y ago1 watchersCompare

[ Source](https://github.com/huaweichenai/web-socket)[ Packagist](https://packagist.org/packages/huaweichenai/web-socket)[ RSS](/packages/huaweichenai-web-socket/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)DependenciesVersions (2)Used By (0)

webSocket
=========

[](#websocket)

This is a service class that implements websocket (这是一个websocket的服务类，可以快速的帮助你实现websocket的一些基本功能)

[![Latest Stable Version](https://camo.githubusercontent.com/9c85f1dabefb4b087892b3d99324429575b00f7af1774324765d974c27dd10ab/68747470733a2f2f706f7365722e707567782e6f72672f6875617765696368656e61692f7765622d736f636b65742f762f737461626c65)](https://packagist.org/packages/huaweichenai/web-socket)[![Total Downloads](https://camo.githubusercontent.com/de25a961d09efb80c01b41a08ef8b39e0ca937291a8c56b69f86c29e1da7ea39/68747470733a2f2f706f7365722e707567782e6f72672f6875617765696368656e61692f7765622d736f636b65742f646f776e6c6f616473)](https://packagist.org/packages/huaweichenai/web-socket)[![Latest Unstable Version](https://camo.githubusercontent.com/d3aa04f0bc7cb90de08a1d50f4038f9b2dce7370b95d0f4fe78800efed3094eb/68747470733a2f2f706f7365722e707567782e6f72672f6875617765696368656e61692f7765622d736f636b65742f762f756e737461626c65)](https://packagist.org/packages/huaweichenai/web-socket)[![License](https://camo.githubusercontent.com/fd78b0f62e487bfa5d7872f6c14a37e93173e9d9508d684ee5a13df72bcada8e/68747470733a2f2f706f7365722e707567782e6f72672f6875617765696368656e61692f7765622d736f636b65742f6c6963656e7365)](https://packagist.org/packages/huaweichenai/web-socket)

Installation

-------------

[](#installation)

```
composer require huaweichenai/web-socket

```

Usage

------

[](#usage)

### Configuration

[](#configuration)

```
$host = '0.0.0.0';
$port = '8888';
$configs = [
    // 日志文件路径
  'log_file' => dirname(__DIR__) . '/logs/swoole.log',
  // 进程的PID存储文件
  'pid_file' => dirname(__DIR__) . '/logs/swoole.server.pid'
]

```

其他的详细运行参数可与参考：

### use

[](#use)

#### websocket启动

[](#websocket启动)

```
$server = new WebSocketServer($host, $port, $configs);
$server->handshake = true;//设置自定义握手配置
$server->run();

```

#### websocket停止

[](#websocket停止)

```
$server = new WebSocketServer($host, $port, $configs);
$server->stop();

```

根据如上就可是简单的实现websocket运行,如果你需要在websocket运行期间：启动,握手,连接,接收消息,http响应,客户端关闭连接,服务端关闭连接自己设置自己的自定义方法的话 **1**自己创建一个类，专门用户继承websocket，并进行复写各个阶段的事件

```
class Swoole extends WebSocketServer
{
    /**
     * @var bool
     * 开启自定义握手处理
     */
    public $handshake = true;

    /**
     * @param \swoole_websocket_server $server
     *
     * 自定义websocket服务启动处理
     */
    public function socketStart($server)
    {
        //业务代码
    }

    /**
     * @param \swoole_websocket_server $server
     * @param \swoole_http_request $request
     *
     * 自定义websocket建立连接处理
     */
    public function socketOpen($server, $request)
    {
        //业务代码
    }

    /**
     * @param \swoole_websocket_server $server
     * @param \swoole_websocket_frame $frame
     *
     * 自定义websocket 接受客户端消息处理
     */
    public function socketMessage($server, $frame)
    {
        //业务代码
    }

    /**
     * @param \swoole_http_request $request
     * @param \swoole_http_response $response
     *
     * 自定义websocket握手处理
     */
    public function socketHandshake($request, $response)
    {
        //业务代码
        //业务代码
    }

    /**
     * @param \swoole_http_request $request
     * @param \swoole_http_response $response
     *
     * 自定义websocket http响应处理
     */
    public function socketRequest($request, $response)
    {
        //业务代码
    }

    /**
     * @param \swoole_websocket_server $server
     * @param $fd
     *
     * 自定义websocket 客户端连接关闭处理
     */
    public function socketClose($server, $fd)
    {
        //业务代码
    }

    /**
     * @param \swoole_websocket_server $server
     *
     * 自定义websocket 服务端正常关闭处理
     */
    public function socketShutdown($server)
    {
        //业务代码
    }

}

```

在上面的业务代码中我们可以使用下面方法向指定客户端发送信息

```
$this->sendMessage($request->fd, $server,$data);

```

还可以使用如下方法：

```
$this->getParams($request) //获取客户端连接路由
$this->getRoute($request) //获取客户端传参

```

***2***自定义类创建好了之后

```
$server = new Swoole($host, $port, $configs);
$server->run();

```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2321d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2b421da4ccc357acb51b3c127387bf06648210f5fb68fcec8ad831859b474d1a?d=identicon)[huaweichenai](/maintainers/huaweichenai)

---

Top Contributors

[![huaweichenai](https://avatars.githubusercontent.com/u/39394599?v=4)](https://github.com/huaweichenai "huaweichenai (11 commits)")

---

Tags

phpserverwebsocket

### Embed Badge

![Health badge](/badges/huaweichenai-web-socket/health.svg)

```
[![Health](https://phpackages.com/badges/huaweichenai-web-socket/health.svg)](https://phpackages.com/packages/huaweichenai-web-socket)
```

###  Alternatives

[hemiframe/php-websocket

PHP WebSocket server and client library

4911.0k](/packages/hemiframe-php-websocket)[basement-chat/basement-chat

Add a real-time chat widget to your Laravel application.

4983.9k](/packages/basement-chat-basement-chat)[warriorxk/phpwebsockets

A websocket library with support for IPC using socket pairs

1225.3k](/packages/warriorxk-phpwebsockets)[swoole-bundle/swoole-bundle

Open/Swoole Symfony Bundle

6650.4k](/packages/swoole-bundle-swoole-bundle)[morozovsk/websocket-examples

examples for simple php websocket server: simple chat (single daemon) - http://sharoid.ru/chat.html , pro chat (master + worker) - http://sharoid.ru/chat2.html , simple game - http://sharoid.ru/game.html

382.3k](/packages/morozovsk-websocket-examples)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
