
1 swoole 소스 패키지 설치
- 다운로드:
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
- 通过
php -m
命令,可以看到php的扩展模块
-
检测swoole安装成功并且php支持swoole
cd your/swoole/path/examples/server
-
php echo.php
(如果进程被阻塞,则说明成功)
netstat -anp | grep 9501
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
(swoole이 열어놓은 포트번호 확인)
ps aft | grep tcp_server.php
2. 네트워크 통신 엔진
Swoole을 배우려면 설명서를 읽어야 합니다.
swoole 문서
1 swoole
tcp 서버(tcp_server.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | $serv = new swoole_server( "127.0.0.1" , 9501);
$serv ->set([
'worker_num' => 4,
'max_request' => 100,
]);
$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();
|
로그인 후 복사
tcp 클라이언트(tcp_client.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | $client = new swoole_client(SWOOLE_SOCK_TCP);
if (! $client ->connect( "127.0.0.1" , 9501)) {
echo '连接失败' ;
exit ;
}
fwrite(STDOUT, '请输入:' );
$msg = trim( fgets (STDIN));
if (! $client ->send( $msg )) {
echo '发送消息失败' ;
exit ;
}
$result = $client ->recv();
echo $result ;
|
로그인 후 복사
2를 통해 가장 간단한 tcp 서비스 만들기 2. php의 4가지 콜백
익명 기능
1 2 3 | $server ->on( 'Request' , function ( $req , $resp ) {
echo "hello world" ;
});
|
로그인 후 복사
class static method
1 2 3 4 5 6 7 8 9 | class A
{
static function test( $req , $resp )
{
echo "hello world" ;
}
}
$server ->on( 'Request' , 'A::Test' );
$server ->on( 'Request' , array ( 'A' , 'Test' ));
|
로그인 후 복사
function🎜🎜🎜
1 2 3 4 5 | function my_onRequest( $req , $resp )
{
echo "hello world" ;
}
$server ->on( 'Request' , 'my_onRequest' );
|
로그인 후 복사
🎜🎜🎜object method🎜🎜🎜
1 2 3 4 5 6 7 8 9 10 | class A
{
function test( $req , $resp )
{
echo "hello world" ;
}
}
$object = new A();
$server ->on( 'Request' , array ( $object , 'test' ));
|
로그인 후 복사
🎜🎜작은 팁: 🎜열린 작업자 프로세스를 확인하세요. 🎜🎜🎜 🎜3. UDP 서버 클라이언트는 설명서🎜🎜🎜4. http service🎜
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | $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 ) {
$param = json_encode( $request ->get);
$response ->cookie( 'name' , 'ronaldo' , time() + 1800);
$response -> end ( "<h1>Hello Swoole - {$param}</h1>" );
});
$http ->start();
|
로그인 후 복사
🎜🎜5에 따라 직접 만들 수 있습니다. swoole🎜🎜websocket 서버(websocket_server.php)🎜
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | $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 클라이언트(websockt_client.html)🎜
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | var websocketURL = "ws://www.rona1do.top:9502" ;
var websocket = new WebSocket(websocketURL);
websocket.onopen = function (ev) {
websocket.send( "hello-websocket" );
console.log( "connect-swoole-success" );
}
websocket.onmessage = function (ev) {
console.log( "websockect-server-return-data:" + ev.data);
}
websocket.onclose = function (ev) {
console.log( "close" );
}
|
로그인 후 복사
를 통해 웹소켓 서비스를 만듭니다. 🎜🎜6. 객체 지향을 사용하여 웹소켓 서비스 코드 최적화🎜
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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();
}
function onOpen( $server , $request ) {
echo "server: handshake success with fd{$request->fd}\n" ;
}
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" ;
}
}
|
로그인 후 복사
🎜🎜7. swoole🎜🎜🎜onTask의 작업 소규모 사례: 🎜는 task_worker 프로세스 내에서 호출됩니다. 작업자 프로세스는 swoole_server_task 함수를 사용하여 task_worker 프로세스에 새 작업을 전달할 수 있습니다. 현재 작업 프로세스가 onTask 콜백 함수를 호출하면 프로세스 상태가 사용 중으로 전환되고 더 이상 새 작업을 수신하지 않습니다. onTask 함수가 반환되면 프로세스 상태를 유휴 상태로 전환하고 새 작업을 계속 수신합니다. 🎜🎜🎜onFinish: 🎜작업자 프로세스가 전달한 작업이 task_worker에서 완료되면 작업 프로세스는 swoole_server->finish() 메서드를 통해 작업 처리 결과를 작업자 프로세스로 보냅니다. 🎜아아아아
위 내용은 Swoole은 왜 시작했다가 포기하게 되었나요?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!