php-fpm - 如何用php實現websocket?

來源:互聯網
上載者:User
php-fpm能實現websocket嗎?目前貌似還沒看到這種實現,websocket貌似需要長串連,但php過一會就自己斷掉了。我的理解正確嗎?目前好像只有node.js,java等等這種常駐記憶體的服務端語言才能實現,php有可能實現嗎?

回複內容:

php-fpm能實現websocket嗎?目前貌似還沒看到這種實現,websocket貌似需要長串連,但php過一會就自己斷掉了。我的理解正確嗎?目前好像只有node.js,java等等這種常駐記憶體的服務端語言才能實現,php有可能實現嗎?

php-fpm無法支援websocket協議。php也可以常駐記憶體,php版本的websocket server推薦用workerman,文檔比較全,使用簡單也非常穩定,也是常駐記憶體的,效能也比較高https://github.com/walkor/workerman

也可以用swoole.com, 是一個擴充,支援的特性多一些,使用起來也複雜一些,比較適合有點c基礎的coder

PHP 可以通過 PHP CLI 常駐記憶體… http://www.cnblogs.com/hustskyking/p/websocket-with-php.html

php有可用的websocket庫,不需要php-fpm。

目前比較成熟的有swoole(swoole.com),和workman(workman.net)

swoole是c寫的php擴充。 效率比nodejs還要高
workman是純php實現。

兩者都號稱可以實現並發百萬TCP串連,我還沒研究到這步。
目前我還只是將swoole用在了手遊的線上同步戰鬥系統,過程比較順利。

這個要通過cmd啟動並執行 具體帶的參數有點忘記了$value){            if($key == 'close'){                disconnect($socket);                console('close success');                return 'close success';            }else if($key == 'msg'){                console($value."\n");                return $value;            }        }    }    //擷取頭部資訊     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_create() 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,20)                                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\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\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 .= ' is ok';        $len = strlen($msg);        $frame[1] = $len<16?"0".dechex($len):dechex($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是返回字串第一個字元的ascii值            //dechex把十進位轉換為十六進位            $msg .= dechex(ord($data{$i}));        }        return $msg;    }    function disconnect($socket){        global $is_shaked, $is_closed;        $is_shaked = false;        $is_closed = true;        socket_close($socket);    }?>
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.