PHP-FPM-How to implement WebSocket in PHP?

Source: Internet
Author: User
Tags php cli
Can php-fpm achieve websocket? At present it seems to have not seen this realization, websocket seemingly need a long connection, but PHP after a while it broke down. Is my understanding correct? At present, it seems that only Node.js,java and so on the resident memory of the service-side language can be implemented, PHP is possible to implement it?

Reply content:

Can php-fpm achieve websocket? At present it seems to have not seen this realization, websocket seemingly need a long connection, but PHP after a while it broke down. Is my understanding correct? At present, it seems that only Node.js,java and so on the resident memory of the service-side language can be implemented, PHP is possible to implement it?

PHP-FPM cannot support WebSocket protocol. PHP can also be resident memory, PHP version of the WebSocket server recommended Workerman, document comparison, the use of simple is very stable, is also resident memory, performance is relatively high https://github.com/walkor/workerman

can also use swoole.com, is an extension, support more features, use more complex, more suitable for a little C-based coder

PHP can reside in memory via PHP CLI ... http://www.cnblogs.com/hustskyking/p/websocket-with-php.html

PHP has a websocket library available and does not need to be php-fpm.

At present the more mature have swoole (swoole.com), and Workman (Workman.net)

Swoole is a C-written PHP extension. Efficiency is higher than nodejs.
Workman is a pure PHP implementation.

Both claim to be capable of implementing millions of TCP connections, and I have not yet studied this step.
At present I just use swoole in the hand-tour of the online synchronous combat system, the process is relatively smooth.

This is going to run through the cmd specific parameters a little forgot
  $value) {if ($key = = ' close ') {disconnect ($socket);                Console (' close success ');            Return ' close success '; }else if ($key = = ' msg ') {Console ($value. ")                \ n ");            return $value;        }}}//Get header information function getheaders ($req) {$r = $h = $o =null;        if (Preg_match ("/get (. *) http/", $req, $match)) {$r = $match [1];}        if (Preg_match ("/host: (. *) \r\n/", $req, $match)) {$h = $match [1];}        if (Preg_match ("/origin: (. *) \r\n/", $req, $match)) {$o = $match [1];}        if (Preg_match ("/sec-websocket-key: (. *) \r\n/", $req, $match)) {$key = $match [1];} if (Preg_match ("/\r\n (. *?)        \$/", $req, $match)) {$data = $match [1];}    Return Array ($r, $h, $o, $key, $data); } function WebSocket ($address, $port) {$master =socket_create (af_inet, Sock_stream, sol_tcp) or Die ("SOCKET_CR        Eate () failed "); Socket_set_option ($master, Sol_socket, SO_REUSEADDR, 1) or Die ("socket_option ()Failed ");        Socket_bind ($master, $address, $port) or Die ("Socket_bind () failed");        Socket_listen ($master,) or Die ("Socket_listen () failed"); echo "Server Started:". Date (' y-m-d h:i:s '). "        \ n "; echo "Master socket:". $master. "        \ n "; echo "Listening on:". $address. "Port". $port. "        \ n ";    return $master;        } function Dohandshake ($buffer) {list ($resource, $host, $origin, $key, $data) = Getheaders ($buffer);        echo "Resource is $resource \ n";        Echo "Origin is $origin \ n";        echo "host is $host \ n";        echo "key is $key \ n";        $response _key = Base64_encode (SHA1 ($key. ' 258eafa5-e914-47da-95ca-c5ab0dc85b11 ', true));                    $return _str = "http/1.1 101 switching protocols\r\n".                    "Upgrade:websocket\r\n".                    "Connection:upgrade\r\n".        "Sec-websocket-accept: $response _key\r\n\r\n";    return $return _str; } function CoNsole ($msg) {$msg = TRANSTOGBK ($msg);        echo "$msg \ n";    return $msg;        } function decode ($msg = "") {$mask = array ();        $data = "";        $msg = Unpack ("h*", $msg);        $head = substr ($msg [1],0,2);        if (Hexdec ($head {1}) = = = 8) {$data = false;            } else if (Hexdec ($head {1}) = = = 1) {$mask [] = Hexdec (substr ($msg [1],4,2));            $mask [] = Hexdec (substr ($msg [1],6,2));            $mask [] = Hexdec (substr ($msg [1],8,2));            $mask [] = Hexdec (substr ($msg [1],10,2));            $s = 12;            $e = strlen ($msg [1])-2;            $n = 0;                for ($i = $s; $i <= $e; $i + = 2) {$data. = Chr ($mask [$n%4]^hexdec (substr ($msg [1], $i, 2)));            $n + +;    }} return $data;        } function encode ($msg = "") {$frame = array ();        $frame [0] = "81";        $msg. = ' OK ';        $len = strlen ($msg); $frame [1] = $len <16? "     0 ". Dechex ($len):d Echex ($len);   $frame [2] = Ord_hex ($msg);        $data = Implode ("", $frame);    Return Pack ("h*", $data);        } function Transtogbk ($s) {//utf8->gbk//echo $s;        Return Iconv ("UTF-8", "GBK", $s);    return $s;        } function Ord_hex ($data) {$msg = "";        $l = strlen ($data); for ($i =0; $i < $l; $i + +) {//ord is the ASCII value that returns the first character of a string//dechex converts decimal to a hexadecimal $msg. = Dechex (or        D ($data {$i}));    } return $msg;        } function Disconnect ($socket) {global $is _shaked, $is _closed;        $is _shaked = false;        $is _closed = true;    Socket_close ($socket); }?>
  • Related Article

    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.