"Go" web games can be used in PHP for backend development? PHP libevent extension Installation and application

Source: Internet
Author: User

can web games use PHP for back-end development?

Of course. The best way to go http, you can do network programming, and write code is very simple, 1 functions can be built on a server side. Stream_socket_server ()
Multithreading is not a good idea, you can use PHP libevent extension, asynchronous high concurrency. PHP also has a lot of network expansion packs.
PHP is highly developed,. NET Java, you are rich enough to use.

PHP libevent extension Installation and application

Libevent is an event-driven, high-performance network library. Supports multiple I/O multiplexing Technologies, Epoll, poll, Dev/poll, select, and Kqueue, support for events such as I/O, timers and Signals, and register event priorities.
PHP libevent Extension Installation:
The libevent extension relies on the original Libevent library, and the Libevent library must be installed first.
(1) Installing the Libevent Library

  1. wget http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.20-stable.tar.gz
  2. Tar zxvf libevent-2.0. -stable. Tar. GZ
  3. CD libevent-2.0. -Stable/
  4. ./Configure --prefix=/usr/local/libevent-2.0. /
  5. Make
  6. Make install


(2) Installing the libevent extension (http://pecl.php.net/package/libevent)

  1. wget http://pecl.php.net/get/libevent-0.1.0.tgz
  2. Tar -zxvf libevent-0.1. 0.tgz
  3. CD libevent-0.1. 0
  4. ./configure --with-php-config= /usr/local/php54/< Span class= "PLN" >bin/php-config --with-libevent= /usr/local/libevent- 2.0. 20
  5. Make && make install
  6. #php. ini Add extension=libevent.so


Introduction to PHP libevent Extensions:
(1) Constants

(2) function

  • Event_base_free () frees the resource, which cannot destroy the binding event
  • Event_base_loop () Handles events and handles event loops based on the specified base
  • Event_base_loopbreak () immediately cancels the event loop, with the same behavior for each break statement
  • Event_base_loopexit () exits the loop after a specified time
  • Event_base_new () creation and initial events
  • Event_base_priority_init () Sets the priority of the event
  • Event_base_set () Associating events to event base
  • Event_buffer_base_set () Associating cached events to Event_base
  • Event_buffer_disable () disabling a cached event
  • Event_buffer_enable () enable a specified cached event
  • Event_buffer_fd_set () Change the file system description of a cache
  • Event_buffer_free () releasing cache events
  • Event_buffer_new () to create a new cache event
  • Event_buffer_priority_set () Priority setting for cache events
  • Event_buffer_read () reading data from a cache event
  • Event_buffer_set_callback () Sets or resets the callback Hansh function to the cached event
  • Event_buffer_timeout_set () sets the read and write time to timeout for a cached event
  • Event_buffer_watermark_set setting watermark flags for read and write events
  • Event_buffer_write () writes data to the cache event
  • Event_add () Adds an execution event to the specified settings
  • Event_del () Removing events from the set events
  • Event_free () empty event handle
  • Event_new () Create a new event
  • Event_set () Prepare to add an event in Event_add


PHP libevent Extensions use:
Example 1:5s after triggering callback

  1. $base = event_base_new();
  2. $event = event_new();
  3. Event_set($event, 0, ev_timeout, function() {
  4. echo "function called";
  5. });
  6. Event_base_set($event, $base);
  7. Event_add($event, 5000000);
  8. Event_base_loop($base);


Example 2: printing an input stream

  1. function print_line($fd, $events, $arg)
  2. {
  3. static $max _requests = 0;
  4. $max _requests+ +;
  5. if ($max _requests = = ) {
  6. //Exit loop after ten writes
  7. Event_base_loopexit($arg[1]);
  8. }
  9. echo fgets($fd);
  10. }
  11. Create Base and Event
  12. $base = event_base_new();
  13. $event = event_new();
  14. $FD = STDIN;
  15. Set Event Flags
  16. Event_set($event, $fd, ev_read | Ev_persist, ' Print_line ', array($event, $base));
  17. Set Event Base
  18. Event_base_set($event, $base);
  19. Enable Event
  20. Event_add($event);
  21. Start Event Loop
  22. Event_base_loop($base);


Example 3: Implementing a simple Web server
After the CLI executes, open the browser 2000 port and try again.

  1. $socket = stream_socket_server (' tcp://0.0.0.0:2000 ', $errno, $errstr) ;
  2. Stream_set_blocking($socket, 0);
  3. $base = event_base_new();
  4. $event = event_new();
  5. Event_set($event, $socket, ev_read | Ev_persist, ' ev_accept ', $base);
  6. Event_base_set($event, $base);
  7. Event_add($event);
  8. Event_base_loop($base);
  9. function ev_accept($socket, $flag, $base)
  10. {
  11. $connection = stream_socket_accept($socket);
  12. Stream_set_blocking($connection, 0);
  13. $buffer = event_buffer_new($connection, ' Ev_read ', NULL, ' ev_ Error ', $connection);
  14. Event_buffer_base_set($buffer, $base);
  15. Event_buffer_timeout_set($buffer, + );
  16. Event_buffer_watermark_set($buffer, ev_read, 0, 0xffffff);
  17. Event_buffer_priority_set($buffer, ten);
  18. Event_buffer_enable($buffer, ev_read | Ev_persist);
  19. $GLOBALS[' _ '] = $buffer; //This buffer must be assigned to a global variable that seems to be a bug in the process of passing the value or a 5.3.8 closure or a problem?
  20. }
  21. function ev_error($buffer, $error, $connection)
  22. {
  23. Event_buffer_disable($buffer, ev_read | Ev_write);
  24. Event_buffer_free($buffer);
  25. Fclose($connection);
  26. }
  27. function ev_read($buffer, $connection)
  28. {
  29. while ($read = event_buffer_read($buffer, up )) {
  30. }
  31. Fwrite($connection , date(' y-m-d h:i:s '));
  32. Ev_error($buffer , " , $connection);
  33. }


Resources:

      • http://blog.csdn.net/laoyi_grace/article/details/6539244
      • Http://www.ibm.com/developerworks/cn/aix/library/au-libev/index.html
      • http://blog.csdn.net/shagoo/article/details/6396089

"Go" web games can be used in PHP for backend development? PHP libevent extension Installation and application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.