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
- wget http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.20-stable.tar.gz
- Tar zxvf libevent-2.0. -stable. Tar. GZ
- CD libevent-2.0. -Stable/
- ./Configure --prefix=/usr/local/libevent-2.0. /
- Make
- Make install
(2) Installing the libevent extension (http://pecl.php.net/package/libevent)
- wget http://pecl.php.net/get/libevent-0.1.0.tgz
- Tar -zxvf libevent-0.1. 0.tgz
- CD libevent-0.1. 0
- ./configure --with-php-config= /usr/local/php54/< Span class= "PLN" >bin/php-config --with-libevent= /usr/local/libevent- 2.0. 20
- Make && make install
- #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
- $base = event_base_new();
- $event = event_new();
- Event_set($event, 0, ev_timeout, function() {
- echo "function called";
- });
- Event_base_set($event, $base);
- Event_add($event, 5000000);
- Event_base_loop($base);
Example 2: printing an input stream
- function print_line($fd, $events, $arg)
- {
- static $max _requests = 0;
- $max _requests+ +;
- if ($max _requests = = ) {
- //Exit loop after ten writes
- Event_base_loopexit($arg[1]);
- }
- echo fgets($fd);
- }
- Create Base and Event
- $base = event_base_new();
- $event = event_new();
- $FD = STDIN;
- Set Event Flags
- Event_set($event, $fd, ev_read | Ev_persist, ' Print_line ', array($event, $base));
- Set Event Base
- Event_base_set($event, $base);
- Enable Event
- Event_add($event);
- Start Event Loop
- Event_base_loop($base);
Example 3: Implementing a simple Web server
After the CLI executes, open the browser 2000 port and try again.
- $socket = stream_socket_server (' tcp://0.0.0.0:2000 ', $errno, $errstr) ;
- Stream_set_blocking($socket, 0);
- $base = event_base_new();
- $event = event_new();
- Event_set($event, $socket, ev_read | Ev_persist, ' ev_accept ', $base);
- Event_base_set($event, $base);
- Event_add($event);
- Event_base_loop($base);
- function ev_accept($socket, $flag, $base)
- {
- $connection = stream_socket_accept($socket);
- Stream_set_blocking($connection, 0);
- $buffer = event_buffer_new($connection, ' Ev_read ', NULL, ' ev_ Error ', $connection);
- Event_buffer_base_set($buffer, $base);
- Event_buffer_timeout_set($buffer, + );
- Event_buffer_watermark_set($buffer, ev_read, 0, 0xffffff);
- Event_buffer_priority_set($buffer, ten);
- Event_buffer_enable($buffer, ev_read | Ev_persist);
- $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?
- }
- function ev_error($buffer, $error, $connection)
- {
- Event_buffer_disable($buffer, ev_read | Ev_write);
- Event_buffer_free($buffer);
- Fclose($connection);
- }
- function ev_read($buffer, $connection)
- {
- while ($read = event_buffer_read($buffer, up )) {
- }
- Fwrite($connection , date(' y-m-d h:i:s '));
- Ev_error($buffer , " , $connection);
- }
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