git clone https: //gitee.com/swoole/swoole.git
git clone https://gitee.com/swoole/swoole.git
通过phpize(扩展php扩展模块,建立php外挂模块):
cd swoole
your/phpize/path
./configure --with-php-config=your/php/path/bin/php-config
make && make install
可以看到swoole.so的位置
/opt/soft/php/lib/php/extensions/no-debug-non-zts-20170718/
配置php.ini
extension=swoole.so
php -m
命令,可以看到php的扩展模块检测swoole安装成功并且php支持swoole
cd your/swoole/path/examples/server
php echo.php
(如果进程被阻塞,则说明成功)netstat -anp | grep 9501
cd swoole
실행: your/phpize/path
./configure --with-php-config=your/php/path/bin/php-config
make && make install
/opt/soft/php/lib/php/extensions/no-debug-non입니다. -zts-20170718 /
extension=swoole.so
를 통해 보실 수 있습니다
명령 php
cd your/swoole/path/examples/server
php echo를 지원하는지 감지합니다. .php
(프로세스가 차단되면 성공) netstat -anp | grep 9501
(swoole이 열어놓은 포트번호 확인)
ps aft | grep tcp_server.php
//创建Server对象,监听 127.0.0.1:9501端口 $serv = new swoole_server("127.0.0.1", 9501); $serv->set([ 'worker_num' => 4, // worker进程数,cpu 1-4倍 'max_request' => 100, ]); /** * 监听连接进入事件 * $fd 客户端连接服务端的唯一标识 * $reactor_id 线程id */ $serv->on('connect', function ($serv, $fd, $reactor_id) { echo "Client: {$fd} - {$reactor_id} - Connect.\n"; }); //监听数据接收事件 $serv->on('receive', function ($serv, $fd, $reactor_id, $data) { $serv->send($fd, "Server: ".$data); }); //监听连接关闭事件 $serv->on('close', function ($serv, $fd) { echo "Client: Close.\n"; }); //启动服务器 $serv->start();
2를 통해 가장 간단한 tcp 서비스 만들기 2. php의 4가지 콜백// 创建tcp客户端 $client = new swoole_client(SWOOLE_SOCK_TCP); // 连接tcp服务端 if (!$client->connect("127.0.0.1", 9501)) { echo '连接失败'; exit; } // php cli fwrite(STDOUT, '请输入:'); $msg = trim(fgets(STDIN)); // 发送消息给tcp服务端 if (!$client->send($msg)) { echo '发送消息失败'; exit; } // 接收 $result = $client->recv(); echo $result;로그인 후 복사
$server->on('Request', function ($req, $resp) { echo "hello world"; });
class static method
function🎜🎜🎜class A { static function test($req, $resp) { echo "hello world"; } } $server->on('Request', 'A::Test'); $server->on('Request', array('A', 'Test'));로그인 후 복사
function my_onRequest($req, $resp) { echo "hello world"; } $server->on('Request', 'my_onRequest');
class A { function test($req, $resp) { echo "hello world"; } } $object = new A(); $server->on('Request', array($object, 'test'));
// 监听所有地址和9501端口 $http = new swoole_http_server('0.0.0.0', 9501); // 动静分离配置 $http->set([ // 开启静态请求 'enable_static_handler' => true, // 静态资源目录 'document_root' => '/opt/app/code1/', ]); $http->on('request', function ($request, $response) { // 获取get请求的参数 $param = json_encode($request->get); // 设置cookie $response->cookie('name', 'ronaldo', time() + 1800); // 输出到页面 $response->end("<h1>Hello Swoole - {$param}</h1>"); }); // 开启http服务 $http->start();
// 监听所有地址和9502端口 $server = new swoole_websocket_server('0.0.0.0', 9502); // 动静分离配置 $server->set([ // 开启静态请求 'enable_static_handler' => true, // 静态资源目录 'document_root' => '/opt/app/swoole/websocket', ]); $server->on('open', function ($server, $request) { echo "server:handshake success with fd - {$request->fd}\n"; }); $server->on('message', function ($server, $frame) { echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n"; $server->push($frame->fd, "this is server"); }); $server->on('close', function ($server, $fd) { echo "client - {$fd} - close\n"; }); $server->start();
// 创建websocket实例 var websocketURL = "ws://www.rona1do.top:9502"; var websocket = new WebSocket(websocketURL); // 实例化对象的onopen属性 websocket.onopen = function (ev) { websocket.send("hello-websocket"); console.log("connect-swoole-success"); } // 实例化对象的onmessage属性,接收服务端返回的数据 websocket.onmessage = function (ev) { console.log("websockect-server-return-data:" + ev.data); } // close websocket.onclose = function (ev) { console.log("close"); }
class WebSocket { const HOST = '0.0.0.0'; const PORT = 9502; private $ws = null; function __construct() { $this->ws = new swoole_websocket_server(self::HOST, self::PORT); $this->ws->on('open', [$this, 'onOpen']); $this->ws->on('message', [$this, 'onMessage']); $this->ws->on('close', [$this, 'onClose']); $this->ws->start(); } // 监听websocket连接事件 function onOpen($server, $request) { echo "server: handshake success with fd{$request->fd}\n"; } // 监听websocket消息接收事件 function onMessage($server, $frame) { echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n"; $server->push($frame->fd, "this is server"); } // 监听客户端关闭事件 function onClose($server, $fd) { echo "Client:{$fd} closes\n"; } }
위 내용은 Swoole은 왜 시작했다가 포기하게 되었나요?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!