Exemplo n.º 1
0
            } else {
                if ($msg['type'] == 'popo') {
                    $redis = new Redis();
                    $redis->connect('localhost', 6379);
                    $popid = $redis->incr('popoid');
                    $msg['id'] = 'p' . $popid;
                    $client = new RouterClient('127.0.0.1', 9010);
                    $client->sendAllMsg(json_encode($msg));
                } else {
                    if ($msg['type'] == 'boom') {
                        $client = new RouterClient('127.0.0.1', 9010);
                        $client->sendAllMsg($data);
                    } else {
                        if ($msg['type'] == 'message') {
                            //$msg['text'] = mb_substr($msg['text'], 0, 10);
                            $client = new RouterClient('127.0.0.1', 9010);
                            $client->sendAllMsg($data);
                        } else {
                            if ($msg['type'] == 'kill') {
                                $redis = new Redis();
                                $key = 'role:' . $msg['id'];
                                $redis->connect('localhost', 6379);
                                $redis->hset($key, 'die', 1);
                            }
                        }
                    }
                }
            }
        }
    }
}
Exemplo n.º 2
0
<?php

require 'api.php';
date_default_timezone_set('Asia/Shanghai');
$data = file_get_contents("php://input");
$info = json_decode($data, true);
$router = new RouterClient('127.0.0.1', 9010);
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$sid = $_SERVER['SESSIONID'];
if (empty($_SERVER['EVENT'])) {
    if ($info['type'] == 'login') {
        //发送登陆信息
        $info['name'] = str_replace(array('<', '>'), array('&lt;', '&gt;'), $info['name']);
        $router->sendAllMsg(json_encode(array('type' => 'login', 'name' => $info['name'], 'id' => $sid)));
        //获取所有用户信息
        $keys = $redis->keys('chatuser:*');
        $users = array();
        foreach ($keys as $k) {
            $u = $redis->hgetAll($k);
            $users[] = $u;
        }
        //设置当前用户信息
        $redis->hMset("chatuser:{$sid}", array("name" => $info['name'], 'id' => $sid));
        //返回用户列表
        $body = json_encode(array('type' => 'list', 'list' => $users));
        header("Content-Length: " . strlen($body));
        echo $body;
    } else {
        if ($info['type'] == 'join') {
            //加入分组