react和react2_初步了解React

react和react2

For the past couple of years, Node.js has been drawing increasing amounts of attention as a promising web technology. While it has some strengths, like being event driven, some people just want to stick to PHP. For over a year now, however, there has been a similar project for PHP named React.

在过去的两年中,作为一种有前途的Web技术, Node.js引起了越来越多的关注。 尽管它有一些优势,例如受事件驱动,但有些人只想坚持使用PHP。 但是,一年多以来,PHP已有一个类似的项目称为React

React is mostly coded by Igor Wiedler, who is also a prominent contributor to the Silex framework. While reading through the React examples, it really does look similar to Node.js.

React主要由Igor Wiedler编码,他也是Silex框架的杰出贡献者。 在阅读React示例时,它的确确实类似于Node.js。

Like Node.js, you’re encouraged to write your applications to execute asynchronously. For those who may not be familiar with asynchronous programming, the server answers requests with a single thread but uses worker threads for time-consuming tasks like writing to a file, reading from a database, retrieving results from the Twitter API, etc. When the task is finished, the worker returns the result to the main thread which finalizes the request. This style of programming can give an enormous speed boost.

与Node.js一样,建议您编写应用程序以异步执行。 对于可能不熟悉异步编程的用户,服务器使用单个线程来回答请求,但使用工作线程来执行耗时的任务,例如写入文件,从数据库读取,从Twitter API检索结果等。任务完成后,工作程序将结果返回到主线程,最终确定请求。 这种编程风格可以极大地提高速度。

得到React (Getting React)

React is available with Composer through Packagist (if you’re not familiar with working with Composer, check out my previous article). To install React, create a new composer.json file with the following content:

通过Packagist可以在Composer中使用React (如果您不熟悉使用Composer,请查阅我的上一篇文章 )。 要安装React,请创建一个具有以下内容的新composer.json文件:

{
"require": {
"react/react": "0.2.*"
}
}

Open a console, navigate to where the file is, and run php composer install. React and its dependencies will be retrieved and can then be used by including the vendor/autoload.php file.

打开控制台,导航到文件所在的位置,然后运行php composer install 。 React及其依赖关系将被检索,然后可以通过包含vendor/autoload.php文件来使用。

创建一个简单的服务器 (Creating a Simple Server)

With Node.js you create a server script yourself instead of using a server application like Apache or Nginx. You set up an event function, define a port which your server will listen to, and then wait for requests. With each request the event function is executed. This is the same in React.

使用Node.js,您可以自己创建服务器脚本,而不必使用Apache或Nginx之类的服务器应用程序。 您设置事件函数,定义服务器将监听的端口,然后等待请求。 对于每个请求,都会执行事件功能。 在React中也是如此。

This example is taken from the the React project’s README.md file:

这个例子取自React项目的README.md文件:

<?php
require_once 'vendor/autoload.php';

$i = 0;
$app = function ($request, $response) use (&$i) {
$i++;
$text = "This is request number $i.n";
$headers = array('Content-Type' => 'text/plain');

$response->writeHead(200, $headers);
$response->end($text);
};

$loop = ReactEventLoopFactory::create();
$socket = new ReactSocketServer($loop);
$http = new ReactHttpServer($socket);

$http->on('request', $app);

$socket->listen(1337);
$loop->run();

First it includes the Composer autoloader file, and then sets the variable $i to zero which will be used to count the requests. Then, something that is not an overly common practice in the PHP community, our event function which will handle all incoming requests is stored to a variable. The function sets the response’s headers and prints the number of handled requests.

首先,它包含Composer自动加载器文件,然后将变量$i设置为零,该变量将用于计数请求。 然后,在PHP社区中不太常见的做法是,将处理所有传入请求的事件函数存储到变量中。 该函数设置响应的标题并打印已处理请求的数量。

The script creates an event loop that handles unblocked I/O for us, a socket, and then the server. Finally, the event function is assigned, the socket is set to listen on port 1337, and the event loop is run.

该脚本创建一个事件循环,为我们,套接字以及服务器处理无阻塞的I / O。 最后,分配事件功能,将套接字设置为侦听端口1337,然后运行事件循环。

If you save the code as server.php and run it from the command line with php server.php, and then navigate your browser to http://localhost:1337, you’ll see the response. Don’t forget to check your command line to see how many requests have been handled!

如果将代码另存为server.php并使用php server.php从命令行运行它,然后将浏览器导航到http:// localhost:1337 ,则会看到响应。 不要忘了检查您的命令行以查看已处理了多少个请求!

进一步探索React (Exploring React Further)

React can do much more than act as an HTTP server. We can use it for some much cooler things. Think of this example: you’re a spy for the CIA and you want to transfer data to your boss. The boss creates a server, you connect as a client via a socket, and everything you type is then logged in the server’s log.

React不仅可以充当HTTP服务器,还可以做更多的事情。 我们可以将其用于一些更酷的事情。 想想这个例子:您是CIA的间谍,并且想要将数据传输给老板。 老板创建一个服务器,您通过套接字作为客户端连接,然后键入的所有内容都会记录在服务器的日志中。

Support for sockets is handled by the socket component in React, and for this example you don’t need the full React library. The following composer.json should be sufficient:

对套接字的支持由React中的套接字组件处理,对于本示例,您不需要完整的React库。 以下composer.json应该足够了:

{
"require": {
"react/socket": "0.2.*"
}
}

And this is the example code:

这是示例代码:

<?php
require_once 'vendor/autoload.php';

$loop = ReactEventLoopFactory::create();
$socket = new ReactSocketServer($loop);

$socket->on('connection', function ($conn) use ($loop) {
$conn->write("Successfully connected to the writing servern");
echo 'client connected';
$dataStream = new ReactStreamStream(fopen('data.txt', 'w'), $loop);

$conn->on('data', function($data) use ($conn, $dataStream) {
$dataStream->write($data);
});

$conn->on('end', function() {
echo 'connection closed';
});
});

$socket->listen(4000);
$loop->run();

We create an event loop again and then create a socket. When the connection event is emitted, we print a message to the console. We create a new writing stream and name it $dataStream and when we receive data, we push it down the stream and the data will be written to our file. When the client disconnects, the stream is closed.

我们再次创建一个事件循环,然后创建一个套接字。 当发出连接事件时,我们将消息打印到控制台。 我们创建一个新的写入流,并将其命名为$dataStream ,当我们接收到数据时,将其下推并将其写入数据。 当客户端断开连接时,流将关闭。

After running php server.php you can connect by using nc localhost 4000. Everything you type and press enter afterwards is logged to data.txt.

运行php server.php您可以使用nc localhost 4000进行连接。 您键入的所有内容,然后按Enter键都将记录到data.txt

谁会使用React? (Who Would Use React?)

So React offers some really awesome functionality, but why would you use a younger library that’s not yet stable instead the more mature Node.js, which also has a big community? I was thinking this myself, so I asked Igor Wiedler:

因此,React提供了一些非常强大的功能,但是为什么要使用一个尚未稳定的较年轻的库,而不是更成熟的Node.js(它也有一个庞大的社区)? 我本人也在思考,所以我问伊戈尔·维德勒:

I wouldn’t really recommend running react in production at this point. Some of the lower level parts are quite stable, the higher level stuff not so much. The target audience right now is people who like to experiment with cutting-edge technologies and aren’t afraid to debug stuff when it breaks. I particular: I’m interested in having new libraries based on the react event loop, so that they can be used together.

我现在真的不建议在生产中运行React。 一些较低级别的部分非常稳定,较高级别的东西则不是很多。 现在的目标受众是喜欢尝试尖端技术并且不怕出现故障时调试的人。 我特别:我对基于react事件循环的新库感兴趣,以便可以将它们一起使用。

结论 (Conclusion)

Is React an interesting project? I think so, even if it just ports Node.js’ functionality, it does so in a cool way, and who would have thought that it was even possible to do such things in PHP? At least I didn’t.

React是一个有趣的项目吗? 我认为是这样,即使它只是移植Node.js的功能,它也是以一种很酷的方式进行的,还有谁会想到甚至可以用PHP来完成这些工作呢? 至少我没有。

React seems like a promising project, even while it’s not production-ready yet, it seems to have some cool features and a good developer to maintain it. Of course I can’t explain every aspect of React in this short article, so if you want to learn more about React:

React似乎是一个有前途的项目,即使它尚未投入生产,它似乎也有一些很酷的功能,并且有很好的开发人员来维护它。 当然,我不能在这篇简短的文章中解释React的各个方面,因此,如果您想了解更多有关React的信息,

Image via Fotolia

图片来自Fotolia

翻译自: https://www.sitepoint.com/a-first-look-at-react/

react和react2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值