PHP Socket Implementation WebSocket (a) Basic function introduction

Source: Internet
Author: User

WebSocket protocol is a new protocol for HTML5. It implements browser-to-server full-duplex communication (Full-duplex).

The first handshake needs to be done with an HTTP request.

The WebSocket is implemented based on TCP:

Wikipedia:https://en.wikipedia.org/wiki/WebSocket

https://html.spec.whatwg.org/multipage/comms.html#network :

The server side can refer to the PHP socket function to implement

http://php.net/manual/en/book.sockets.php

Basic Concept Understanding

in network programming, we often see synchronous (Sync)/async (Async), blocking (block)/non-blocking (Unblock) four call modes:
Sync:
The so-called synchronization is that when a function call is made, the call does not return until the result is obtained. that is, one thing must be done, so that the next thing is done before you can do it.

b/s Mode (synchronous): Submit Request -> waiting for server processing -> processing finished return   This period the client browser cannot do anything

Asynchronous:
Asynchronous concepts and synchronization are relative. When an asynchronous procedure call is made, the caller cannot get the result immediately. The part that actually handles the call notifies the caller via status, notification, and callback after completion.

     such as Ajax requests ( async) :  < Span style= "font-family: ' Times New Roman ';" >-> -> finish processing

Blocking
A blocking call means that the current thread is suspended until the call results are returned (the thread goes into a non-executable state where the CPU does not allocate a time slice to the thread, that is, the thread pauses). Functions are returned only after the result is obtained.

someone might equate blocking calls with synchronous calls, and in fact he is different. For the sameAt a pace, many times the current thread is still active, but logically the current function does not return. For example, we are in SOcketCall R inECVfunction, if there are no numbers in the bufferThis function will wait until there is data to return. At this point, the current thread will continue to process a wide variety of messages.

non-blocking
The concept of non-blocking and blocking corresponds to a function that does not block the current thread and returns immediately until the result is not immediately available.
blocking mode and blocking function calls for objects
whether the object is in blocking mode and if the function is not a blocking call has a strong correlation, but not one by one corresponds. Blocking objects can have non-blocking call methods, we can pass a certainAPIgo to poll likeThe blocking function at the appropriate time to avoid blocking. For non-blocking objects, calling a special function can also enter a blocking call. functionSelectis an example of this.

1. Synchronization, That's what I call a feature that doesn't end before I death the result.
2. Asynchronous, is I invoke a feature that does not need to know the result of the feature, notify me when the feature has results (callback notification)
3. Blocking,       Just call me (function), I (function) I will not return until I have finished receiving data or have not obtained the result.
4. Non-blocking,   is calling me , I immediately returns, notify callers by select

The difference between synchronous IO and asynchronous IO is that the process is blocked when the data is copied!

the difference between blocking IO and non-blocking IO is that: whether the application's call returns immediately!

For a simple C/s mode:

Sync:Submit request, wait for server processing, processing completed returns this period the client browser cannot do anything
Async:The request is processed by the event trigger, server processing (this is what the browser can still do).both synchronous and asynchronous are only for native sockets.

Synchronous and asynchronous, blocking and non-blocking, some mixing, in fact, they are completely not the same, and they modify the object is not the same.
Blocking and non-blocking refers to whether the process needs to wait when the data accessed by the process is not ready, simply saying that this is equivalent to the implementation difference within the function , that is, when it is not ready to return directly or wait for the ready;

While synchronous and asynchronous refers to the mechanism of accessing data , synchronization generally refers to the active request and wait for the completion of the I/O operation, when the data is ready to read and write must block (difference between ready and read and write two stages, synchronous read and write must block), asynchronous refers to the active request data can continue to process other tasks , then waits for I/O, the notification of the operation, which allows the process to read and write data without blocking. (Wait for "notification")

Specific reference:

http://284772894.iteye.com/blog/1852265

PHP Socket Implementation WebSocket (a) Basic function introduction

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.