Skip to content

Commit

Permalink
基本完成Room类
Browse files Browse the repository at this point in the history
  • Loading branch information
RunsTp committed Apr 18, 2018
1 parent 6bac3af commit 844d63b
Show file tree
Hide file tree
Showing 10 changed files with 1,420 additions and 52 deletions.
14 changes: 11 additions & 3 deletions EasySwooleEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
use \EasySwoole\Core\Http\Request;
use \EasySwoole\Core\Http\Response;
use \EasySwoole\Core\Component\Di;
use \App\Utility\Redis;

use \MysqliDb;
use \App\Utility\Redis;

use App\Socket\Parser\WebSocket;
use App\Socket\Logic\WebSocket\Room;

Class EasySwooleEvent implements EventInterface {

Expand All @@ -30,12 +32,18 @@ public function frameInitialize(): void

public function mainServerCreate(ServerManager $server,EventRegister $register): void
{
// TODO: Implement mainServerCreate() method.
//注册WebSocket处理
EventHelper::registerDefaultOnMessage($register, new WebSocket());
//注册onClose事件
$register->add($register::onClose, function (\swoole_server $server, $fd, $reactorId) {
//清除Redis fd的全部关联
Room::getInstance()->close($fd);
});

Di::getInstance()->set('MYSQL', MysqliDb::class, Config::getInstance()->getConf('MYSQL'));
Di::getInstance()->set('REDIS', new Redis(Config::getInstance()->getConf('REDIS')));
}

public function onRequest(Request $request,Response $response): void
{
// TODO: Implement onRequest() method.
Expand Down
28 changes: 28 additions & 0 deletions Server/Socket/Controller/WebSocket/Base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
namespace App\Socket\Controller\WebSocket;

use EasySwoole\Core\Component\Spl\SplStream;
use EasySwoole\Core\Socket\Client\WebSocket;
use EasySwoole\Core\Socket\Common\CommandBean;
use EasySwoole\Core\Socket\AbstractInterface\WebSocketController;

class Base extends WebSocketController
{
public function __construct(WebSocket $client,CommandBean $request,SplStream $response)
{
parent::__construct($client, $request, $response);
$this->_initialize();
}

protected function _initialize(){}

/**
* 访问找不到的action
* @param ?string $actionName 找不到的name名
* @return string
*/
public function actionNotFound(?string $actionName)
{
$this->response()->write("action call {$actionName} not found");
}
}
27 changes: 27 additions & 0 deletions Server/Socket/Controller/WebSocket/Live.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace App\Socket\Controller\WebSocket;

use EasySwoole\Core\Socket\Response;

use App\Socket\Logic\WebSocket\Room;

/**
*
*/
class Live extends Base
{
protected function _initialize(){

}

public function test()
{
$id = mt_rand(0, 10000);
Room::getInstance()->intoRoom('5513', $id, $this->client()->getFd());
Room::getInstance()->selectRoom('5513');
$this->response()->write('your fd is '.$this->client()->getFd());
// var_dump(Room::getInstance()->getUserFd('77'));
Room::getInstance()->sendToRoom('5513', json_encode(['code' => 200, 'msg' => 'ok']));
}

}
42 changes: 0 additions & 42 deletions Server/Socket/Controller/WebSocket/Room.php

This file was deleted.

124 changes: 124 additions & 0 deletions Server/Socket/Logic/WebSocket/Room.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php
namespace App\Socket\Logic\WebSocket;

use EasySwoole\Core\Swoole\ServerManager;
use EasySwoole\Core\Swoole\Task\TaskManager;
use EasySwoole\Core\Component\Di;

/**
*
*/
class Room
{
private static $instance;
private function __construct(){}

/**
* 获取实例
* @return self Room
*/
public static function getInstance()
{
if(!isset(self::$instance)){
self::$instance = new static();
}
return self::$instance;
}

/**
* 获取Redis实例
* @return object redis
*/
public static function getRedis()
{
return Di::getInstance()->get('REDIS');
}

/**
* 进入房间
* @param string $roomId 房间id
* @param string $userId 用户id
* @param string $fd 链接id
*/
public function intoRoom($roomId, $userId, $fd)
{
//全局在线 Redis zSet
self::getRedis()->handler()->zAdd('online', $fd, $userId);
//关系映射 Redis zSet
self::getRedis()->handler()->zAdd('room_map', $roomId, $fd);
//房间在线 Redis list
self::getRedis()->handler()->lPush('room:'. $roomId, $fd);
}

/**
* 查询房间
* @param string $roomId 房间id
* @return array 房间内人的fd
*/
public function selectRoom($roomId)
{
return self::getRedis()->handler()->lrange('room:'. $roomId, 0, -1);
}

/**
* 获取用户fd
* @param string $userId 用户id
* @return string 用户fd
*/
public function getUserFd($userId)
{
return self::getRedis()->handler()->zScore('online', $userId);
}

/**
* 获取roomId
* @param string $fd fd
* @return string roomId
*/
public function getRoomId($fd)
{
return self::getRedis()->handler()->zScore('room_map', $fd);
}

/**
* 删除房间内的fd
* @param string $roomId roomId
* @param string $fd fd
* @return int 删除数量
*/
public function removeRoomFd($roomId, $fd)
{
self::getRedis()->handler()->lRem('room:'. $roomId, $fd, 0);
self::getRedis()->handler()->zRem('room_map', $fd);
}

/**
* 关闭连接
* @param string $fd 链接id
*/
public function close($fd)
{
$roomId = $this->getRoomId($fd);
$this->removeRoomFd($roomId, $fd);
self::getRedis()->handler()->zRemRangeByScore('online', $fd, $fd);
}

/**
* 发送信息给房间里的所有人
* @param string $roomId roomId
* @param string $message 信息
* @return
*/
public function sendToRoom($roomId, $message)
{
//异步推送
TaskManager::async(function ()use($roomId, $message){
$list = $this->selectRoom($roomId);
foreach ($list as $fd) {
ServerManager::getInstance()->getServer()->push($fd, $message);
}
});
}


}
3 changes: 1 addition & 2 deletions Server/Socket/Parser/WebSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ public function decode($raw, $client)
if ('PING' === $raw) {
return json_encode(['code' => 200, 'message' => 'ok']);
}

$commandLine = json_decode($raw, true);
if (!$commandLine) {
if (!is_array($commandLine)) {
return 'unknown command';
}

Expand Down
Loading

0 comments on commit 844d63b

Please sign in to comment.