ekstazi/reamp

PHP WebSocket 库

v0.1 2018-02-11 21:40 UTC

README

什么是 ReAmp?它是 RatchEt 到 amphp/amp 的端口。它作为原始项目的分支创建,但添加了一些新功能

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

尽管仍然缺少一些功能,但这些将在未来完成

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

由于代码库与 Ratchet 相似,文档可以在原始网站上找到。

Ratchet

Build Status Autobahn Testsuite Latest Stable Version

一个用于异步服务 WebSocket 的 PHP 库。通过简单的接口构建您的应用程序,并通过组合不同的组件重用您的应用程序,无需更改其任何代码。

要求

需要 Shell 访问,建议使用 root 访问。为了避免代理/防火墙拦截,建议在端口 80 或 443(SSL)上请求 WebSocket,这需要 root 访问。为了做到这一点,除了您的同步 Web 堆栈,您可以使用反向代理或两台独立的机器。您可以在 服务器配置文档 中找到更多详细信息。

文档

用户和 API 文档可在 Ratchet 网站上找到: http://socketo.me

有关使用 Ratchet 的一些现成工作示例,请参阅 https://github.com/cboden/Ratchet-examples

需要帮助?有问题?想要提供反馈?请在 Google Groups 邮件列表 上留言。

一个快速示例

<?php
use Reamp\MessageComponentInterface;
use Reamp\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 MessageComponentInterface {
    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 = new Reamp\App('localhost', 8080);
    $app->route('/chat', new MyChat);
    $app->route('/echo', new Reamp\Server\EchoServer, array('*'));
    $app->run();
$ php chat.php
    // Then some JavaScript in the browser:
    var conn = new WebSocket('ws://localhost:8080/echo');
    conn.onmessage = function(e) { console.log(e.data); };
    conn.onopen = function(e) { conn.send('Hello Me!'); };