Linux 安裝php擴展 swoole

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

 

注意事項:
一、server.php中的ip地址必須是外網可訪問地址 123.57.232.99,不能爲localhost


一、安裝
# 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 
# make  && make install 
 
提示:
Build complete.
Don't forget to run 'make test'.
 
Installing shared extensions:    /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/
說明安裝成功


二、php加載swoole擴展


extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/"
extension=swoole.so
 
三、重啓服務
service php-fpm restart
service nginx restart


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




五、代碼測試
 
server.php代碼:
<?php
$serv = new swoole_server("123.57.232.99", 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 "Client: Close.\n";
});
$serv->start();
?>
 
 
client.php代碼
<?php
$client = new swoole_client(SWOOLE_SOCK_TCP);
if (!$client->connect('123.57.232.99', 55152, -1))
{
    exit("connect failed. Error: {$client->errCode}\n");
}
$client->send("hello world\n");
echo $client->recv();
$client->close();
?>
 
 
首先啓動服務:
[root@iZ25l6q4losZ swoole]# php server.php
Client:Connect.
 
而後linux終端 telnet測試
# telnet 123.57.232.99 55152
Trying 123.57.232.99...
Connected to 123.57.232.99.
Escape character is '^]'.
rr
Swoole: rr
測試
Swoole: 測試
 linux

相關文章
相關標籤/搜索