<?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://wtbis.cn/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace GatewayWorker\Lib;
use Exception;
use GatewayWorker\Protocols\GatewayProtocol;
use Workerman\Connection\TcpConnection;
/**
* 數(shù)據(jù)發(fā)送相關(guān)
*/
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) {//消息接受者無(wú)法立刻查看時(shí),將消息設(shè)置為未讀
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 實(shí)例
*/
報(bào)錯(cuò)信息
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的命名空間,應(yīng)該是 new \Redis();
php.net/manual/zh/language.namespaces.php
改成 new \Redis();現(xiàn)在還有報(bào)錯(cuò) WORKER EXIT UNEXPECTED E_ERROR Class 'Redis' not found in /pi/components/com_work/GatewayWorker/Lib/Gateway.php on line 31
安裝了。下面是# 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擴(kuò)展,不是redis服務(wù)端。
類(lèi)似mysql 擴(kuò)展和 mysql 服務(wù)端的區(qū)別
現(xiàn)在問(wèn)題在這個(gè)地方,在單獨(dú)的php文件測(cè)試redis功能都很正常,在workerman里寫(xiě)就報(bào)錯(cuò)
workerman是php命令行運(yùn)行的,你要保證php命令行裝了redis擴(kuò)展。
運(yùn)行 php -m ,如果沒(méi)看到redis,就是php命令行沒(méi)裝redis擴(kuò)展。
手冊(cè)上有擴(kuò)展安裝方法
http://doc3.workerman.net/appendices/install-extension.html
剛剛用php -m查看,確實(shí)沒(méi)有redis .,現(xiàn)在在非命令行下已經(jīng)有redis,不知道在命令行redis怎么安裝