实际上我在php中创建了一个框架,它是Java Servlet Model的模拟器,但是使用了Facebook Big Pipe similer技术。
要清除,请查看此Servlet(PHP Servlet)
<?php
class login_servlet extends Servlet{
public function doGet($request, $response){
$response->getWriter()->write('hii');
} // __ end of doGet
public function doPost($request, $response){ } // end of doPost
} // __ Servlet
?>
所以当用户输入http://myweb.com/login
时将调用此servlet,映射将由框架完成,框架执行如下操作: -
$servlet_name = genrate_servlet_name('login');
$servlet_boj = new $servlet_name(new Request(), new Response());
$servlet_obj-> init(new ServletConfig());
$servlet_obj-> service();
$servlet_obj-> destroy();
所以我们可以说单个servlet是MVC中控制器的similer,并且根据doGet或doPost方法将由框架调用,如上面我用请求和响应参数说明的那样,但是视图被分成多个pagelet,ex : -
如图所示,此页面包含4个小页面: -
1个标题 2中间左侧部分 3中右部(登录部分) 4页脚
所以login_servlet代码如下所示: -
<?php
class login_servlet extends Servlet{
public function doGet($request, $response){
$writer = $response->getWriter();
$pg_strucutre = new PageletIndexStructure($request, $response);
$writer->writePagelet($pg_strucutre);
$pg = new PageletIndexHead($request, $response);
$writer->writePagelet($pg);
$pg = new PageletIndexLeft($request, $response);
$writer->writePagelet($pg);
$pg = new PageletIndexRight($request, $response);
$writer->writePagelet($pg);
$pg = new PageletIndexFooter($request, $response);
$writer->writePagelet($pg);
} // __ end of doGet
public function doPost($request, $response){ } // end of doPost
} // __ Servlet
?>
和pagelet类看起来这是每个pagelet必须扩展的: -
<?php
class Pagelet{
public $template_name = '';
public $js = array();
public $css = array();
public $output = null;
public $container_id = 'root'; // __default is root
private $request = null;
private $response = null;
function __construct($request, $response){
$this->request = $request;
$this->response = $response;
$this->doProcess();
/* default */
} // __ construct
public function doProcess(){
// __ here all code will comes
} // __doprocess
public function render(){
$arr = array(
'js' => $this->js,
'css' => $this->css,
'content' => $this->output,
'container_id' => $this->container_id,
'response_type' => 2
);
return json_encode($arr);
} // __ end of render
} // __ end of class
?>
并且在客户端响应看起来像这样: -
<!Doctype html>
<html>
<head>
<script type="text/javascript" src="js/jquery.min.js"> </script> <script type="text/javascript" src="js/require.js" data-main="js/load.js"></script> <script type="text/javascript" src="js/boot.js"> </script> <link rel="stylesheet" href="css/nnj.css" />
</head>
<body>
<div id="__global"> </div> <div style="position : fixed; width : 100%; height : 50px; background-color : black; bottom : 0px; left : 0px;"></div>
<iframe id ="__navigate" style=""> </iframe>
<script type="text/javascript"> boot.on_arrived({"response_type" : 1}); </script>
<script type='text/javascript'> boot.on_arrived({"js":["index_structure"],"css":["index_structure"],"content":"<div id=\"cntr\"> <div id=\"cntr_head\"> </div> <div> <div id=\"cntr_left\"> </div> <div id=\"cntr_right\"> </div> </div> <div id=\"cntr_footer\"> </div></div>","container_id":"root","response_type":2}); </script>
<!-- <code><div id="cntr"> <div id="cntr_head"> </div> <div> <div id="cntr_left"> </div> <div id="cntr_right"> </div> </div> <div id="cntr_footer"> </div></div></code> -->
<script type='text/javascript'> boot.on_arrived({"js":[""],"css":[""],"content":"<b> This is the login page : Enter your informations </b>","container_id":"cntr_head","response_type":2}); </script>
<!-- <code><b> This is the login page : Enter your informations </b></code> -->
<script type='text/javascript'> boot.on_arrived({"js":[""],"css":[""],"content":"<h2> login and join others</h2>","container_id":"cntr_left","response_type":2}); </script>
<!-- <code><h2> login and join others</h2></code> -->
<script type='text/javascript'> boot.on_arrived({"js":[""],"css":[""],"content":"<form action=\"/login\" method=\"post\"> Email : <input type=\"text\" name=\"email\"> <br> Pasword : <input type=\"password\" name=\"password\"> <br> <input type=\"submit\"></from><br><br><a href=\"/register\"> don't have account ? </a>","container_id":"cntr_right","response_type":2}); </script>
<!-- <code><form action="/login" method="post"> Email : <input type="text" name="email"> <br> Pasword : <input type="password" name="password"> <br> <input type="submit"></from><br><br><a href="/register"> don't have account ? </a></code> -->
<script type='text/javascript'> boot.on_arrived({"js":[""],"css":[""],"content":"<div style=\"width : 150px; text-align : left; display : inline-block;\"> About </div><div style=\"width : 150px; text-align : left; display : inline-block;\"> help </div><div style=\"width : 150px; text-align : left; display : inline-block;\"> Privacy </div><div style=\"width : 150px; text-align : left; display : inline-block;\"> Terms and Conditions </div><div style=\"width : 150px; text-align : left; display : inline-block;\"> Advertise </div>","container_id":"cntr_footer","response_type":2}); </script>
<!-- <code><div style="width : 150px; text-align : left; display : inline-block;"> About </div><div style="width : 150px; text-align : left; display : inline-block;"> help </div><div style="width : 150px; text-align : left; display : inline-block;"> Privacy </div><div style="width : 150px; text-align : left; display : inline-block;"> Terms and Conditions </div><div style="width : 150px; text-align : left; display : inline-block;"> Advertise </div></code> --> <script type="text/javascript"> boot.on_arrived({"response_type" : 3}); </script>
</body>
</html>
所以我的问题是我做得对,还是我需要改变一些东西,?抱歉弱英语:(,提前谢谢
答案 0 :(得分:1)
很难回答像#34;我做得对的问题吗?&#34;。您有什么需要具体反馈的吗?试着提出一个非常有针对性的问题。
关于Bigpipe的博文:https://www.facebook.com/notes/facebook-engineering/bigpipe-pipelining-web-pages-for-high-performance/389414033919
由于它不是开源的,我认为你不会发现很多关于内部的信息。这里有一个开放的实现:http://www.juhonkoti.net/2010/10/01/open-bigpipe-javascript-implementation。也许这可以帮助你进一步。