Use php + swoole to update client data in real time (1). swooleclient_PHP tutorial-php Tutorial

Source: Internet
Author: User
Tags php websocket
Use php + swoole to update client data in real time (1), swooleclient. Use php + swoole to update client data in real time (1). if swooleclient wants to update a list in real time, the traditional method is polling. Taking web as an example, using Ajax to regularly update client data using php + swoole (1), swooleclient

To update a list in real time, the traditional method is polling. Take the web as an example. use Ajax to regularly request the server and then obtain the data displayed on the page. This method is simple, but the disadvantage is that it is a waste of resources.

HTTP1.1 adds support for websocket so that passive display can be changed to active notification. That is to say, the websocket is used to maintain a persistent link with the server. Once the data changes, the server notifies the client that the data has been updated, and then refresh the data. This saves a lot of unnecessary passive requests and server resources.

To implement a webscoket program, you must first use a browser that supports html5.

If (ws = null) {var wsServer = 'Ws: // '+ location. hostname + ': 8888'; ws = new WebSocket (wsServer); ws. onopen = function () {console. log ("socket connection enabled") ;}; ws. onmessage = function (e) {console. log ("message:" + e. data) ;}; ws. onclose = function () {console. log ("socket connection disconnected") ;}; ws. onerror = function (e) {console. log ("ERROR:" + e. data) ;}; // close the connection when you leave the page $ (window ). bind ('beforeunload', function () {ws. close ();});}

In this way, a client is implemented, but the process is far from over. The above code is just a simple connection, dialog, close, and other basic actions. If you want to communicate with the server, you must have a more specific solution. For example, when a message is received, the system determines the type for further operations.

Server: Swoole is used for websocket development on the php Server. it is very simple to use swoole for websocket development on the php server, and it also supports httpserver.

$server = new swoole_websocket_server("0.0.0.0", 8888);$server->on('open', function (swoole_websocket_server $server, $request) {echo "server: handshake success with fd{$request->fd}\n";});$server->on('message', function (swoole_websocket_server $server, $frame) {echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";$server->push($frame->fd, "this is server");});$server->on('close', function ($ser, $fd) {echo "client {$fd} closed\n";});$server->start();

Swoole is an extension of php. for the installation method, refer to: install swoole extension in php

This article is written here first, and the next article will write more specific operations. if you are interested, please continue to pay attention to this site. Thank you!

Articles you may be interested in:
  • Example of using swoole to expand php websocket
  • Analysis of the Timer features of the PHP Swoole Timer
  • Example of php asynchronous multi-thread swoole usage
  • How to install swoole extension in php
  • Swoole-1.7.22 version released to fix PHP7 problems

Round (1). if swooleclient wants to update a list in real time, the traditional method is round robin. Taking web as an example, using Ajax timing...

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.