Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
323 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.idea | ||
.DS_Store | ||
|
||
vendor | ||
demo_composer/vendor | ||
demo_composer/composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
/** | ||
* User: shenzhe | ||
* Date: 13-6-17 | ||
*/ | ||
|
||
|
||
namespace Protocol; | ||
use ZPHP\Core; | ||
use ZPHP\View; | ||
use ZPHP\Core\Config; | ||
use ZPHP\Protocol\IProtocol; | ||
use ZPHP\Common\Route as ZRoute; | ||
use ZPHP\Common\Utils as ZUtils; | ||
|
||
class WebSocket implements IProtocol | ||
{ | ||
private $_ctrl = 'main\\main'; | ||
private $_method = 'main'; | ||
private $_params = array(); | ||
private $_view_mode = ''; | ||
private $_tpl_file = ''; | ||
|
||
/** | ||
* 直接 parse $_REQUEST | ||
* @param $_data | ||
* @return bool | ||
*/ | ||
public function parse($data) | ||
{ | ||
$this->_ctrl = Config::getField('project', 'default_ctrl_name', 'main\\main'); | ||
$this->_method = Config::getField('project', 'default_method_name', 'main'); | ||
$apn = Config::getField('project', 'ctrl_name', 'a'); | ||
$mpn = Config::getField('project', 'method_name', 'm'); | ||
if (isset($data[$apn])) { | ||
$this->_ctrl = \str_replace('/', '\\', $data[$apn]); | ||
} | ||
if (isset($data[$mpn])) { | ||
$this->_method = $data[$mpn]; | ||
} | ||
if(!empty($_SERVER['PATH_INFO'])) { | ||
$routeMap = ZRoute::match(Config::get('route', false), $_SERVER['PATH_INFO']); | ||
if(is_array($routeMap)) { | ||
$this->_ctrl = $routeMap[0]; | ||
$this->_method = $routeMap[1]; | ||
if(!empty($routeMap[2]) && is_array($routeMap[2])) { | ||
//参数优先 | ||
$data = $data + $routeMap[2]; | ||
} | ||
} | ||
} | ||
$this->_params = $data; | ||
$this->_tpl_file = str_replace('\\', DS, $this->_ctrl) . DS . $this->_method . '.php'; | ||
return true; | ||
} | ||
|
||
public function getCtrl() | ||
{ | ||
return $this->_ctrl; | ||
} | ||
|
||
public function getMethod() | ||
{ | ||
return $this->_method; | ||
} | ||
|
||
public function getParams() | ||
{ | ||
return $this->_params; | ||
} | ||
|
||
public function setFd($fd) | ||
{ | ||
$this->_fd = $fd; | ||
} | ||
|
||
public function getFd() | ||
{ | ||
return $this->_fd; | ||
} | ||
|
||
public function setViewMode($mode) | ||
{ | ||
$this->_view_mode = $mode; | ||
} | ||
|
||
public function setTplFile($tpl) | ||
{ | ||
$this->_tpl_file = $tpl; | ||
} | ||
|
||
public function display($model) | ||
{ | ||
($viewMode = $this->_view_mode) || ($viewMode = Config::getField('project', 'view_mode', '')); | ||
if(is_array($model) && !empty($model['_view_mode'])) { | ||
$viewMode = $model['_view_mode']; | ||
unset($model['_view_mode']); | ||
} | ||
$this->_view_mode = ''; | ||
if(empty($viewMode)) { | ||
if (ZUtils::isAjax()) { | ||
$viewMode = 'Json'; | ||
} else { | ||
$viewMode = 'Php'; | ||
} | ||
} | ||
|
||
$view = View\Factory::getInstance($viewMode); | ||
if ('Php' === $viewMode) { | ||
if(is_array($model) && !empty($model['_tpl_file'])) { | ||
$view->setTpl($model['_tpl_file']); | ||
unset($model['_tpl_file']); | ||
} else if(!empty($this->_tpl_file)){ | ||
$view->setTpl($this->_tpl_file); | ||
$this->_tpl_file = null; | ||
} else { | ||
throw new \Exception("tpl file empty"); | ||
} | ||
} | ||
$view->setModel($model); | ||
return $view->display(); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
/** | ||
* User: shenzhe | ||
* Date: 13-6-17 | ||
*/ | ||
|
||
|
||
namespace Server; | ||
use ZPHP\Core, | ||
ZPHP\Protocol; | ||
|
||
class WebSocket | ||
{ | ||
public function run() | ||
{ | ||
$server = Protocol\Factory::getInstance('Protocol\WebSocket'); | ||
$server->parse($_REQUEST); | ||
return Core\Route::route($server); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
namespace ctrl; | ||
use ZPHP\Controller\IController; | ||
use ZPHP\Core\Config as ZConfig; | ||
|
||
class login implements IController | ||
{ | ||
private $_server; | ||
public function setServer($server) | ||
{ | ||
$this->_server = $server; | ||
} | ||
|
||
public function _before() | ||
{ | ||
return true; | ||
} | ||
|
||
public function _after() | ||
{ | ||
// | ||
} | ||
|
||
public function check() | ||
{ | ||
print_r($this->_server->getParams()); | ||
} | ||
|
||
public function reg() | ||
{ | ||
return []; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
use ZPHP\Common\Route as ZRoute; | ||
?> | ||
<html> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | ||
<link href="/static/css/bootstrap.css" rel="stylesheet"> | ||
<script src="/static/js/jquery.js"></script> | ||
<script src="/static/js/bootstrap.js"></script> | ||
</head> | ||
<body> | ||
<style> | ||
* { | ||
font-size: 14px; | ||
} | ||
|
||
body { | ||
background-color:rgb(131, 131, 131); | ||
} | ||
|
||
input { | ||
height: 36px; | ||
margin: 5px; | ||
} | ||
|
||
.login { | ||
width: 600px; | ||
margin: 200px auto; | ||
} | ||
#show_avatar{ | ||
width: 120px; | ||
position: relative; | ||
left: 420px; | ||
top: -140px; | ||
} | ||
#show_avatar img{ | ||
border-radius: 8px; | ||
} | ||
</style> | ||
<script> | ||
/* db_connect('my.db', function(db) { | ||
}); */ | ||
function falert(text) { | ||
$('#alert_msg').html(text) | ||
} | ||
function checkForm(o) { | ||
var name = $.trim($('input[name=name]').val()); | ||
var avatar = $.trim($('input[name=avatar]').val()); | ||
if (name == '' || name == '请输入一个昵称') { | ||
falert("请输入一个昵称^-^"); | ||
o.name.focus(); | ||
return false; | ||
} | ||
if (avatar == '' || avatar == '请输入头像图片URL') { | ||
falert("请输入头像的图片,有图有真相。^-^"); | ||
o.name.focus(); | ||
return false; | ||
} | ||
return true; | ||
} | ||
function showAvatar(pic) { | ||
if(pic=='') { | ||
return | ||
} | ||
$('#avatar_pic').attr('src', pic); | ||
$('#avatar_pic').show(); | ||
} | ||
function pic_error(o) { | ||
$(o).hide(); | ||
falert("图片不存在,请重新填写一个吧。^-^"); | ||
$('input[name=avatar]').val(''); | ||
} | ||
</script> | ||
|
||
<div class='container login' > | ||
<form action="<?=ZRoute::makeUrl('login', 'check')?>" method="post" class="well" | ||
onsubmit="return checkForm(this)" style="height: 162px"> | ||
<h3>zphp websocket chat 注册</h3> | ||
<input type="text" name="name" placeholder="请输入用户名" required="required"/></br> | ||
<input type="password" name="password" placeholder="请输入密码" required="required"/></br> | ||
<input type="submit" class="btn btn-primary" value="注册" id="login_submit"> | ||
<input type="reset" class="btn" value="Reset"> [<a href="<?=ZRoute::makeUrl('main', 'main')?>">登录</a>] | ||
</form> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
use ZPHP\Common\Route as ZRoute; | ||
?> | ||
<html> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | ||
<link href="/static/css/bootstrap.css" rel="stylesheet"> | ||
<script src="/static/js/jquery.js"></script> | ||
<script src="/static/js/bootstrap.js"></script> | ||
</head> | ||
<body> | ||
<style> | ||
* { | ||
font-size: 14px; | ||
} | ||
|
||
body { | ||
background-color:rgb(131, 131, 131); | ||
} | ||
|
||
input { | ||
height: 36px; | ||
margin: 5px; | ||
} | ||
|
||
.login { | ||
width: 600px; | ||
margin: 200px auto; | ||
} | ||
#show_avatar{ | ||
width: 120px; | ||
position: relative; | ||
left: 420px; | ||
top: -140px; | ||
} | ||
#show_avatar img{ | ||
border-radius: 8px; | ||
} | ||
</style> | ||
|
||
<div class='container login' > | ||
<form action="<?=ZRoute::makeUrl('login', 'check')?>" method="post" class="well" style="height: 162px"> | ||
<h3>zphp websocket chat</h3> | ||
<input type="text" name="name" placeholder="请输入用户名" required="required"/></br> | ||
<input type="password" name="password" placeholder="请输入密码" required="required"/></br> | ||
<input type="submit" class="btn btn-primary" value="Login" id="login_submit"> | ||
<input type="reset" class="btn" value="Reset"> [<a href="<?=ZRoute::makeUrl('login', 'reg')?>">注册</a>] | ||
</form> | ||
</div> | ||
</body> | ||
</html> |