centos安裝php擴展swoole及使用

swoole是一個PHP的異步、並行、高性能網絡通訊引擎,使用純C語言編寫,提供了PHP語言的異步多線程服務器,異步TCP/UDP網絡客戶端,異步MySQL,異步Redis,數據庫鏈接池,AsyncTask,消息隊列,毫秒定時器,異步文件讀寫,異步DNS查詢。 Swoole內置了Http/WebSocket服務器端/客戶端、Http2.0服務器端。 Swoole能夠普遍應用於互聯網、移動通訊、企業軟件、雲計算、網絡遊戲、物聯網(IOT)、車聯網、智能家居等領域。 使用PHP+Swoole做爲網絡通訊框架,能夠使企業IT研發團隊的效率大大提高,更加專一於開發創新產品。 注意事項: 一、server.php中的ip地址必須是外網可訪問地址 http://47.105.128.101,不能爲localhostphp

一、安裝linux

wget https://github.com/swoole/swoole-src/archive/swoole-1.7.6-stable.tar.gz

tar zxvf swoole-1.7.6-stable.tar.gz

cd swoole-1.7.6-stable

phpize

./configure --with-php-config=/usr/local/php/bin/php-config

make  && make install

提示: Build complete. Don't forget to run 'make test'.   Installing shared extensions:     /usr/lib64/php/modules/ 說明安裝成功nginx

二、php加載swoole擴展git

extension_dir = "/usr/lib64/php/modules/" extension=swoole.sogithub

三、重啓服務 service php-fpm restart數據庫

                    service nginx restart服務器

四、測試,查看phpinfo信息,以下圖所示:swoole

五、代碼測試   server.php代碼網絡

<?php多線程

$serv = new \swoole_server("127.0.0.1", 55152);

$serv->on('connect', function ($serv, $fd){

echo "Client:Connect.\n";

});

$serv->on('receive', function ($serv, $fd, $from_id, $data) {

$serv->send($fd,'Swoole:'.$data);

});

$serv->on('close', function ($serv, $fd) {

echo "Clinet:close.\n";

});

$serv->start();

?>

client.php代碼

<?php

$client = new \swoole_client(SWOOLE_SOCK_TCP);

//var_dump($client);exit;

if (!$client->connect('127.0.0.1', 55152, -1))

{

exit("connect failed. Error:{$client->errCode}\n");

}

$client->send("hello world\n");

echo $client->recv();

$client->close();

?>

首先啓動服務:(只可在命令啓動)

 
而後linux終端 telnet測試
# telnet 47.105.128.101 55152

 

小知識點 php--info 

                php -r "echo swoole_version();" //查看當前用的Swoole的版本號

相關文章
相關標籤/搜索