├── CommentClient.php ├── CommentServer.php ├── CommentServer2.php ├── NoReload.php ├── README.md ├── Reload.php ├── Test.php ├── WebSocketServer.php ├── WebSocketServerValid.php ├── client-first.php ├── client-pack.php ├── client.php ├── commentClient.html ├── index.html ├── server-eof-check.php ├── server-eof-client.php ├── server-eof-split.php ├── server-first.php ├── server-http.php ├── server-pack.php ├── server-task-first.php ├── server.log ├── tcp-buffer-client.php ├── tcp-buffer-server.php ├── tick-2.php ├── tick-after.php ├── tick-server.php ├── tick.php ├── websocket-client-faild.html └── websocket-client-success.html /CommentClient.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | Document 9 | 10 | 11 | 16 | 17 |
18 | 发送内容:
19 | 发送给谁:
20 | 21 |
22 | 23 | 45 | 46 | -------------------------------------------------------------------------------- /CommentServer.php: -------------------------------------------------------------------------------- 1 | value,key是用户的uid,value是用户的fd 8 | public $user2fd = []; 9 | 10 | public function __construct() 11 | { 12 | $this->_serv = new swoole_websocket_server("127.0.0.1", 9501); 13 | $this->_serv->set([ 14 | 'worker_num' => 1, 15 | 'heartbeat_check_interval' => 60, 16 | 'heartbeat_idle_time' => 125, 17 | ]); 18 | $this->_serv->on('open', [$this, 'onOpen']); 19 | $this->_serv->on('message', [$this, 'onMessage']); 20 | $this->_serv->on('close', [$this, 'onClose']); 21 | } 22 | 23 | /** 24 | * @param $serv 25 | * @param $request 26 | * @return mixed 27 | */ 28 | public function onOpen($serv, $request) 29 | { 30 | // 连接授权 31 | $accessResult = $this->checkAccess($serv, $request); 32 | if (!$accessResult) { 33 | return false; 34 | } 35 | // 始终把用户最新的fd跟uid映射在一起 36 | if (array_key_exists($request->get['uid'], $this->user2fd)) { 37 | $existFd = $this->user2fd[$request->get['uid']]; 38 | $this->close($existFd, 'uid exists.'); 39 | $this->user2fd[$request->get['uid']] = $request->fd; 40 | return false; 41 | } else { 42 | $this->user2fd[$request->get['uid']] = $request->fd; 43 | } 44 | } 45 | 46 | /** 47 | * @param $serv 48 | * @param $frame 49 | * @return mixed 50 | */ 51 | public function onMessage($serv, $frame) 52 | { 53 | // 校验数据的有效性,我们认为数据被`json_decode`处理之后是数组并且数组的`event`项非空才是有效数据 54 | // 非有效数据,关闭该连接 55 | $data = $frame->data; 56 | $data = json_decode($data, true); 57 | if (!$data || !is_array($data) || empty($data['event'])) { 58 | $this->close($frame->fd, 'data format invalidate.'); 59 | return false; 60 | } 61 | // 根据数据的`event`项,判断要做什么,`event`映射到当前类具体的某一个方法,方法不存在则关闭连接 62 | $method = $data['event']; 63 | if (!method_exists($this, $method)) { 64 | $this->close($frame->fd, 'event is not exists.'); 65 | return false; 66 | } 67 | $this->$method($frame->fd, $data); 68 | } 69 | public function onClose($serv, $fd) 70 | { 71 | echo "client {$fd} closed.\n"; 72 | } 73 | 74 | /** 75 | * 校验客户端连接的合法性,无效的连接不允许连接 76 | * @param $serv 77 | * @param $request 78 | * @return mixed 79 | */ 80 | public function checkAccess($serv, $request) 81 | { 82 | // get不存在或者uid和token有一项不存在,关闭当前连接 83 | if (!isset($request->get) || !isset($request->get['uid']) || !isset($request->get['token'])) { 84 | $this->close($request->fd, 'access faild.'); 85 | return false; 86 | } 87 | $uid = $request->get['uid']; 88 | $token = $request->get['token']; 89 | // 校验token是否正确,无效关闭连接 90 | if (md5(md5($uid) . $this->key) != $token) { 91 | $this->close($request->fd, 'token invalidate.'); 92 | return false; 93 | } 94 | return true; 95 | } 96 | 97 | /** 98 | * @param $fd 99 | * @param $message 100 | * 关闭$fd的连接,并删除该用户的映射 101 | */ 102 | public function close($fd, $message = '') 103 | { 104 | // 关闭连接 105 | $this->_serv->close($fd); 106 | // 删除映射关系 107 | if ($uid = array_search($fd, $this->user2fd)) { 108 | unset($this->user2fd[$uid]); 109 | } 110 | } 111 | 112 | public function alertTip($fd, $data) 113 | { 114 | // 推送目标用户的uid非真或者该uid尚无保存的映射fd,关闭连接 115 | if (empty($data['toUid']) || !array_key_exists($data['toUid'], $this->user2fd)) { 116 | $this->close($fd); 117 | return false; 118 | } 119 | $this->push($this->user2fd[$data['toUid']], ['event' => $data['event'], 'msg' => '收到一条新的回复.']); 120 | } 121 | /** 122 | * @param $fd 123 | * @param $message 124 | */ 125 | public function push($fd, $message) 126 | { 127 | if (!is_array($message)) { 128 | $message = [$message]; 129 | } 130 | $message = json_encode($message); 131 | // push失败,close 132 | if ($this->_serv->push($fd, $message) == false) { 133 | $this->close($fd); 134 | } 135 | } 136 | 137 | public function start() 138 | { 139 | $this->_serv->start(); 140 | } 141 | } 142 | 143 | $server = new CommentServer; 144 | $server->start(); -------------------------------------------------------------------------------- /CommentServer2.php: -------------------------------------------------------------------------------- 1 | value,key是用户的uid,value是用户的fd 8 | public $user2fd = []; 9 | private $_tcp; 10 | 11 | public function __construct() 12 | { 13 | $this->_serv = new swoole_websocket_server("127.0.0.1", 9501); 14 | $this->_serv->set([ 15 | 'worker_num' => 1, 16 | 'heartbeat_check_interval' => 60, 17 | 'heartbeat_idle_time' => 125, 18 | ]); 19 | 20 | $this->_tcp = $this->_serv->listen('127.0.0.1', 9502, SWOOLE_SOCK_TCP); 21 | $this->_tcp->set([ 22 | 'open_eof_check' => true, //打开EOF检测 23 | 'package_eof' => "\r\n", //设置EOF 24 | 'open_eof_split' => true, // 自动分包 25 | ]); 26 | $this->_tcp->on('Receive', [$this, 'onReceive']); 27 | 28 | $this->_serv->on('open', [$this, 'onOpen']); 29 | $this->_serv->on('message', [$this, 'onMessage']); 30 | $this->_serv->on('close', [$this, 'onClose']); 31 | } 32 | 33 | /** 34 | * @param $serv 35 | * @param $request 36 | * @return mixed 37 | */ 38 | public function onOpen($serv, $request) 39 | { 40 | // 连接授权 41 | $accessResult = $this->checkAccess($serv, $request); 42 | if (!$accessResult) { 43 | return false; 44 | } 45 | // 始终把用户最新的fd跟uid映射在一起 46 | if (array_key_exists($request->get['uid'], $this->user2fd)) { 47 | $existFd = $this->user2fd[$request->get['uid']]; 48 | $this->close($existFd, 'uid exists.'); 49 | $this->user2fd[$request->get['uid']] = $request->fd; 50 | return false; 51 | } else { 52 | $this->user2fd[$request->get['uid']] = $request->fd; 53 | } 54 | } 55 | 56 | /** 57 | * @param $serv 58 | * @param $frame 59 | * @return mixed 60 | */ 61 | public function onMessage($serv, $frame) 62 | { 63 | // 校验数据的有效性,我们认为数据被`json_decode`处理之后是数组并且数组的`event`项非空才是有效数据 64 | // 非有效数据,关闭该连接 65 | $data = $frame->data; 66 | $data = json_decode($data, true); 67 | if (!$data || !is_array($data) || empty($data['event'])) { 68 | $this->close($frame->fd, 'data format invalidate.'); 69 | return false; 70 | } 71 | // 根据数据的`event`项,判断要做什么,`event`映射到当前类具体的某一个方法,方法不存在则关闭连接 72 | $method = $data['event']; 73 | if (!method_exists($this, $method)) { 74 | $this->close($frame->fd, 'event is not exists.'); 75 | return false; 76 | } 77 | $this->$method($frame->fd, $data); 78 | } 79 | public function onClose($serv, $fd) 80 | { 81 | echo "client {$fd} closed.\n"; 82 | } 83 | 84 | /** 85 | * 校验客户端连接的合法性,无效的连接不允许连接 86 | * @param $serv 87 | * @param $request 88 | * @return mixed 89 | */ 90 | public function checkAccess($serv, $request) 91 | { 92 | // get不存在或者uid和token有一项不存在,关闭当前连接 93 | if (!isset($request->get) || !isset($request->get['uid']) || !isset($request->get['token'])) { 94 | $this->close($request->fd, 'access faild.'); 95 | return false; 96 | } 97 | $uid = $request->get['uid']; 98 | $token = $request->get['token']; 99 | // 校验token是否正确,无效关闭连接 100 | if (md5(md5($uid) . $this->key) != $token) { 101 | $this->close($request->fd, 'token invalidate.'); 102 | return false; 103 | } 104 | return true; 105 | } 106 | 107 | /** 108 | * @param $fd 109 | * @param $message 110 | * 关闭$fd的连接,并删除该用户的映射 111 | */ 112 | public function close($fd, $message = '') 113 | { 114 | // 关闭连接 115 | $this->_serv->close($fd); 116 | // 删除映射关系 117 | if ($uid = array_search($fd, $this->user2fd)) { 118 | unset($this->user2fd[$uid]); 119 | } 120 | } 121 | 122 | public function alertTip($fd, $data) 123 | { 124 | // 推送目标用户的uid非真或者该uid尚无保存的映射fd,关闭连接 125 | if (empty($data['toUid']) || !array_key_exists($data['toUid'], $this->user2fd)) { 126 | $this->close($fd); 127 | return false; 128 | } 129 | $this->push($this->user2fd[$data['toUid']], ['event' => $data['event'], 'msg' => '收到一条新的回复.']); 130 | } 131 | /** 132 | * @param $fd 133 | * @param $message 134 | */ 135 | public function push($fd, $message) 136 | { 137 | if (!is_array($message)) { 138 | $message = [$message]; 139 | } 140 | $message = json_encode($message); 141 | // push失败,close 142 | if ($this->_serv->push($fd, $message) == false) { 143 | $this->close($fd); 144 | } 145 | } 146 | 147 | public function start() 148 | { 149 | $this->_serv->start(); 150 | } 151 | 152 | /** 153 | * =============== TCP SERVER OPERATE ============================================ 154 | */ 155 | /** 156 | * tcp server register function onReceive 157 | */ 158 | public function onReceive($serv, $fd, $fromId, $data) 159 | { 160 | try { 161 | $data = json_decode($data, true); 162 | if (!isset($data['event'])) { 163 | throw new \Exception("params error, needs event param.", 1); 164 | } 165 | 166 | $method = $data['event']; 167 | 168 | // 调起对应的方法 169 | if(!method_exists($this, $method)) { 170 | throw new \Exception("params error, not support method.", 1); 171 | } 172 | $this->$method($fd, $data); 173 | 174 | return true; 175 | 176 | } catch (\Exception $e) { 177 | $msg = $e->getMessage(); 178 | throw new \Exception("{$msg}", 1); 179 | } 180 | } 181 | } 182 | 183 | $server = new CommentServer2; 184 | $server->start(); -------------------------------------------------------------------------------- /NoReload.php: -------------------------------------------------------------------------------- 1 | _serv = new Swoole\Server("127.0.0.1", 9501); 16 | $this->_serv->set([ 17 | 'worker_num' => 1, 18 | ]); 19 | $this->_serv->on('Receive', [$this, 'onReceive']); 20 | 21 | $this->_test = new Test; 22 | } 23 | /** 24 | * start server 25 | */ 26 | public function start() 27 | { 28 | $this->_serv->start(); 29 | } 30 | public function onReceive($serv, $fd, $fromId, $data) 31 | { 32 | $this->_test->run($data); 33 | } 34 | } 35 | 36 | $reload = new NoReload; 37 | $reload->start(); 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # swoole-practice 2 | swoole基础课程练习 3 | 4 | # 对应的swoole系列基础教程地址如下 5 | 6 | http://www.manks.top/swoole-description.html 7 | -------------------------------------------------------------------------------- /Reload.php: -------------------------------------------------------------------------------- 1 | _serv = new Swoole\Server("127.0.0.1", 9501); 14 | $this->_serv->set([ 15 | 'worker_num' => 1, 16 | 'daemonize' => true, 17 | 'log_file' => __DIR__ . '/server.log', 18 | ]); 19 | $this->_serv->on('Receive', [$this, 'onReceive']); 20 | $this->_serv->on('WorkerStart', [$this, 'onWorkerStart']); 21 | } 22 | /** 23 | * start server 24 | */ 25 | public function start() 26 | { 27 | $this->_serv->start(); 28 | } 29 | public function onWorkerStart($serv, $workerId) 30 | { 31 | require_once("Test.php"); 32 | $this->_test = new Test; 33 | } 34 | public function onReceive($serv, $fd, $fromId, $data) 35 | { 36 | $this->_test->run($data); 37 | } 38 | } 39 | 40 | $reload = new Reload; 41 | $reload->start(); -------------------------------------------------------------------------------- /Test.php: -------------------------------------------------------------------------------- 1 | _serv = new swoole_websocket_server("127.0.0.1", 9501); 10 | $this->_serv->set([ 11 | 'worker_num' => 1, 12 | ]); 13 | $this->_serv->on('open', [$this, 'onOpen']); 14 | $this->_serv->on('message', [$this, 'onMessage']); 15 | $this->_serv->on('close', [$this, 'onClose']); 16 | } 17 | 18 | /** 19 | * @param $serv 20 | * @param $request 21 | */ 22 | public function onOpen($serv, $request) 23 | { 24 | echo "server: handshake success with fd{$request->fd}.\n"; 25 | } 26 | 27 | /** 28 | * @param $serv 29 | * @param $frame 30 | */ 31 | public function onMessage($serv, $frame) 32 | { 33 | // 循环当前的所有连接,并把接收到的客户端信息全部发送 34 | foreach ($serv->connections as $fd) { 35 | $serv->push($fd, $frame->data); 36 | } 37 | } 38 | public function onClose($serv, $fd) 39 | { 40 | echo "client {$fd} closed.\n"; 41 | } 42 | 43 | public function start() 44 | { 45 | $this->_serv->start(); 46 | } 47 | } 48 | 49 | $server = new WebSocketServer; 50 | $server->start(); -------------------------------------------------------------------------------- /WebSocketServerValid.php: -------------------------------------------------------------------------------- 1 | _serv = new swoole_websocket_server("127.0.0.1", 9501); 11 | $this->_serv->set([ 12 | 'worker_num' => 1, 13 | 'heartbeat_check_interval' => 30, 14 | 'heartbeat_idle_time' => 62, 15 | ]); 16 | $this->_serv->on('open', [$this, 'onOpen']); 17 | $this->_serv->on('message', [$this, 'onMessage']); 18 | $this->_serv->on('close', [$this, 'onClose']); 19 | } 20 | 21 | /** 22 | * @param $serv 23 | * @param $request 24 | */ 25 | public function onOpen($serv, $request) 26 | { 27 | $this->checkAccess($serv, $request); 28 | } 29 | 30 | /** 31 | * @param $serv 32 | * @param $frame 33 | */ 34 | public function onMessage($serv, $frame) 35 | { 36 | $this->_serv->push($frame->fd, 'Server: ' . $frame->data); 37 | } 38 | public function onClose($serv, $fd) 39 | { 40 | echo "client {$fd} closed.\n"; 41 | } 42 | 43 | /** 44 | * 校验客户端连接的合法性,无效的连接不允许连接 45 | * @param $serv 46 | * @param $request 47 | * @return mixed 48 | */ 49 | public function checkAccess($serv, $request) 50 | { 51 | // get不存在或者uid和token有一项不存在,关闭当前连接 52 | if (!isset($request->get) || !isset($request->get['uid']) || !isset($request->get['token'])) { 53 | $this->_serv->close($request->fd); 54 | return false; 55 | } 56 | $uid = $request->get['uid']; 57 | $token = $request->get['token']; 58 | // 校验token是否正确,无效关闭连接 59 | if (md5(md5($uid) . $this->key) != $token) { 60 | $this->_serv->close($request->fd); 61 | return false; 62 | } 63 | } 64 | 65 | public function start() 66 | { 67 | $this->_serv->start(); 68 | } 69 | } 70 | 71 | $server = new WebSocketServerValid; 72 | $server->start(); -------------------------------------------------------------------------------- /client-first.php: -------------------------------------------------------------------------------- 1 | connect('127.0.0.1', 9501) || exit("connect failed. Error: {$client->errCode}\n"); 20 | // 向服务端发送数据 21 | $client->send("hello server."); 22 | // 从服务端接收数据 23 | $response = $client->recv(); 24 | // 输出接受到的数据 25 | echo $response . PHP_EOL; 26 | // 关闭连接 27 | $client->close(); -------------------------------------------------------------------------------- /client-pack.php: -------------------------------------------------------------------------------- 1 | connect('127.0.0.1', 9501) || exit("connect failed. Error: {$client->errCode}\n"); 5 | 6 | // 向服务端发送数据 7 | for ($i = 0; $i < 3; $i++) { 8 | $data = "Just a test."; 9 | $data = pack('N', strlen($data)) . $data; 10 | $client->send($data); 11 | } 12 | 13 | $client->close(); -------------------------------------------------------------------------------- /client.php: -------------------------------------------------------------------------------- 1 | connect('127.0.0.1', 9501) || exit("connect failed. Error: {$client->errCode}\n"); 5 | 6 | // 向服务端发送数据 7 | $client->send("Just a test.\n"); 8 | $client->close(); -------------------------------------------------------------------------------- /commentClient.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | Document 9 | 10 | 11 |
12 | 13 | 14 |
15 | 91 | 92 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | Document 9 | 10 | 11 |
12 | 13 | 14 |
15 |
16 | 18 |
19 | 20 | 42 | 43 | -------------------------------------------------------------------------------- /server-eof-check.php: -------------------------------------------------------------------------------- 1 | _serv = new Swoole\Server("127.0.0.1", 9501); 13 | $this->_serv->set([ 14 | 'worker_num' => 1, 15 | 'open_eof_check' => true, //打开EOF检测 16 | 'package_eof' => "\r\n", //设置EOF 17 | ]); 18 | $this->_serv->on('Connect', array($this, 'onConnect')); 19 | $this->_serv->on('Close', array($this, 'onClose')); 20 | $this->_serv->on('Receive', [$this, 'onReceive']); 21 | } 22 | public function onConnect($serv, $fd, $fromId) 23 | { 24 | } 25 | public function onReceive($serv, $fd, $fromId, $data) 26 | { 27 | // echo "Server received data: {$data}" . PHP_EOL; 28 | 29 | $datas = explode("\r\n", $data); 30 | foreach ($datas as $data) 31 | { 32 | if(!$data) 33 | continue; 34 | 35 | echo "Server received data: {$data}" . PHP_EOL; 36 | } 37 | } 38 | public function onClose($serv, $fd, $fromId) 39 | { 40 | } 41 | /** 42 | * start server 43 | */ 44 | public function start() 45 | { 46 | $this->_serv->start(); 47 | } 48 | } 49 | 50 | $reload = new ServerEofCheck; 51 | $reload->start(); -------------------------------------------------------------------------------- /server-eof-client.php: -------------------------------------------------------------------------------- 1 | connect('127.0.0.1', 9501) || exit("connect failed. Error: {$client->errCode}\n"); 5 | 6 | // 向服务端发送数据 7 | for ($i = 0; $i < 3; $i++) { 8 | $client->send("Just a test.\r\n"); 9 | } 10 | $client->close(); 11 | 12 | -------------------------------------------------------------------------------- /server-eof-split.php: -------------------------------------------------------------------------------- 1 | _serv = new Swoole\Server("127.0.0.1", 9501); 13 | $this->_serv->set([ 14 | 'worker_num' => 1, 15 | 'open_eof_check' => true, //打开EOF检测 16 | 'package_eof' => "\r\n", //设置EOF 17 | 'open_eof_split' => true, 18 | ]); 19 | $this->_serv->on('Connect', array($this, 'onConnect')); 20 | $this->_serv->on('Close', array($this, 'onClose')); 21 | $this->_serv->on('Receive', [$this, 'onReceive']); 22 | } 23 | public function onConnect($serv, $fd, $fromId) 24 | { 25 | } 26 | public function onReceive($serv, $fd, $fromId, $data) 27 | { 28 | echo "Server received data: {$data}" . PHP_EOL; 29 | } 30 | public function onClose($serv, $fd, $fromId) 31 | { 32 | } 33 | /** 34 | * start server 35 | */ 36 | public function start() 37 | { 38 | $this->_serv->start(); 39 | } 40 | } 41 | 42 | $reload = new ServerEofSplit; 43 | $reload->start(); -------------------------------------------------------------------------------- /server-first.php: -------------------------------------------------------------------------------- 1 | set([ 6 | 'worker_num' => 2, 7 | ]); 8 | 9 | // 有新的客户端连接时,worker进程内会触发该回调 10 | $serv->on('Connect', function ($serv, $fd) { 11 | echo "new client connected." . PHP_EOL; 12 | }); 13 | 14 | // server接收到客户端的数据后,worker进程内触发该回调 15 | $serv->on('Receive', function ($serv, $fd, $fromId, $data) { 16 | // 收到数据后发送给客户端 17 | $serv->send($fd, 'Server '. $data); 18 | }); 19 | 20 | // 客户端断开连接或者server主动关闭连接时 worker进程内调用 21 | $serv->on('Close', function ($serv, $fd) { 22 | echo "Client close." . PHP_EOL; 23 | }); 24 | 25 | // 启动server 26 | $serv->start(); -------------------------------------------------------------------------------- /server-http.php: -------------------------------------------------------------------------------- 1 | on('request', function (swoole_http_request $request, swoole_http_response $response) { 5 | print_r($request); 6 | $response->status(200); 7 | $response->end('hello world.'); 8 | }); 9 | $http->start(); -------------------------------------------------------------------------------- /server-pack.php: -------------------------------------------------------------------------------- 1 | _serv = new Swoole\Server("127.0.0.1", 9501); 13 | $this->_serv->set([ 14 | 'worker_num' => 1, 15 | 'open_length_check' => true, // 开启协议解析 16 | 'package_length_type' => 'N', // 长度字段的类型 17 | 'package_length_offset' => 0, //第N个字节是包长度的值 18 | 'package_body_offset' => 4, //第几个字节开始计算长度 19 | 'package_max_length' => 81920, //协议最大长度 20 | ]); 21 | $this->_serv->on('Connect', array($this, 'onConnect')); 22 | $this->_serv->on('Receive', [$this, 'onReceive']); 23 | $this->_serv->on('Close', array($this, 'onClose')); 24 | } 25 | public function onConnect($serv, $fd, $fromId) 26 | { 27 | } 28 | public function onReceive($serv, $fd, $fromId, $data) 29 | { 30 | $info = unpack('N', $data); 31 | $len = $info[1]; 32 | $body = substr($data, - $len); 33 | echo "server received data: {$body}\n"; 34 | } 35 | public function onClose($serv, $fd, $fromId) 36 | { 37 | } 38 | /** 39 | * start server 40 | */ 41 | public function start() 42 | { 43 | $this->_serv->start(); 44 | } 45 | } 46 | 47 | $reload = new ServerPack; 48 | $reload->start(); -------------------------------------------------------------------------------- /server-task-first.php: -------------------------------------------------------------------------------- 1 | set([ 6 | 'task_worker_num' => 1, 7 | ]); 8 | 9 | $serv->on('Connect', function ($serv, $fd) { 10 | echo "new client connected." . PHP_EOL; 11 | }); 12 | $serv->on('Receive', function ($serv, $fd, $fromId, $data) { 13 | echo "worker received data: {$data}" . PHP_EOL; 14 | 15 | // 投递一个任务到task进程中 16 | $serv->task($data); 17 | 18 | // 通知客户端server收到数据了 19 | $serv->send($fd, 'This is a message from server.'); 20 | 21 | // 为了校验task是否是异步的,这里和task进程内都输出内容,看看谁先输出 22 | echo "worker continue run." . PHP_EOL; 23 | }); 24 | /** 25 | * $serv swoole_server 26 | * $taskId 投递的任务id,因为task进程是由worker进程发起,所以多worker多task下,该值可能会相同 27 | * $fromId 来自那个worker进程的id 28 | * $data 要投递的任务数据 29 | */ 30 | $serv->on('Task', function ($serv, $taskId, $fromId, $data) { 31 | echo "task start. --- from worker id: {$fromId}." . PHP_EOL; 32 | for ($i=0; $i < 5; $i++) { 33 | sleep(1); 34 | echo "task runing. --- {$i}" . PHP_EOL; 35 | } 36 | // echo "task end." . PHP_EOL; 37 | return "task end." . PHP_EOL; 38 | }); 39 | /** 40 | * 只有在task进程中调用了finish方法或者return了结果,才会触发finish 41 | */ 42 | $serv->on('Finish', function ($serv, $taskId, $data) { 43 | echo "finish received data '{$data}'" . PHP_EOL; 44 | }); 45 | 46 | $serv->start(); 47 | 48 | -------------------------------------------------------------------------------- /server.log: -------------------------------------------------------------------------------- 1 | server receive $data format error. 2 | -------------------------------------------------------------------------------- /tcp-buffer-client.php: -------------------------------------------------------------------------------- 1 | connect('127.0.0.1', 9501) || exit("connect failed. Error: {$client->errCode}\n"); 5 | 6 | // 向服务端发送数据 7 | for ($i = 0; $i < 3; $i++) { 8 | $client->send("Just a test.\n"); 9 | } 10 | $client->close(); 11 | 12 | -------------------------------------------------------------------------------- /tcp-buffer-server.php: -------------------------------------------------------------------------------- 1 | _serv = new Swoole\Server("127.0.0.1", 9501); 13 | $this->_serv->set([ 14 | 'worker_num' => 1, 15 | ]); 16 | $this->_serv->on('Connect', array($this, 'onConnect')); 17 | $this->_serv->on('Close', array($this, 'onClose')); 18 | $this->_serv->on('Receive', [$this, 'onReceive']); 19 | } 20 | public function onConnect($serv, $fd, $fromId) 21 | { 22 | } 23 | public function onReceive($serv, $fd, $fromId, $data) 24 | { 25 | echo "Server received data: {$data}" . PHP_EOL; 26 | } 27 | public function onClose($serv, $fd, $fromId) 28 | { 29 | } 30 | /** 31 | * start server 32 | */ 33 | public function start() 34 | { 35 | $this->_serv->start(); 36 | } 37 | } 38 | 39 | $reload = new TcpBufferServer; 40 | $reload->start(); -------------------------------------------------------------------------------- /tick-2.php: -------------------------------------------------------------------------------- 1 | = 5) { 9 | swoole_timer_clear($timeId); 10 | } 11 | }, 'world'); -------------------------------------------------------------------------------- /tick-after.php: -------------------------------------------------------------------------------- 1 | set([ 5 | 'worker_num' => 2, 6 | ]); 7 | $serv->on('WorkerStart', function ($serv, $workerId){ 8 | if ($workerId == 0) { 9 | $i = 0; 10 | $params = 'world'; 11 | $serv->tick(1000, function ($timeId) use ($serv, &$i, $params) { 12 | $i ++; 13 | echo "hello, {$params} --- {$i}\n"; 14 | if ($i >= 5) { 15 | $serv->clearTimer($timeId); 16 | } 17 | }); 18 | } 19 | }); 20 | $serv->on('Connect', function ($serv, $fd) { 21 | }); 22 | $serv->on('Receive', function ($serv, $fd, $fromId, $data) { 23 | $serv->after(3000, function () { 24 | echo "only once.\n"; 25 | }); 26 | }); 27 | $serv->on('Close', function ($serv, $fd) { 28 | }); 29 | $serv->start(); -------------------------------------------------------------------------------- /tick.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | Document 9 | 10 | 11 | 23 | 24 | -------------------------------------------------------------------------------- /websocket-client-success.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | Document 9 | 10 | 11 | 16 | 28 | 29 | --------------------------------------------------------------------------------