Html5-the essence is the websocket client. how does phpWebsocket receive data?

Source: Internet
Author: User
{Code...} can send an array to the server. how can I receive data?
class WebsocketClient{    private $_Socket = null;    public function __construct($host, $port)    {        $this->_connect($host, $port);    }    public function __destruct()    {        $this->_disconnect();    }    public function sendData($data)    {        // send actual data:        return fwrite($this->_Socket, $this->encode($data)) or die('Error:' . $errno . ':' . $errstr);        $wsData = fread($this->_Socket, 2000);        $retData = trim($wsData, chr(0) . chr(255));        return $retData;    }    private function encode($data)    {        $data = is_array($data) || is_object($data) ? json_encode($data) : (string)$data;        $len = strlen($data);        $mask = array();        for ($j = 0; $j < 4; $j ++)        {            $mask[] = mt_rand(1, 255);        }        $head[0] = 129;        if ($len <= 125)        {            $head[1] = $len;        } elseif ($len <= 65535)        {            $split = str_split(sprintf('%016b', $len), 8);            $head[1] = 126;            $head[2] = bindec($split[0]);            $head[3] = bindec($split[1]);        } else        {            $split = str_split(sprintf('%064b', $len), 8);            $head[1] = 127;            for ($i = 0; $i < 8; $i ++)            {                $head[$i + 2] = bindec($split[$i]);            }            if ($head[2] > 127)            {                return false;            }        }        $head[1] += 128;        $head = array_merge($head, $mask);        foreach ($head as $k => $v)        {            $head[$k] = chr($v);        }        $mask_data = '';        for ($j = 0; $j < $len; $j ++)        {            $mask_data .= chr(ord($data[$j]) ^ $mask[$j % 4]);        }        return implode('', $head) . $mask_data;    }    private function _connect($host, $port)    {        $key1 = $this->_generateRandomString(32);        $key2 = $this->_generateRandomString(32);        $key3 = $this->_generateRandomString(8, false, true);        $header = "GET ws://" . $host . ":" . $port . "/ HTTP/1.1\r\n";        $header .= "Host: " . $host . ":" . $port . "\r\n";        $header .= "Connection: Upgrade\r\n";        $header .= "Pragma: no-cache\r\n";        $header .= "Cache-Control: no-cache\r\n";        $header .= "Upgrade: websocket\r\n";        $header .= "Sec-WebSocket-Version: 13\r\n";        $header .= "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36\r\n";        $header .= "Accept-Encoding: gzip, deflate, sdch\r\n";        $header .= "Accept-Language: zh-CN,zh;q=0.8\r\n";        $header .= "Sec-WebSocket-Key: " . $key1 . "\r\n";        $header .= "Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n";        $header .= "\r\n";        $this->_Socket = fsockopen($host, $port, $errno, $errstr, 2);        fwrite($this->_Socket, $header) or die('Error: ' . $errno . ':' . $errstr);        $response = fread($this->_Socket, 2000);        /**         * @todo: check response here. Currently not implemented cause "2 key handshake" is already deprecated.         * See: http://en.wikipedia.org/wiki/WebSocket#WebSocket_Protocol_Handshake         */        return true;    }    private function _disconnect()    {        fclose($this->_Socket);    }    private function _generateRandomString($length = 10, $addSpaces = true, $addNumbers = true)    {        $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"§$%&/()=[]{}';        $useChars = array();        // select some random chars:        for ($i = 0; $i < $length; $i ++)        {            $useChars[] = $characters[mt_rand(0, strlen($characters) - 1)];        }        // add spaces and numbers:        if ($addSpaces === true)        {            array_push($useChars, ' ', ' ', ' ', ' ', ' ', ' ');        }        if ($addNumbers === true)        {            array_push($useChars, rand(0, 9), rand(0, 9), rand(0, 9));        }        shuffle($useChars);        $randomString = trim(implode('', $useChars));        $randomString = substr($randomString, 0, $length);        return $randomString;    }}

How can I receive data when I can send an array to the server?

Reply content:
class WebsocketClient{    private $_Socket = null;    public function __construct($host, $port)    {        $this->_connect($host, $port);    }    public function __destruct()    {        $this->_disconnect();    }    public function sendData($data)    {        // send actual data:        return fwrite($this->_Socket, $this->encode($data)) or die('Error:' . $errno . ':' . $errstr);        $wsData = fread($this->_Socket, 2000);        $retData = trim($wsData, chr(0) . chr(255));        return $retData;    }    private function encode($data)    {        $data = is_array($data) || is_object($data) ? json_encode($data) : (string)$data;        $len = strlen($data);        $mask = array();        for ($j = 0; $j < 4; $j ++)        {            $mask[] = mt_rand(1, 255);        }        $head[0] = 129;        if ($len <= 125)        {            $head[1] = $len;        } elseif ($len <= 65535)        {            $split = str_split(sprintf('%016b', $len), 8);            $head[1] = 126;            $head[2] = bindec($split[0]);            $head[3] = bindec($split[1]);        } else        {            $split = str_split(sprintf('%064b', $len), 8);            $head[1] = 127;            for ($i = 0; $i < 8; $i ++)            {                $head[$i + 2] = bindec($split[$i]);            }            if ($head[2] > 127)            {                return false;            }        }        $head[1] += 128;        $head = array_merge($head, $mask);        foreach ($head as $k => $v)        {            $head[$k] = chr($v);        }        $mask_data = '';        for ($j = 0; $j < $len; $j ++)        {            $mask_data .= chr(ord($data[$j]) ^ $mask[$j % 4]);        }        return implode('', $head) . $mask_data;    }    private function _connect($host, $port)    {        $key1 = $this->_generateRandomString(32);        $key2 = $this->_generateRandomString(32);        $key3 = $this->_generateRandomString(8, false, true);        $header = "GET ws://" . $host . ":" . $port . "/ HTTP/1.1\r\n";        $header .= "Host: " . $host . ":" . $port . "\r\n";        $header .= "Connection: Upgrade\r\n";        $header .= "Pragma: no-cache\r\n";        $header .= "Cache-Control: no-cache\r\n";        $header .= "Upgrade: websocket\r\n";        $header .= "Sec-WebSocket-Version: 13\r\n";        $header .= "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36\r\n";        $header .= "Accept-Encoding: gzip, deflate, sdch\r\n";        $header .= "Accept-Language: zh-CN,zh;q=0.8\r\n";        $header .= "Sec-WebSocket-Key: " . $key1 . "\r\n";        $header .= "Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n";        $header .= "\r\n";        $this->_Socket = fsockopen($host, $port, $errno, $errstr, 2);        fwrite($this->_Socket, $header) or die('Error: ' . $errno . ':' . $errstr);        $response = fread($this->_Socket, 2000);        /**         * @todo: check response here. Currently not implemented cause "2 key handshake" is already deprecated.         * See: http://en.wikipedia.org/wiki/WebSocket#WebSocket_Protocol_Handshake         */        return true;    }    private function _disconnect()    {        fclose($this->_Socket);    }    private function _generateRandomString($length = 10, $addSpaces = true, $addNumbers = true)    {        $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"§$%&/()=[]{}';        $useChars = array();        // select some random chars:        for ($i = 0; $i < $length; $i ++)        {            $useChars[] = $characters[mt_rand(0, strlen($characters) - 1)];        }        // add spaces and numbers:        if ($addSpaces === true)        {            array_push($useChars, ' ', ' ', ' ', ' ', ' ', ' ');        }        if ($addNumbers === true)        {            array_push($useChars, rand(0, 9), rand(0, 9), rand(0, 9));        }        shuffle($useChars);        $randomString = trim(implode('', $useChars));        $randomString = substr($randomString, 0, $length);        return $randomString;    }}

How can I receive data when I can send an array to the server?

Http://www.workerman.net/workerman-chat
Let's take a look at this. the websocket chat room written in PHP, the customer service function can actually be implemented using the single chat function in it. someone has used it to implement customer service, including web pages and client software.
Development Manual: http://workerman.net/gatewaydoc/
Demo: http://chat.workerman.net/

There are enough php websocket tutorials on Baidu. you can check them out.

Socket_read

Thanks for your answers.

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.