reamp/server

基本 Reamp 服务器组件

dev-master 2018-05-28 20:16 UTC

This package is not auto-updated.

Last update: 2024-09-15 04:55:42 UTC


README

什么是 ReAmp ? 它是 Ratchet 到 amphp 的移植。它是原始项目的分支,但增加了某些新功能

  • 使用不同的命名空间
  • 默认使用 amphp
  • PHP7 支持
  • PHPUnit 6
  • 代码风格

目前仍缺少一些功能,但将在未来完成

  • 支持 HTTP 请求体(post/put/create)
  • 支持 HTTP 请求管道
  • 支持 psr 日志
  • 更好的解析,类似于 amp/aerys 风格
  • 支持组件中的异步和承诺

Reamp 服务器

这是 reamp 的基本组件。它用于创建套接字服务器

安装

此软件包可以作为 Composer 依赖项安装。

composer require reamp/server

文档

文档可以在 Ratchet 网站 上找到,以及在 ./docs 目录中。

要求

  • PHP 7.0+
  • 需要 shell 访问权限,建议使用 root 权限。

示例

此示例在端口 8080 上创建简单的聊天服务器

<?php
use Reamp\Server\IoServer;
use Reamp\Server\IoServerInterface;
use Reamp\Server\ConnectionInterface;

// Make sure composer dependencies have been installed
require __DIR__ . '/vendor/autoload.php';

/**
 * chat.php
 * Send any incoming messages to all connected clients (except sender)
 */
class MyChat implements IoServerInterface {
    protected $clients;

    public function __construct() {
        $this->clients = new \SplObjectStorage;
    }

    public function onOpen(ConnectionInterface $conn) {
        $this->clients->attach($conn);
    }

    public function onMessage(ConnectionInterface $from, $msg) {
        foreach ($this->clients as $client) {
            if ($from != $client) {
                $client->send($msg);
            }
        }
    }

    public function onClose(ConnectionInterface $conn) {
        $this->clients->detach($conn);
    }

    public function onError(ConnectionInterface $conn, \Throwable $e) {
        $conn->close();
    }
}

// Run the server application through the WebSocket protocol on port 8080
$app = IoServer::factory(new MyChat(), 8080, 'localhost');
IoServer::run();
$ php chat.php

更多示例可以在本存储库的 ./examples 目录以及 Ratchet 存储库中找到。