欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Linux下源码包安装Swoole及基本使用操作图文详解

程序员文章站 2023-10-30 17:41:28
本文实例讲述了linux下源码包安装swoole及基本使用操作。分享给大家供大家参考,具体如下: 下载swoole pecl扩展源码包: 关于php版本依赖选择:...

本文实例讲述了linux下源码包安装swoole及基本使用操作。分享给大家供大家参考,具体如下:

下载swoole pecl扩展源码包:

关于php版本依赖选择:

Linux下源码包安装Swoole及基本使用操作图文详解

下载好放到/usr/local/src下,解压缩:

tar -zxvf swoole-2.2.0.tgz

准备扩展安装编译环境:

phpize

Linux下源码包安装Swoole及基本使用操作图文详解

查看php-config位置:

find / -name php-config

Linux下源码包安装Swoole及基本使用操作图文详解

配置:(--with-php-config==后面是你自己的php-config位置)

./configure --with-php-config=/www/server/php/72/bin/php-config

编译安装:

make && make install

Linux下源码包安装Swoole及基本使用操作图文详解

在php.ini里面加一行 :

extension = swoole.so

使用 php -m 命令查看swoole扩展已经安装成功:

Linux下源码包安装Swoole及基本使用操作图文详解

查看phpinfo信息:

Linux下源码包安装Swoole及基本使用操作图文详解


(测试前说明:以下使用的端口,要确认服务器放行,宝塔环境还需要添加安全组规则)

【创建tcp服务器】

创建server.php:

<?php
  //创建server对象,监听 127.0.0.1:9501端口
  $serv = new swoole_server("127.0.0.1", 9501); 
 
  //监听连接进入事件
  $serv->on('connect', function ($serv, $fd) { 
    echo "client: connect.\n";
  });
 
  //监听数据接收事件
  $serv->on('receive', function ($serv, $fd, $from_id, $data) {
    $serv->send($fd, "server: ".$data);
  });
 
  //监听连接关闭事件
  $serv->on('close', function ($serv, $fd) {
    echo "client: close.\n";
  });
 
  //启动服务器
  $serv->start(); 

启动tcp服务:

php server.php

查看9501端口已被监听:

netstat -an | grep 9501

Linux下源码包安装Swoole及基本使用操作图文详解

使用telnet连接tcp服务,输入hello,服务器返回hello即测试成功:

telnet 127.0.0.1 9501

Linux下源码包安装Swoole及基本使用操作图文详解

(如果telnet工具没有安装,执行yum install telnetyum install telnet-server

也可以写一个tcp客户端连接tcp服务器端:

创建tcp_client.php:

<?php
  //创建client对象,监听 127.0.0.1:9501端口
  $client = new swoole_client(swoole_sock_tcp); 

  if(!$client->connect("127.0.0.1" ,9501)){
	echo "连接失败";
	exit;
  }

  //向tcp服务器发送消息
  fwrite(stdout, "请输入:");
  $msg = trim(fgets(stdin));
  $client->send($msg);

  //接受tcp服务器消息
  $result = $client->recv();
  echo $result;

启动tcp客户端:

php tcp_client.php

测试结果: 

Linux下源码包安装Swoole及基本使用操作图文详解


【创建udp服务器】

创建udp_server.php:

<?php
  //创建server对象,监听 127.0.0.1:9502端口,类型为swoole_sock_udp
  $serv = new swoole_server("127.0.0.1", 9502, swoole_process, swoole_sock_udp); 

  //监听数据接收事件
  $serv->on('packet', function ($serv, $data, $clientinfo) {
    $serv->sendto($clientinfo['address'], $clientinfo['port'], "server ".$data);
    var_dump($clientinfo);
  });

  //启动服务器
  $serv->start(); 

启动udp服务:

php udp_server.php

查看9502端口已被监听:

netstat -an | grep 9502

Linux下源码包安装Swoole及基本使用操作图文详解

使用netcat连接udp服务,输入hello,服务器返回hello即测试成功(centos):

nc -u 127.0.0.1 9502

 Linux下源码包安装Swoole及基本使用操作图文详解

(如果没有安装netcat监听器,执行yum install -y nc


【创建web服务器】

创建http_server.php:

<?php
  $http = new swoole_http_server("0.0.0.0", 9501);
  
  //配置静态文件根目录(可选)
  $http->set([
    'document_root' => '/www/wwwroot/lwsblog',
    'enable_static_handler' => true,
  ]);

  $http->on('request', function ($request, $response) {
    var_dump($request->get, $request->post);
    
    //设置header
    $response->header("content-type", "text/html; charset=utf-8");

    //设置cookie
    $response->cookie("name", "lws", time()+3600);

    //发送http响应体,并结束请求处理。
    $response->end("<h1>hello swoole. #".rand(1000, 9999)."</h1>");
  });

  $http->start();

启动服务:

php http_server.php

(如果9501端口已经被占用查看进程pid,杀死进程:)

Linux下源码包安装Swoole及基本使用操作图文详解

lsof -i:9501

Linux下源码包安装Swoole及基本使用操作图文详解

kill 9013

浏览器访问主机地址:端口号,得到程序预期结果即测试成功:

Linux下源码包安装Swoole及基本使用操作图文详解


【创建websocket服务器】

创建ws_server.php:

<?php
  //创建websocket服务器对象,监听0.0.0.0:9501端口
  $ws = new swoole_websocket_server("0.0.0.0", 9501);
  
  //配置静态文件根目录(可选)
  $ws ->set([
    'document_root' => '/www/wwwroot/lwsblog',
    'enable_static_handler' => true,
  ]);

  //监听websocket连接打开事件
  $ws->on('open', function ($ws, $request) {
    var_dump($request->fd, $request->get, $request->server);
    $ws->push($request->fd, "hello, welcome\n");
  });

  //监听websocket消息事件
  $ws->on('message', function ($ws, $frame) {
    echo "message: {$frame->data}\n";
    $ws->push($frame->fd, "server: {$frame->data}");
  });

  //监听websocket连接关闭事件
  $ws->on('close', function ($ws, $fd) {
    echo "client-{$fd} is closed\n";
  });

  $ws->start();

运行程序:(这里还是要确认监听的端口没有被占用,如果被占用查看进程pid,杀死进程)

php ws_server.php

前端页面js监听:(127.0.0.1改成你的主机地址)

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>websocket</title>
</head>
<body>
</body>

<script type="text/javascript">
var wsserver = 'ws://127.0.0.1:9501';
var websocket = new websocket(wsserver);
websocket.onopen = function (evt) {
  console.log("connected to websocket server.");
};

websocket.onclose = function (evt) {
  console.log("disconnected");
};

websocket.onmessage = function (evt) {
  console.log('retrieved data from server: ' + evt.data);
};

websocket.onerror = function (evt, e) {
  console.log('error occured: ' + evt.data);
};
</script>
</html>

使用谷歌浏览器访问前端页面:

Linux下源码包安装Swoole及基本使用操作图文详解

服务器端收到请求信息:

Linux下源码包安装Swoole及基本使用操作图文详解

更多关于php相关内容感兴趣的读者可查看本站专题:《php扩展开发教程》、《php面向对象程序设计入门教程》、《php数学运算技巧总结》、《php数组(array)操作技巧大全》、《php数据结构与算法教程》、《php程序设计算法总结》、《php正则表达式用法总结》及《php常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。