This article mainly introduces the use of Swoole to create a server (below), has a certain reference value, now share to everyone, the need for friends can refer to
1. Create a Httpserver server
Httpserver is based on Swoole_server, so the method Httpserver under Swoole_server can be used
Httpsevrer It can only be recalled by the client
1. Testing
<?php$http = new Swoole_http_server ("0.0.0.0", 8811), $http->on (' request ', function ($request, $response) { Echo ' Nihao '; $response->end (' Hello Swoole ');}); $http->start ();
PHP execution, open the browser
Server
Here we find a strange elephant.
1.echo (VAR_DUMP,PRINT_RD) content in the server reality
2. The content of the browser can only be accessed through the end (content)
End can only be called once, and if you need to send data to the client multiple times, use the Write method
Extension: Gee this, very much like our usual visit to the xxx.com:80 if we want to access the file below what to do?
Do you think of a function set that we had before TCP and UDP, which I understood as a configuration, then we godless it to configure
$http->set ([ ' enable_static_handler ' = = True, ' document_root ' = '/www/wwwroot/server ',// Set the root directory here according to your own path to write ]);
Isn't it much like configuring virtual addresses? We try to access the test.html under the server directory
Got it!!
In general, the document_root http_server process is set up like this:
1. Go to the URL to access the document_root relative path of the file, with the configuration domain name access to the file principle is similar
2. If this file is not, then we will http_server follow the execution $http->on (' request ' function ($request, $response)) and make corresponding
2. Create a WebSocket server
1. Features:
(1) HTTP protocol has a flaw: communication can only be initiated by the client, do not make the server actively push information to the client.
And websocket its biggest feature is that the server can proactively push information to the client, the client can also actively send information to the server, is a true two-way equality dialogue, belongs to the server push technology.
(2) Web_socket Inheritance Http_server
Due to the Web_server full duplex feature, it's a good place to be a chat room.
2. Speaking of which, let's do a simple test!
(1) We'll write a web_server.php file first.
$server = new Swoole_websocket_server ("0.0.0.0", 8811),//web_socket inherits Http_server, so it can also have the same set method//Including $server On (' Request ', function ($request, $response) {} is also available $server->set ( [ ' enable_static_handler ' = = True, ' document_root ' = "/www/wwwroot/server", ]); /Listen WebSocket connection Open event $server->on (' Open ', ' onOpen '), function OnOpen ($server, $request) { echo "FD is:". $request- >fd. " Already online \ n ";} Listens for WS message Event $server->on (' Message ', function ($server, $frame) { var_dump ($frame); $msg = ' fd is '. $frame->fd. ' said: ' $frame->data;//$frame->data The information passed to the client $server->push ($frame->fd, $msg);}); $server->on (' Close ', function ($server, $FD) { echo "client {$FD} closed\n";}); $server->start ();
(2) Then write a client file
<script type= "Text/javascript" > var url= ' ws://Your IP address: 8811 '; var ws =new WebSocket (URL); Ws.onopen=function (evt) { ws.send (' Client: Establish link success '); Console.log (EVT); } Ws.onmessage=function (evt) { console.log (evt); Console.log (' Server reply: ' +evt.data); } Ws.onclose=function (evt) { console.log (evt); } Ws.onerror=function (evt) { console.log (evt); } </script>
You can write a chat room based on this web_socket, which is very interesting.
Learning the Tcp,udp,websocket,http service, we can do a wave of summary:
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!