<?php
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace GatewayWorker\Lib;
use Exception;
use GatewayWorker\Protocols\GatewayProtocol;
use Workerman\Connection\TcpConnection;
/**
* 数据发送相关
*/
class Gateway
{
private $redis;
public $checkUserReadable = false;
public static function setChatRecord($from, $to, $message) {
$data = array('from' => $from, 'to' => $to, 'message' => $message, 'sent' => time());
$value = json_encode($data);
//生成json字符串
$keyName = 'rec:' . self::getRecKeyName($from, $to);
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$res = $redis -> lPush($keyName, $value);
if (!$this -> checkUserReadable) {//消息接受者无法立刻查看时,将消息设置为未读
return $redis -> hIncrBy('unread_' . $to, $from, 1);
}
return $res;
}
private static function getRecKeyName($from, $to)
{ if ($from > $to){return $to . '_' . $from;
}else{ return $from . '_' . $to;}
}
/**
* gateway 实例
*/
报错信息
Fatal error: Class 'GatewayWorker\Lib\Redis' not found in /pi/components/com_work/GatewayWorker/Lib/Gateway.php on line 49
WORKER EXIT UNEXPECTED E_ERROR Class 'GatewayWorker\Lib\Redis' not found in /pi/components/com_work/GatewayWorker/Lib/Gateway.php on line 49
worker exit with status 65280
49行就是$redis = new Redis(); 是不是不能直接使用redis.
注意PHP的命名空间,应该是 new \Redis();
php.net/manual/zh/language.namespaces.php
改成 new \Redis();现在还有报错 WORKER EXIT UNEXPECTED E_ERROR Class 'Redis' not found in /pi/components/com_work/GatewayWorker/Lib/Gateway.php on line 31
没装redis扩展
。。。。
安装了。下面是# service redis-server status
● redis-server.service - SYSV: Redis is a persistent key-value database
Loaded: loaded (/etc/rc.d/init.d/redis-server)
Active: active (running) since Thu 2016-07-14 00:10:36 CST; 16h ago
Docs: man:systemd-sysv-generator(8)
Process: 2145 ExecStart=/etc/rc.d/init.d/redis-server start (code=exited, status=0/SUCCESS)
Main PID: 2164 (redis-server)
CGroup: /system.slice/redis-server.service
└─2164 /usr/local/redis/bin/redis-server 127.0.0.1:6379
Jul 14 00:10:36 localhost.localdomain systemd: Starting SYSV: Redis is a persistent key-value database...
Jul 14 00:10:36 localhost.localdomain redis-server: Starting redis-server:
Jul 14 00:10:36 localhost.localdomain systemd: PID file /var/run/redis.pid not readable (yet?) after start.
Jul 14 00:10:36 localhost.localdomain systemd: Started SYSV: Redis is a persistent key-value database.
reidis扩展,不是redis服务端。
类似mysql 扩展和 mysql 服务端的区别
现在问题在这个地方,在单独的php文件测试redis功能都很正常,在workerman里写就报错
workerman是php命令行运行的,你要保证php命令行装了redis扩展。
运行 php -m ,如果没看到redis,就是php命令行没装redis扩展。
手册上有扩展安装方法
http://doc3.workerman.net/appendices/install-extension.html
刚刚用php -m查看,确实没有redis .,现在在非命令行下已经有redis,不知道在命令行redis怎么安装
通过卸载重装redis,已经不报错了。谢谢。