分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

swoole 安装 搭建tcp服务器和websocket

发布时间:2023-09-06 02:32责任编辑:郭大石关键词:websocket
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文件并输入如下内容:

// Serverclass 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文件并输入如下内容:

<?phpclass Client{ ???private $client; ???public function __construct() { ???????$this->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();

swoole 安装 搭建tcp服务器和websocket

原文地址:http://blog.51cto.com/itafei/2347852

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved