定制 kzykhys/coroutine-io 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

kzykhys/coroutine-io

Composer 安装命令:

composer require kzykhys/coroutine-io

包简介

Fast socket server implementation using *Generator*

README 文档

README

Latest Stable Version Build Status

Fast socket server implementation using Generator. This project is heavily inspired by @nikic's great post.

Following project shows the possibility of CoroutineIO.

  • kzykhys/coupe - Coupé - A Handy HTTP/HTTPS Server written in PURE PHP

Requirements

  • PHP5.5+

Installation

Create or update your composer.json and run composer update

{
    "require": {
        "kzykhys/coroutine-io": "~0.1.0"
    }
}

Example (HTTP Server)

  1. Run php example.php
  2. Open http://localhost:8000 in browser and hold down the F5 key
  3. Watch the console output
php example.php
::1:50531
GET / HTTP/1.1
Host: localhost:8000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:25.0) Gecko/20100101 Firefox/25.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: ja,en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive

example.php

<?php

use CoroutineIO\Example\HttpHandler;
use CoroutineIO\Example\HttpServer;

require __DIR__ . '/vendor/autoload.php';

$server = new HttpServer(new HttpHandler());
$server->run();

Class HttpServer

<?php

namespace CoroutineIO\Example;

use CoroutineIO\Exception\Exception;
use CoroutineIO\Server\Server;

/**
 * Simple HTTP Server Implementation
 */
class HttpServer extends Server
{

    /**
     * {@inheritdoc}
     */
    public function createSocket($address = 'localhost:8000')
    {
        $socket = @stream_socket_server('tcp://' . $address, $no, $str);

        if (!$socket) {
            throw new Exception("$str ($no)");
        }

        return $socket;
    }

}

Class HttpHandler

<?php

namespace CoroutineIO\Example;

use CoroutineIO\Server\HandlerInterface;
use CoroutineIO\Socket\ProtectedStreamSocket;
use CoroutineIO\Socket\StreamSocket;

/**
 * Simple HTTP Server Implementation
 */
class HttpHandler implements HandlerInterface
{

    /**
     * {@inheritdoc}
     */
    public function handleClient(StreamSocket $socket)
    {
        $socket->block(false);
        $data = (yield $socket->read(8048));

        $response = $this->handleRequest($data, new ProtectedStreamSocket($socket));

        yield $socket->write($response);
        yield $socket->close();
    }

    /**
     * {@inheritdoc}
     */
    public function handleRequest($input, ProtectedStreamSocket $socket)
    {
        // Displays request information
        echo $socket->getRemoteName() . "\n";
        echo $input;

        return "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 5\n\nHello";
    }

}

License

The MIT License

Author

Kazuyuki Hayashi (@kzykhys)

统计信息

  • 总下载量: 101
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 2
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-11-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固