_php instance of PHP and WebSocket chat room based on Swoole

Source: Internet
Author: User
Tags http request php language learn php message queue

WebSocket

WebSocket is just a network communication protocol.

Like HTTP, FTP and so on are network communication protocols; Don't think much of it;

WebSocket is a protocol for persistent network communication, as opposed to HTTP, a non-persistent protocol;

The relationship between WebSocket and HTTP

There are intersections, but not all of them.

WebSocket just borrowed a part of the HTTP protocol to complete a handshake. (Three handshake of HTTP, only once done here)

HTTP and WebSocket request header comparisons:


HTTP:

The original time, the client through HTTP (horseback) with a letter request server, server processing request (write back), again through the HTTP (horseback) return;

WebSocket:

The client requests the server with a letter via HTTP (horseback riding), but at the same time, carrying Upgrade:websocket and Connection:Upgrade (two tubes), if the server supports the WebSocket protocol (with two pipe interfaces), uses the WebSocket protocol to return the available information (discarded horses), and thereafter the information is transmitted, Use these two pipes unless one of them cuts the pipe, and if the server does not support, the client requests the link to fail, returning an error message;

HTTP and WebSocket response header comparison:


The difference between WebSocket and Ajax polling, long poll

The first is Ajax polling, the principle of Ajax polling is very simple, let the browser send a request every few seconds, asking the server if there is new information

Scene Reappearance:

Client: La La la, no new information (Request)

Service side: No (Response)

Client: La La la, no new information (Request)

Service side: No. (Response)

Client: La La la, no new information (Request)

Service side: You are annoying ah, no ah ... (Response)

Client: La La la, there is no new information (Request)

Service side: All right, here you are. (Response)

Client: La La la, there is no new information (Request)

Service side: ... Didn't.... Didn't.. No

Long poll In fact, the principle is similar to the Ajax Polling, is the use of polling, not discussed;

As can be seen from the above, polling is in fact constantly set up an HTTP connection, and then wait for the service side processing, can reflect the HTTP protocol another feature, passivity . At the same time, after each request and response of HTTP, the server will discard the client information, the next request, must carry identity information (cookies), no State ;

The appearance of the websocket, neatly solved these problems;

So the above scenario can be modified as follows.

Client: La la la, I want to establish websocket protocol, required services:Chat,websocket Protocol version: (HTTP Request)

Service side: OK, confirm, upgraded to WebSocket protocol (HTTP protocols switched)

Client: Please send me a message when you have information oh.

Service side: OK, sometimes I will tell you.

Client: Balab start bucket map Alabala

Service side: Cang-well empty ala

Client: nosebleed, I rub ...

Service End: Hahabur education Oh, haha!

Service side: I'm laughing.

Swoole

However, in order to use PHP with HTML5 to complete a websocket request and response, brother traveled mountains and mountains, deep in the jungle, found Swoole:

PHP language asynchronous, parallel, high-performance network communication framework, using pure C language, provides the PHP language of the asynchronous multi-threaded server, asynchronous TCP/UDP network client, asynchronous MySQL, database connection pool, Asynctask, message queue, millisecond timer, asynchronous file read and write, An asynchronous DNS query.

Supported Services:

Httpserver

WebSocket Server

TCP Server

TCP Client

Async-io (asynchronous)

Task (timed Task)

Environmental dependency:

Supports only linux,freebsd,macos,3 class operating systems

Linux kernel version 2.3.32 above

PHP5.3.10 above version

gcc4.4 version or Clang

cmake2.4+, you need to use CMake when compiling as libswoole.so as A/C + + library

Installation:

You must ensure that the following software is available in the system:

Php-5.3.10 or later

gcc-4.4 or later

Make

Autoconf

Swoole is run as a php extension

Install (Root permissions):

CD Swoole

Phpize

./configure

Make

sudo make install

Configure PHP.ini

Extension=swoole.so

Want to study swoole students, to see the manual (although not written well, but still can read)

Make a chat room

Server side: socket.php

Create WebSocket server object, listen for 0.0.0.0:9502 port
$ws = new Swoole_websocket_server ("0.0.0.0", 9502);

Listen for WebSocket connection Open event
$ws->on (' Open ', function ($ws, $request) {
  $FD [] = $request->fd;
  $GLOBALS [' fd '] = $FD;
  $ws->push ($request->fd, "Hello, welcome\n");
};

Listens for WebSocket message events
$ws->on (' Messages ', function ($ws, $frame) {
  $msg = ' from '. $frame->fd. ': {$frame- >data}\n ";
Var_dump ($GLOBALS [' FD ']);
Exit;
  foreach ($GLOBALS [' FD '] as $aa) {
    foreach ($aa as $i) {
      $ws->push ($i, $msg);
    }
  }
  $ws->push ($frame->fd, "server: {$frame->data}");
  $ws->push ($frame->fd, "server: {$frame->data}");

Listens for WebSocket connection shutdown events
$ws->on (' Close ', function ($ws, $FD) {
  echo ' client-{$fd} ' closed\n ';
});

$ws->start ();

client: socket.html

<! DOCTYPE html>  

The above is based on swoole implementation of PHP and WebSocket chat room full content, I believe this article for everyone to learn PHP and WebSocket and development chat room is very helpful.

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.