swoole学习笔记第三季

ps:每次PHP 运行swoole的时候 都是把代码写入内存然后执行 每次执行的都是内存里的代码 所以 每次改完以后 需要重新PHP执行 从存储中写入内存执行

以下代码就是 swoole收到的数据存入redis缓存
on(‘open’, function ($ws, $request) {
foreach($ws->connections as $fd)
{
$ws->push($request->fd, “hello, welcome1\n”);

}
//获取连接总数
echo ‘获取所有链接数’.count($ws->connections).’\n’;
});

//监听WebSocket消息事件
$ws->on(‘message’, function ($ws, $frame) {
echo “Message: {$frame->data}\n”;

//连接本地的 Redis 服务
$redis = new Redis();
$redis->connect(‘127.0.0.1’, 6379);
echo “Connection to server sucessfully”;
//存储数据到列表中
$redis->lpush(“tutorial-list”, “Redis”);
$redis->lpush(“tutorial-list”, “Mongodb”);
$redis->lpush(“tutorial-list”, “Mysql”);
// 获取存储的数据并输出
$arList = $redis->lrange(“tutorial-list”, 0 ,5);
echo “Stored string in redis”;
var_dump($arList);

$ws->push($frame->fd, “server: {$frame->data}”);
});

//监听WebSocket连接关闭事件
$ws->on(‘close’, function ($ws, $fd) {
echo “client-{$fd} is closed\n”;
});

$ws->start();

swoole学习笔记第二季

第一部分定时器写法

swoole_timer_tick函数就相当于setInterval,是持续触发的
swoole_timer_after函数相当于setTimeout,仅在约定的时间触发一次
swoole_timer_tick/swoole_timer_after函数会返回一个整数,表示定时器的ID
可以使用 swoole_timer_clear 清除此定时器,参数为定时器ID 说明客户端断开连接不会影响定时器

<?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";
});




//定时器要写在WorkerStart这个里面哦
$ws->on('WorkerStart', function ($serv, $worker_id){
    //3000ms后执行此函数
    $aaa=swoole_timer_tick(2000, function ($timer_id) {
        echo "tick-2000ms\n";
    });
    echo $aaa.'\n';
   //清除定时器
//    swoole_timer_clear($aaa);
});

$ws->start();

使用迭代器遍历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);
    foreach($ws->connections as $fd)
    {
        $ws->push($request->fd, "hello, welcome1\n");

    }
    //获取连接总数
    echo '获取所有链接数'.count($ws->connections).'\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();



swoole学习笔记第一季

首先swoole是自己开发的web服务器 跟apache和nginx无关

1 首先开启端口 并且监听9502这个端 是否有客户端请求这个端口 ps:这里讲的是websocket 因为方便测试
//创建websocket服务器对象,监听0.0.0.0:9502端口

<!--?php //创建websocket服务器对象,监听0.0.0.0:9502端口 端口号可以自定义哦 $ws = new swoole_websocket_server("0.0.0.0", 9502); //监听WebSocket连接打开事件 $ws-&gt;on('open', function ($ws, $request) {
    var_dump($request-&gt;fd, $request-&gt;get, $request-&gt;server);
    $ws-&gt;push($request-&gt;fd, "hello, welcome\n");
});

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

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

$ws-&gt;start();
&lt;/pre&gt;
&lt;p&gt;然后php 运行这个代码 就开启了端口&lt;br ?-->
netstat -ntlp 查看端口 就有了
tcp        0      0 0.0.0.0:9502                0.0.0.0:*                   LISTEN      10328/php

第二部分 客户端websocket请求


WebSocket Test
<script language="javascript" type="text/javascript">
    var wsUri = "ws://ip:9502";
    var output;

    var ws = new WebSocket(wsUri);
    //请求服务端后 成功后触发
    ws.onopen = function () {
        console.log('open');

        ws.send('hello');

    };

    ws.onmessage = function (evt) {

        console.log(evt.data)

    };
    //我关闭服务的时候 就会触发
    ws.onclose = function (evt) {

        console.log('WebSocketClosed!');

    };

    ws.onerror = function (evt) {

        console.log('WebSocketError!');

    };
</script>

输出结果

 

qq%e6%88%aa%e5%9b%be20161018105001

phpredis函数使用方法详解

phpredis函数文档详解

连接到 redis 服务

<?php
    //连接本地的 Redis 服务
   $redis = new Redis();
   $redis->connect('127.0.0.1', 6379);
   echo "Connection to server sucessfully";
         //查看服务是否运行
   echo "Server is running: " . $redis->ping();

//输出结果
Connection to server sucessfully
Server is running: PONG
?>

Redis set函数

<?php
   //连接本地的 Redis 服务
   $redis = new Redis();
   $redis->connect('127.0.0.1', 6379);
$redis->flushAll();
   echo "Connection to server sucessfully";
   //设置 redis 字符串数据
   $redis->set("tutorial-name", "Redis tutorial");
   // 获取存储的数据并输出
   echo "Stored string in redis:: " . $redis->get("tutorial-name");

  //输出结果
Connection to server sucessfully
Stored string in redis:: Redis tutorial
?>

Redis lpush函数

<?php
//连接本地的 Redis 服务
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
//每次运行清除redis所有缓存数据
$redis->flushAll();
//存储数据到列表中(可以想象上车  从前门一个个进入 所以先进入的都是排在后面)
$redis->lpush("tutorial-list", "Redis");
$redis->lpush("tutorial-list", "Mongodb");
$redis->lpush("tutorial-list", "Mysql");
$redis->lpush("tutorial-list", "c++");
$redis->lpush("tutorial-list", "php");
// 获取存储的数据并输出
$arList = $redis->lrange("tutorial-list", 0 ,2);

print_r($arList);

//结果 读取了0到2的范围
Array
(
    [0] => php
    [1] => c++
    [2] => Mysql
)

?>

Redis keys函数

<?php
   //连接本地的 Redis 服务
   $redis = new Redis();
   $redis->connect('127.0.0.1', 6379);
$redis->flushAll();
//存储数据到列表中
$redis->lpush("tutorial-list", "Redis");
$redis->lpush("tutorial-list", "Mongodb");
$redis->lpush("tutorial-list", "Mysql");
$redis->lpush("tutorial-list", "c++");
$redis->lpush("tutorial-list", "php");

   // 获取数据并输出
   $arList = $redis->keys("*");
   echo "Stored keys in redis:: ";
   print_r($arList);

  //输出结果 返回了 所有key
 Stored keys in redis:: Array ( [0] => tutorial-list ) 
?>

linux关闭端口的方法

netstat -ntlp 展示当前使用的端口
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 2088/redis-server 1
tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN 21474/memcached
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1411/nginx
tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 717/vsftpd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 924/sshd
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1906/mysqld

PID是进程号 直接关闭进程就可以关闭端口了
kill -9 2088 就关闭了redis端口