首页 > linux > swoole学习

swoole学习

作者:bin
目录
[隐藏]

一、编译安装swoole

下载地址: 

git clone https://github.com/swoole/swoole-src.git

启用配置脚本

phpize

./configure

如果这里报错  Cannot find php-config. Please use –with-php-config=PATH ,则使用

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

(

PATH为你的php安装目录,

1、在phpinfo() 或者 使用命令php –info|grep “configure”找到configure目录,

2、在configure目录下查找内容php-config(我在bin目录下)

例外:如果你装了多个php版本,需要通过类似apt-get install php7.1-dev 方式生成php-config文件

)

然后make

make

sudo make install

编译完成后,在php.ini中修改

取消extension=swoole.so 的注释

使用 php -m 查看swoole是否存在,或者phpinfo();

二、程序的运行

在linux的shell下使用

php your_file.php

1.一个简单的TCP服务器:tcp_service.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服务器,监听本机9501端口。它的逻辑很简单,当客户端Socket通过网络什么字符,服务器就会回复相同字符

  1. 1、$fd是用户唯一标识
  2. 2、当用户请求时调用OnReceive
  3. 3、通过$serv->send(), 发送一个消息给客户端
  4. 4、当链接断开时候,掉用OnClose

执行程序

php  tcp_service.php

使用telnet命令

telnet 127.0.0.1 9501

hello

Server: hello

测试时请打开2个窗口,一个服务端一个测试端口

服务端可以看到链接记录

Client: Connect.
Client: Close.

测试完成退出,进入telent使用

ctrl + ]

输入quit即可

 

2.一个简单的UDP服务器:udp_service.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();
  1. 1、UDP服务器与TCP服务器不同,UDP没有连接的概念。
  2. 2、启动Server后,客户端无需Connect,直接可以向Server监听的9502端口发送数据包。
  3. 3、对应的事件为onPacket。
  4. 4、$clientInfo是客户端的相关信息,是一个数组,有客户端的IP和端口等内容
  5. 5、调用 $server->sendto 方法向客户端发送数据

启动服务

php udp_server.php

测试

netcat -u 127.0.0.1 9502

hello

Server: hello

注意这里

服务端没有连接与断开的记录,因为udp没有连接概念,和寄信(udp),打电话(tcp)

3.一个简单的web服务器:  http_service.php

$http = new swoole_http_server("0.0.0.0", 9501);

$http->on('request', function ($request, $response) {
    var_dump($request->get, $request->post);
    $response->header("Content-Type", "text/html; charset=utf-8");
    $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
});

$http->start();

http服务器相对简单些,这里只需要关注onRequst即用户请求,通过response去相应即可

 

4.一个简单的websocket服务器, websocket_server.php

//创建websocket服务器对象,监听0.0.0.0:9502端口
$ws = new swoole_websocket_server("0.0.0.0", 9502);

//监听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();

WebSocket服务器是建立在Http服务器之上的长连接服务器,客户端首先会发送一个Http的请求与服务器进行握手。握手成功后会触发onOpen事件,表示连接已就绪,onOpen函数中可以得到$request对象,包含了Http握手的相关信息,如GET参数、Cookie、Http头信息等。

建立连接后客户端与服务器端就可以双向通信了。

  1. 1、客户端向服务器端发送信息时,服务器端触发onMessage事件回调
  2. 2、服务器端可以调用$server->push()向某个客户端(使用$fd标识符)发送消息
  3. 3、服务器端可以设置onHandShake事件回调来手工处理WebSocket握手

剩下的使用细节参考:
https://wiki.swoole.com/wiki/page/484.html

您必须 [ 登录 ] 才能发表留言!