初识swoole【上】

2018-11-01 10:20:16   PHP

  php   swoole  

前言:都是为了生存

有收获的话请加颗小星星,没有收获的话可以 反对 没有帮助 举报三连

一、什么是swoole

Swoole:面向生产环境的 PHP 异步网络通信引擎

使 PHP 开发人员可以编写高性能的异步并发 TCP、UDP、Unix Socket、HTTP,WebSocket 服务。Swoole 可以广泛应用于互联网、移动通信、企业软件、云计算、网络游戏、物联网(IOT)、车联网、智能家居等领域。 使用 PHP + Swoole 作为网络通信框架,可以使企业 IT 研发团队的效率大大提升,更加专注于开发创新产品。

二、安装

1、pecl

  1. pecl install swoole

2、源码安装

  1. curl -O https://pecl.php.net/get/swoole-4.2.5.tgz
  2. tar -zxvf swoole-4.2.5.tgz
  3. cd swoole-4.2.5.tgz
  4. phpize
  5. ./configure
  6. make && make install
  7. # 加入到php.ini中
  8. php --ini
  9. extension=swoole.so

三、基本入门

注意:示例代码都引入了"symfony/var-dumper"包进行美化打印

Swoole的绝大部分功能只能用于cli命令行环境

1、TCP服务器

  1. <?php
  2. // new
  3. $server = new swoole_server("0.0.0.0", "9501",SWOOLE_PROCESS, SWOOLE_SOCK_TCP);
  4. // connect 连接
  5. $server->on("connect", function (swoole_server $server, $fd) {
  6. dump("{$fd}连接");
  7. $server->send($fd, "欢迎{$fd}大山驴\n");
  8. });
  9. // receive 回调
  10. $server->on("receive", function (swoole_server $server, $fd, $from_id, $data) {
  11. $server->send($fd, "服务端回复:{$data}\n");
  12. foreach ($server->connections as $connection) {
  13. if ($connection != $fd){
  14. $server->send($connection, "{$fd}说{$data}");
  15. }
  16. }
  17. });
  18. // close
  19. $server->on("close", function (swoole_server $server, $fd) {
  20. dump("{$fd}关闭");
  21. foreach ($server->connections as $connection) {
  22. if ($connection != $fd){
  23. $server->send($connection, "{$fd}断开连接");
  24. }
  25. }
  26. });
  27. // start
  28. $server->start();
  1. php tcp_server.php
  2. telnet 127.0.0.1 9501

2、UDP服务器

  1. // new
  2. <?php
  3. $server = new swoole_server("0.0.0.0", "9502", SWOOLE_PROCESS, SWOOLE_SOCK_UDP);
  4. // packet
  5. $server->on("Packet", function (swoole_server $server, $data, $clientInfo) {
  6. $server->sendto($clientInfo['address'], $clientInfo['port'], "服务器回复: {$data}");
  7. dump($clientInfo);
  8. });
  9. // start
  10. $server->start();
  1. php udp_server.php
  2. netcat -u 127.0.0.1 9502

3、Web服务器

  1. <?php
  2. // new
  3. $http = new swoole_http_server(HOST, "9503");
  4. // request
  5. $http->on("request", function (\Swoole\Http\Request $request, \Swoole\Http\Response $response) {
  6. dump($request->get, $request->post);
  7. $response->header("Content-Type", "text/html; charset=utf-8");
  8. $response->end("<h1>Hello Swoole. #" . mt_rand(1000, 9999) . "</h1>\n");
  9. });
  10. // start
  11. $http->start();
  1. php web_server.php
  2. curl -XGET "127.0.0.1:9503?id=1&name=aa&age=26"
  3. curl -XPOST "127.0.0.1:9503?id=1&name=aa&age=26" -d "love=like"

4、WebSocket服务器

服务端

  1. <?php
  2. // new
  3. $ws = new swoole_websocket_server(HOST, "9504");
  4. // open
  5. $ws->on("open", function (Swoole\WebSocket\Server $ws, \Swoole\Http\Request $request) {
  6. dump($request->fd, $request->get, $request->server);
  7. $ws->push($request->fd, "你是大山驴\n");
  8. });
  9. // message
  10. $ws->on("message", function (\Swoole\WebSocket\Server $ws, $frame) {
  11. dump("消息: {$frame->data}\n");
  12. $ws->push($frame->fd, "服务端回复: {$frame->data}\n");
  13. });
  14. // close
  15. $ws->on("close", function (Swoole\WebSocket\Server $ws, $fd) {
  16. dump("{$fd}关闭");
  17. });
  18. // start
  19. $ws->start();

客户端

  1. let ws = new WebSocket("ws://127.0.0.1:9504");
  2. ws.onopen = function (ws) {
  3. console.log("连接服务器");
  4. };
  5. ws.onclose = function (ws) {
  6. console.log("断开连接");
  7. };
  8. ws.onmessage = function (ws) {
  9. console.log('接收来自服务器的消息:' + ws.data);
  10. };
  11. ws.onerror = function (ws, event) {
  12. console.log('错误了:' + ws.data);
  13. };

5、定时器

  1. <?php
  2. // 每2秒执行一次
  3. swoole_timer_tick(2000, function ($time_id){
  4. dump($time_id);
  5. });
  6. // 3秒后执行
  7. swoole_timer_after(3000, function (){
  8. dump("这是3s后");
  9. });