【Redis subscribe()订阅】subscribe.php

 

 1 <?php
 2 header("Content-type:text/html;charset=utf-8");
 3 //redis订阅频道 subscribe()
 4 $redis = new Redis();
 5 $redis->connect("localhost", 6379);
 6 //选择redis数据库
 7 $redis->select(1);
 8 //订阅频道【为数组格式】
 9 $channels=array('myChaRoom');
10 $redis->subscribe($channels, "callBlack");
11 
12 
13 //订阅之后回调的方法
14 function callBlack($redis, $chan, $msg)
15 {
16     show_dump($redis);
17     show_dump($chan);
18     show_dump($msg);
19     exit;
20 }
21 
22 
23 //格式化代码
24 function show_dump($array)
25 {
26     echo "<pre>";
27     print_r($array);
28     echo "</pre>";
29 }

 

 

【Redis publish()发布】publish.php

 1 <?php
 2 header("Content-type:text/html;charset=utf-8");
 3 //redis发布频道 publish()
 4 $redis = new Redis();
 5 $redis->connect("localhost",6379);
 6 //选择redis数据库
 7 $redis->select(1);
 8 //get方式获取消息
 9 $msg=$_GET['msg'];
10 $redis->publish("myChaRoom", $msg);