swoole 安装 搭建tcp服务器和websocket
admin
2023-01-28 11:37:48
0

1、安装swoole
wget https://github.com/swoole/swoole-src/archive/v1.9.1-stable.tar.gz
tar zxvf v1.9.1-stable.tar.gz
cd swoole-src-1.9.1-stable
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
2、配置php支持swoole
vi /usr/local/php/etc/php.ini
添加
extension=swoole.so
3、重启php-fpm
service php-fpm restart
在phpinfo页面可以看到关于swoole的选项,说明安装成功。

4.搭建Echo服务器
服务端 Server
创建一个Server.php文件并输入如下内容:

// Server
class Server
{
    private $serv;

    public function __construct() {
        $this->serv = new swoole_server("0.0.0.0", 9501);
        $this->serv->set(array(
            'worker_num' => 8,
            'daemonize' => false,
        ));

        $this->serv->on('Start', array($this, 'onStart'));
        $this->serv->on('Connect', array($this, 'onConnect'));
        $this->serv->on('Receive', array($this, 'onReceive'));
        $this->serv->on('Close', array($this, 'onClose'));

        $this->serv->start();
    }

    public function onStart( $serv ) {
        echo "Start\n";
    }

    public function onConnect( $serv, $fd, $from_id ) {
        $serv->send( $fd, "Hello {$fd}!" );
    }

    public function onReceive( swoole_server $serv, $fd, $from_id, $data ) {
        echo "Get Message From Client {$fd}:{$data}\n";
        $serv->send($fd, $data);
    }

    public function onClose( $serv, $fd, $from_id ) {
        echo "Client {$fd} close connection\n";
    }
}
// 启动服务器 Start the server
$server = new Server();

客户端 Client
创建一个Client.php文件并输入如下内容:

client = new swoole_client(SWOOLE_SOCK_TCP);
    }

    public function connect() {
        if( !$this->client->connect("127.0.0.1", 9501 , 1) ) {
            echo "Error: {$this->client->errMsg}[{$this->client->errCode}]\n";
        }

        fwrite(STDOUT, "请输入消息 Please input msg:");  
        $msg = trim(fgets(STDIN));
        $this->client->send( $msg );

        $message = $this->client->recv();
        echo "Get Message From Server:{$message}\n";
    }
}

$client = new Client();
$client->connect();

运行 Run it!
在Terminal下执行命令php Server.php即可启动服务器,在另一个Terminal下执行php Client.php,输入要发送的内容,即可发送消息到服务器,并收到来自服务器的消息。

websocket:

$server = new Swoole\WebSocket\Server("0.0.0.0", 9501);

$server->on('open', function (Swoole\WebSocket\Server $server, $request) {
    echo "server: handshake success with fd{$request->fd}\n";
});

$server->on('message', function (Swoole\WebSocket\Server $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 ($ser, $fd) {
    echo "client {$fd} closed\n";
});

$server->start();

相关内容

热门资讯

美军被疑隐瞒对伊作战伤亡 【环球时报综合报道】美国国防部21日发表声明说,一名美军士兵17日在伊朗针对位于约旦的穆沃费格萨勒提...
OpenAI模型“越狱”攻击,... 一家美国公司遭到美国AI攻击,而提供保护的竟是一款中国人工智能工具。AI工具平台Hugging Fa...
南航第十二架C919国产大飞机... 广州7月21日电 (记者 郭军)7月21日,一架尾号为B-659G的国产大飞机飞抵广州白云国际机场,...
无人艇第一股,要来了 那个被资本追捧的“无人艇第一股”,要来了。 2017年冬天的一个夜晚,港珠澳大桥附近的海面上,81条...
三一锂能取得电池包专利提升电芯... 国家知识产权局信息显示,三一锂能有限公司取得一项名为“电池包”的专利,授权公告号CN22453717...
价格为何下滑?市场竞争力为何不... 记者采访西瓜种植主产区河南开封等地如何种得好,又能卖得好(经济聚焦)本报记者 常 钦 《人民日报》(...
“镐山”之后美再威胁袭伊基础设... 截至7月22日,美军已连续11晚对伊朗发动袭击,伊方对美中东地区军事基地的反击也未停止。与此同时,美...
美国“痛失”杨植麟,这是历史性... 杨植麟的月之暗面创造了Kimi K3,轰动整个世界。他是近年来第一个放弃了美国机会、回中国创业,并且...
铭显光电取得LED显示屏加工夹... 国家知识产权局信息显示,上海铭显光电科技有限公司取得一项名为“一种LED显示屏加工夹具”的专利,授权...
大盘承压依旧登顶,vivo拿下... 7月22日消息,市场调研机构Omdia发布2026年第二季度印度智能市场报告,数据显示本季度印度手机...