Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
LinkedDestiny committed Jul 16, 2016
1 parent e6b521b commit e08113d
Show file tree
Hide file tree
Showing 27 changed files with 1,553 additions and 233 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 7 additions & 11 deletions src/Chatroom/Server/Swoole/Core/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,17 @@

class Route
{
public static function route($server)
public static function route($server, $socket)
{
$action = Config::get('ctrl_path', 'ctrl') . '\\' . $server->getCtrl();
$class = Factory::getInstance($action);
if (!($class instanceof IController)) {
throw new \Exception("ctrl error");
}
$class->setServer($server);
$action = Config::get('ctrl_path', 'ctrl') . '\\' . $server['ctrl'];
$class = new $action($socket);
$before = $class->_before();
$view = $exception = null;
if ($before) {
try {
$method = $server->getMethod();
$method = $server['method'];
if (\method_exists($class, $method)) {
$view = $class->$method();
$view = $class->$method($server);
} else {
throw new \Exception("no method {$method}");
}
Expand All @@ -38,8 +34,8 @@ public static function route($server)
throw $exception;
}
if (null === $view) {
return;
return null;
}
return $server->display($view);
return json_encode($view, JSON_UNESCAPED_UNICODE);
}
}
7 changes: 2 additions & 5 deletions src/Chatroom/Server/Swoole/Socket/ICallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ public function onStart();
* 当有client连接上socket服务时,回调此方法
*/
public function onConnect();

/**
* 当有数据到达时,回调此方法
*/
public function onReceive();

public function onReceive($server, $frame);

/**
* 当有client断开时,回调此方法
Expand Down
20 changes: 3 additions & 17 deletions src/Chatroom/Server/Swoole/Socket/Swoole.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,8 @@ public function __construct(array $config)
throw new \Exception("no swoole extension. get: https://github.com/matyhtf/swoole");
}
$this->config = $config;
$this->serv = new \swoole_server($config['host'], $config['port'], $config['work_mode']);
$this->serv->set(array(
'reactor_num' => empty($config['reactor_num']) ? 2 : $config['reactor_num'], //reactor thread num
'worker_num' => empty($config['worker_num']) ? 2 : $config['worker_num'], //worker process num
'task_worker_num' => empty($config['task_worker_num']) ? 2 : $config['task_worker_num'], //task worker process num
'backlog' => empty($config['backlog']) ? 128 : $config['backlog'], //listen backlog));
'max_request' => empty($config['max_request']) ? 1000 : $config['max_request'],
'max_conn' => empty($config['max_conn']) ? 100000 : $config['max_conn'],
'dispatch_mode' => empty($config['dispatch_mode']) ? 2 : $config['dispatch_mode'],
'log_file' => empty($config['log_file']) ? '/tmp/swoole.log' : $config['log_file'],
'daemonize' => empty($config['daemonize']) ? 0 : 1,
// 'open_length_check' => empty($config['open_length_check']) ? false : $config['open_length_check'],
// 'package_length_offset' => empty($config['package_length_offset']) ? 0 : $config['package_length_offset'],
// 'package_body_offset' => empty($config['package_body_offset']) ? 4 : $config['package_body_offset'],
// 'package_length_type' => empty($config['package_length_type']) ? 'N' : $config['package_length_type'],
));
$this->serv = new \swoole_websocket_server($config['host'], $config['port'], $config['work_mode']);
$this->serv->set($config);
}

public function setClient($client)
Expand All @@ -51,7 +37,7 @@ public function run()
{
$this->serv->on('Start', array($this->client, 'onStart'));
$this->serv->on('Connect', array($this->client, 'onConnect'));
$this->serv->on('Receive', array($this->client, 'onReceive'));
$this->serv->on('Message', array($this->client, 'onReceive'));
$this->serv->on('Close', array($this->client, 'onClose'));
$handlerArray = array(
'onTimer',
Expand Down
48 changes: 23 additions & 25 deletions src/Chatroom/Server/app/ctrl/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@

class Chat extends BaseController
{
private $reids;
private $redis;
protected $server;

public function setServer($server) {
public function __construct($server)
{
$this->server = $server;
$this->params = $server->getParams();
$this->redis = DBFactory::getInstance('Chat');
}

public function online() {
$fd = $this->params['fd'];
$name = $this->params['name'];
public function online($params) {
$fd = $params['fd'];
$name = $params['name'];

echo $name . PHP_EOL;
$data = json_encode( array(
'op' => 'online',
Expand All @@ -26,37 +28,35 @@ public function online() {
));
$this->redis->online( $fd );
$this->redis->setUserInfo( $fd , array( 'name' => $name ) );
$data = pack("Na*", strlen($data) , $data );

$fd_list = $this->redis->getFdList();
unset($fd_list[array_search( $fd ,$fd_list)]);
$this->sendMessage( $fd_list , $data );

$this->getOnlineList();
$this->getOnlineList($params);
}

public function offline() {
$fd = $this->params['fd'];
public function offline($params) {
$fd = $params['fd'];

$data = json_encode( array(
'op' => 'offline',
'fd' => $fd,
));
$data = pack("Na*", strlen($data) , $data );
$this->redis->offline( $fd );
$this->sendMessage( $this->redis->getFdList( $this->redis->getChannel( $fd ) ), $data );
}

public function changeChannel() {
$fd = $this->params['fd'];
$from = $this->params['from'];
$to = $this->params['to'];
public function changeChannel($params) {
$fd = $params['fd'];
$from = $params['from'];
$to = $params['to'];

$data = json_encode( array(
'op' => 'online',
'fd' => $fd,
'name' => $this->redis->getUserInfo( $fd , "name")
));
$data = pack("Na*", strlen($data) , $data );

$fd_list = $this->redis->getFdList($to);
unset($fd_list[array_search( $fd ,$fd_list)]);
Expand All @@ -67,23 +67,22 @@ public function changeChannel() {
}
}

public function send() {
$fd = $this->params['fd'];
$sendto = $this->params['sendto'];
$msg = $this->params['msg'];
public function send($params) {
$fd = $params['fd'];
$sendto = $params['sendto'];
$msg = $params['msg'];
$data = json_encode( array(
'op' => 'recv',
'from' => $fd,
'msg' => $msg
));
$data = pack("Na*", strlen($data) , $data );
$fd_list = $this->redis->getFdList($sendto);
unset($fd_list[array_search( $fd ,$fd_list)]);
$this->sendMessage( $fd_list, $data );
}

public function getOnlineList() {
$fd = $this->params['fd'];
public function getOnlineList($params) {
$fd = $params['fd'];
$fd_list = $this->redis->getFdList();
unset($fd_list[array_search( $fd ,$fd_list)]);
$list = array();
Expand All @@ -94,12 +93,11 @@ public function getOnlineList() {
'op' => 'onlineList',
'list' => $list
));
$data = pack("Na*", strlen($data) , $data );
$this->sendMessage( array( $fd ), $data );
}

private function sendMessage( $fd_list , $msg ) {
$server = $this->server->getServer();
$server = $this->server;
$data = array(
'ctrl' => 'Chat',
'task' => 'sendMessage',
Expand Down
37 changes: 0 additions & 37 deletions src/Chatroom/Server/app/parser/Adapter/ChatParser.php

This file was deleted.

79 changes: 0 additions & 79 deletions src/Chatroom/Server/app/parser/BaseParser.php

This file was deleted.

18 changes: 0 additions & 18 deletions src/Chatroom/Server/app/parser/Factory.php

This file was deleted.

22 changes: 0 additions & 22 deletions src/Chatroom/Server/app/parser/ProtocolParser.php

This file was deleted.

Loading

0 comments on commit e08113d

Please sign in to comment.