原代码
#!/usr/bin/php -q
/**
* This is a demo
* @see docchatserver
*/
//ini_set('default_socket_timeout', -1);
$redis
=
new
Redis();
$redis
->pconnect(
'127.0.0.1'
,6379);
$redis
->select(15);
function
channel(
$redis
,
$channel
,
$msg
) {
echo
'from-'
.
$channel
.
':'
.
$msg
.
"n"
;
}
$redis
->subscribe(
array
(
'world'
,
'city'
,
'union'
,
'firend'
),
'channel'
);
?>
执行上面代码无任何消息时触发Fatal error: Uncaught exception ‘RedisException’ with message ‘read error on connection’,查看redis扩展源码发现扩展连接redis服务器使用默认php的socket方式,查看php.ini设置了default_socket_timeout = 60,所以没有任何消息时出现了上面的错误,解决方法是加入
ini_set
(
'default_socket_timeout'
, -1);
上面的代码可以应用在游戏消息通知方面,需要注意的是redis服务器配置中timeout的设置
修改redis.conf, timeout 设为0
谢谢了