2 qq 18757961 qq_18757961 于 2017.09.10 19:11 提问

最多有2000个点同时与服务器互传数据。用PHPredis队列socket,请大神写个示例代码

我现在只知道:以下两部分,但是不知如何将两者结合?让redis来处理socket的2000个连接?请大神帮我结合下,写个基本的样板我参考下,谢谢啦!

redis的基础代码:

//连接本地的 Redis 服务
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Connection to server sucessfully";
//存储数据到列表中
$redis->lpush("tutorial-list", "Redis");
$redis->lpush("tutorial-list", "Mongodb");
$redis->lpush("tutorial-list", "Mysql");
// 获取存储的数据并输出
$arList = $redis->lrange("tutorial-list", 0 ,5);
echo "Stored string in redis";
print_r($arList);

//连接本地的 Redis 服务
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Connection to server sucessfully";
// 获取数据并输出
$arList = $redis->keys("*");
echo "Stored keys in redis:: ";
print_r($arList);

socket部分:
set_time_limit(0);

$ip = '0.0.0.0';

$port = 8081;

do {

if(($sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP)) < 0) {

echo "socket_create() 失败的原因是:".socket_strerror($sock)."\n";

}

if(($ret = socket_bind($sock,$ip,$port)) < 0) {

echo "socket_bind() 失败的原因是:".socket_strerror($ret)."\n";

}

if(($ret = socket_listen($sock,4)) < 0) {

echo "socket_listen() 失败的原因是:".socket_strerror($ret)."\n";

}

if (($msgsock = socket_accept($sock)) < 0) {  
    echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n";  
    break;  
} else {            
    $msg = "\nPHP Test Server. \n" ."用quit,shutdown,sun等命令测试.\n";  
    socket_write($msgsock, $msg, strlen($msg)); 
    socket_last_error();   
    do{
        if(false ===($buf = socket_read($msgsock,8192))){
        echo "socket_read() failed: reason: " . socket_strerror($ret) . "\n";  
                break 2; 
        } 
        if (!$buf = trim($buf)) {  
            continue;  
    }  
    if ($buf == 'sun') {  
            echo'what are you doing?'; 

            $msg = "\nPHP Test Server. \n" ."what are you doing?.\n";  
        socket_write($msgsock, $msg, strlen($msg));  
    }  
    if ($buf == 'quit') {  
            break;      /*服务器还在监听*/
    }  
    if ($buf == 'shutdown') {  
            socket_close($msgsock);  
            break 2;    /*服务器已经断开连接*/
    }                     
    $talkback = "receive:" . $buf;  
    socket_write($msgsock, $talkback, strlen($talkback));
    echo $talkback . '<br /n>';
    }while(true);

}  
//socket_close($msgsock);      

} while (true);

//socket_close($sock);

Csdn user default icon
上传中...
上传图片
插入图片