{"id":343721,"date":"2021-02-05T23:06:09","date_gmt":"2021-02-05T15:06:09","guid":{"rendered":"http:\/\/4563.org\/?p=343721"},"modified":"2021-02-05T23:06:09","modified_gmt":"2021-02-05T15:06:09","slug":"phpredis-%e6%89%a9%e5%b1%95%e7%9a%84-subscribe-%e8%ae%a2%e9%98%85%e7%96%91%e6%83%91","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=343721","title":{"rendered":"phpRedis \u6269\u5c55\u7684 subscribe \u8ba2\u9605\u7591\u60d1"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  phpRedis \u6269\u5c55\u7684 subscribe \u8ba2\u9605\u7591\u60d1               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xxbutoo <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6309\u7167\u6587\u6863\u4e0a\u7684 demo https:\/\/github.com\/phpredis\/phpredis#connect-open<\/p>\n<pre><code>function f($redis, $chan, $msg) {  switch($chan) {   case 'chan-1':    ...    break;    case 'chan-2':    ...    break;    case 'chan-2':    ...    break;  } } <\/code><\/pre>\n<p>$redis-&gt;subscribe([&#8216;chan-1&#8217;, &#8216;chan-2&#8217;, &#8216;chan-3&#8217;], &#8216;f&#8217;); \/\/ subscribe to 3 chans&#8220;`<\/p>\n<p>\u6211\u7684 demo \u5982\u4e0b:<\/p>\n<pre><code>$r = new Redis(); $r-&gt;pconnect('127.0.0.1', '6379'); $r-&gt;auth('test123456');  $r-&gt;subscribe(['chan-1', 'chan-2', 'chan-3'], function() {  switch($chan) {   case 'chan-1':    file_put_contents('.\/tt.txt', '\u8ba2\u9605\u6d88\u606f 1' . $msg . \"n\", FILE_APPEND);    break;   case 'chan-2':    file_put_contents('.\/tt.txt', '\u8ba2\u9605\u6d88\u606f 2' . $msg . \"n\", FILE_APPEND);    break;   case 'chan-2':     file_put_contents('.\/tt.txt', '\u8ba2\u9605\u6d88\u606f 3' . $msg . \"n\", FILE_APPEND);    break;  } });  <\/code><\/pre>\n<p>\u4f46\u662f\u53d1\u5e03\u4ee5\u540e \u5c31\u4e00\u76f4\u6ca1\u6709\u8fd4\u56de\u503c\u4e86\u3002\u7b49\u4e86\u4e00\u4f1a\u540e\u5c31\u4f1a\u629b\u51fa read error on connection \u5f02\u5e38<\/p>\n<p>\u67e5\u4e86\u8d44\u6599 subscribe \u662f\u963b\u585e\u6a21\u5f0f \u4e0d\u4f1a\u6709\u8fd4\u56de\u503c<\/p>\n<p>\u4e8e\u662f\u6211\u8bbe\u7f6e\u4e86<\/p>\n<pre><code> ini_set('default_socket_timeout', -1); <\/code><\/pre>\n<pre><code>$r-&gt;setOption($r::OPT_READ_TIMEOUT, -1); <\/code><\/pre>\n<p>\u6267\u884c\u540e\u8fd4\u56de 502 \u4e86 \u8111\u74dc\u75bc cgi \u548c nginx \u53c8\u65ad\u5f00\u8fde\u63a5\u4e86\u3002\u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>3<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5295983\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : emeab <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8bf7\u7528 php CLI                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5295984\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : gbqqaybc <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Livid \u91cd\u590d\u53d1\u5e16 https:\/\/www.v2ex.com\/t\/753734                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5295985\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : xxbutoo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @gbqqaybc \u54c8\u54c8\u4f60\u8fd9\u4e2a\u3002\u3002\u3002- -\u54e6\u54e6\u54e6\u54e6                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>phpRedis \u6269\u5c55\u7684 subs&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/343721"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=343721"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/343721\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=343721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=343721"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=343721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}