Wrote a websocket chat room, then finally understand the PHP socket

Source: Internet
Author: User
Tags socket blocking

Original URL: http://www.jnecw.com/p/1523

To understand the socket must first understand the difference between HTTP and TCP, simply said is a short chain, one is a long chain, one is to go to the server pull data, one is the server can actively push data.

The socket is the intermediate software abstraction layer of the application layer and the TCP/IP protocol family, it is a set of interfaces. In design mode, the socket is actually a façade mode, it is the complex TCP/IP protocol family hidden behind the socket interface, for the user, a set of simple interface is all, let the socket to organize data to meet the specified protocol. -From the network.

So how to use PHP+JS to do server push?

Client

The client is very simple, take advantage of the modern browser WebSocket API, described here very detailed: http://msdn.microsoft.com/zh-cn/library/ie/hh673567

Core code:

JAVASCRIPT
12345
var wsserver = ' ws://127.0.0.1:8080 '; var ws = new WebSocket (wsserver), ws.onmessage = function (evt) {do     sth};

The first two rows send a handshake request to the specified server, and if the server returns a legitimate HTTP header, the handshake succeeds, and then the message sent by the server is processed by listening for the OnMessage event. There are many other events to listen to, see the previous URL.

Server ideas

The difficulty is the server, no Apache and nginx these HTTP servers in front of the top, only with PHP how to write?

Here's a tutorial that's very deep http://blog.csdn.net/shagoo/article/details/6396089

Before writing, make a smooth thought:

1 Listening: First to suspend a process to listen for requests from the client
2 handshake: For the first legitimate request, send a valid header back
3 Keep the connection: a new message will be broadcast on the radio. Until the client disconnects
4 Accept another request, repeat 2 and 3

The key code is as follows:

PHP
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 6667686970717273
Public Function Start_server () {$this->socket = socket_create (Af_inet, Sock_stream, sol_tcp);     Allows use of local address socket_set_option ($this->socket, Sol_socket, SO_REUSEADDR, TRUE);    Socket_bind ($this->socket, $this->host, $this->port);     Up to 10 people connected, exceeding the client connection will return wsaeconnrefused error Socket_listen ($this->socket, $this->maxuser);        while (TRUE) {$this->cycle = $this->accept;        $this->cycle[] = $this->socket;        Blocking, Socket_select ($this->cycle, $write, $except, NULL) when there is a new connection; foreach ($this->cycle as $k = + $v) {if ($v = = = $this->socket) {if ($accept = socket_a                Ccept ($v)) < 0) {continue;                }//If the request is from the listener port that socket, a new socket is created for communication $this->add_accept ($accept);            Continue            } $index = Array_search ($v, $this->accept);            if ($index = = = NULL) {continue;  }          if ([Email protected]_recv ($v, $data, 1024x768, 0) | |! $data) {//the socket with no message skips $this->close ($                V);            Continue                } if (! $this->ishand[$index]) {$this->upgrade ($v, $data, $index);                if (!empty ($this->function[' Add ')) {Call_user_func_array ($this->function[' add '), Array ($this));            } continue;            } $data = $this->decode ($data); if (!empty ($this->function[' send ')) {Call_user_func_array ($this->function[' send '], array ($data, $ind            EX, $this));    }} sleep (1);    }}//Add a user for initial connection private function add_accept ($accept) {$this->accept[] = $accept;    $index = Array_keys ($this->accept);    $index = End ($index); $this->ishand[$index] = FALSE;}    Close a connection private function close ($accept) {$index = Array_search ($accept, $this->accept);    Socket_close ($accept);unset ($this->accept[$index]);    unset ($this->ishand[$index]);    if (!empty ($this->function[' close ')) {Call_user_func_array ($this->function[' close '), Array ($this)); }}//Response Upgrade Protocol Private function Upgrade ($accept, $data, $index) {if (Preg_match ("/sec-websocket-key: (. *) \r\n/", $data, $mat CH) {$key = Base64_encode (SHA1 ($match [1].        ' 258eafa5-e914-47da-95ca-c5ab0dc85b11 ', true));                $upgrade = "http/1.1 101 switching protocol\r\n".                "Upgrade:websocket\r\n".                "Connection:upgrade\r\n". "Sec-websocket-accept:". $key.  "\r\n\r\n";        Must end with two carriage returns Socket_write ($accept, $upgrade, strlen ($upgrade));    $this->ishand[$index] = TRUE; }}

There are a few key places where a while (true) hangs the process, or the process exits after execution. The second is the use of socket_select and socket_accept functions. The third is the handshake when the client first requests it.

Socket_select

This function is the key to accepting multiple connections at the same time, and I understand it in order to block the program from continuing down and automatically selecting the currently active connection.

Socket_select ($sockets, $write = null, $except = NULL, NULL);

$sockets can be understood as an array in which the file descriptor is stored. When it changes (that is, there is a new message to or a client connected/disconnected), the Socket_select function returns and continues to execute.
$write is listening for client write data, passing in NULL is not concerned whether there is a write change.
$except is the element to be excluded from the $sockets, and passing in null is "listening" all.
The last parameter is the time-out
If 0: End immediately
If n>1: The end is at most n seconds, and if there is a new dynamic for a connection, return early
If null: If there is a new dynamic for a connection, the return

To understand, dump tests:

PHP
12345
$this->cycle = $this->accept; $this->cycle[] = $this->socket;var_dump ($this->cycle);//array (n),n> =1socket_select ($this->cycle, $write, $except, NULL);//Continue down Var_dump ($this->cycle) after activity;//array (0), n==0

This test is fully understood, before socket_select all the socket connection is thrown in to it, one of the active when it throws the connection to us. The ability to express is limited, which is probably the meaning ...

Socket_accept

This function accepts a unique parameter, the socket file (handle) that was created earlier socket_create. Returns a new resource, or false. This function notifies Socket_listen () that a connected socket resource will be passed in. Once the socket connection is successfully established, a new socket resource will be returned for communication. If there are multiple sockets in the queue, the first one will be processed first. The key is here: if there is no socket connection, then this function will wait until a new socket comes in.

If the front does not need to socket_select in the absence of the socket blocking the program, then the card will never end here.

The following process is clear, when a new client request arrives, create a resource with socket_accept and join the $this->accept connection pool. and set its flag Ishand to false, then the next loop (because $this->cycle[] = $this->socket; $this->cycle changes, so Socket_select will return) The upgrade handshake is performed. Then wait for its new message to be ready.

The program can run successfully after debugging, php5.3+websocket13.

Interested students can download: file address

Wrote a websocket chat room, then finally understand the PHP socket

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.