2 Star 2 Fork 0

inhere / php-server

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

php server

快速的使用和管理 swoole server

相关组件

More please see https://github.com/swoole-kit

install

composer require inhere/server

run flow


php bin/server ->

simple run

  • create a instance
$server = new TcpServer($config);

....

$server->run();

注意事项

  • 协程模式

开启协程模式后,swoole_server 和swoole_http_server将以为每一个请求创建对应的协程,开发者可以在onRequestonReceiveonConnect 3个事件回调中使用协程客户端

  • 在主服务器上追加监听的端口服务的事件不生效
//file: server.php

$mainServer = new swoole_http_server('0.0.0.0', 9501);

// 追加监听tcp端口
// listen 是 addListener 的别名
$port = $mainServer->listen('0.0.0.0', 9601, SWOOLE_SOCK_TCP);
$port->on('receive', function($srv, $fd, $fromId, $data){
    $srv->send($fd, "Server: ".$data);
});

$mainServer->start();

在终端运行server: php server.php

再开一个终端测试追加监听的tcp服务是否成功

telnet 127.0.0.1 9661
text // 发现输入后什么都没有返回

最后发现必须要调用 $port->set() 才行。增加一行设置 tcp 服务的配置

... ...
$port->set([]); // 设置tcp监听的配置,可以覆盖继承的主server(swoole_http_server)配置

$mainServer->start();

重新运行server后再测试:

telnet 127.0.0.1 9661
text
Server text // 返回了消息

NOTICE: 增加端口监听后,必须要调用 $port->set(). 不然不会触发监听服务上的事件,即使传入空数组也行,但不能不调用。

The MIT License (MIT) Copyright (c) 2016 inhere Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

a php server powerd by swoole 展开 收起
PHP
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
PHP
1
https://gitee.com/inhere/php-server.git
git@gitee.com:inhere/php-server.git
inhere
php-server
php-server
master

搜索帮助