Browse Source

Simplify workerman (#5070)

* Simplify workerman

* Delete unnecessary set

* Delete unnecessary set
Joan Miquel 5 years ago
parent
commit
21f30e297f

+ 2 - 6
frameworks/PHP/workerman/dbraw.php

@@ -7,9 +7,7 @@ function dbraw($pdo)
 
 
     if ( ! isset($_GET['queries'])) {
     if ( ! isset($_GET['queries'])) {
         $statement->execute([mt_rand(1, 10000)]);
         $statement->execute([mt_rand(1, 10000)]);
-        echo json_encode($statement->fetch(PDO::FETCH_ASSOC));
-
-        return;
+        return json_encode($statement->fetch(PDO::FETCH_ASSOC));
     }
     }
 
 
     $query_count = 1;
     $query_count = 1;
@@ -17,12 +15,10 @@ function dbraw($pdo)
         $query_count = min($_GET['queries'], 500);
         $query_count = min($_GET['queries'], 500);
     }
     }
 
 
-    $arr = [];
-
     while ($query_count--) {
     while ($query_count--) {
         $statement->execute([mt_rand(1, 10000)]);
         $statement->execute([mt_rand(1, 10000)]);
         $arr[] = $statement->fetch(PDO::FETCH_ASSOC);
         $arr[] = $statement->fetch(PDO::FETCH_ASSOC);
     }
     }
 
 
-    echo json_encode($arr);
+    return json_encode($arr);
 }
 }

+ 8 - 6
frameworks/PHP/workerman/fortune.php

@@ -4,13 +4,15 @@ function fortune($pdo)
     static $statement;
     static $statement;
     $statement = $statement ?? $pdo->prepare('SELECT id,message FROM Fortune');
     $statement = $statement ?? $pdo->prepare('SELECT id,message FROM Fortune');
     $statement->execute();
     $statement->execute();
+
     $arr       = $statement->fetchAll(PDO::FETCH_KEY_PAIR);
     $arr       = $statement->fetchAll(PDO::FETCH_KEY_PAIR);
     $arr[0]    = 'Additional fortune added at request time.';
     $arr[0]    = 'Additional fortune added at request time.';
     asort($arr);
     asort($arr);
-?>
-<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>
-<?php foreach ($arr as $id => $fortune): ?>
-<tr><td><?=$id?></td><td><?=htmlspecialchars($fortune, ENT_QUOTES, 'UTF-8');?></td></tr>
-<?php endforeach?></table></body></html>
-<?php
+
+    $html = '<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>';
+    foreach ($arr as $id => $message) {
+        $message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
+        $html .= "<tr><td>{$id}</td><td>{$message}</td></tr>";
+    }
+    return $html . '</table></body></html>';
 }
 }

+ 3 - 9
frameworks/PHP/workerman/server.php

@@ -31,23 +31,17 @@ $http_worker->onMessage = function ($connection) {
 
 
         case '/db':
         case '/db':
             Http::header('Content-Type: application/json');
             Http::header('Content-Type: application/json');
-            ob_start();
-            dbraw($pdo);
-            $connection->send(ob_get_clean());
+            $connection->send(dbraw($pdo));
             break;
             break;
 
 
         case '/fortune':
         case '/fortune':
             Http::header('Content-Type: text/html; charset=utf-8');
             Http::header('Content-Type: text/html; charset=utf-8');
-            ob_start();
-            fortune($pdo);
-            $connection->send(ob_get_clean());
+            $connection->send(fortune($pdo));
             break;
             break;
 
 
         case '/update':
         case '/update':
             Http::header('Content-Type: application/json');
             Http::header('Content-Type: application/json');
-            ob_start();
-            updateraw($pdo);
-            $connection->send(ob_get_clean());
+            $connection->send(updateraw($pdo));
             break;
             break;
 
 
             //case '/info':
             //case '/info':

+ 1 - 2
frameworks/PHP/workerman/updateraw.php

@@ -5,7 +5,6 @@ function updateraw($pdo) {
     $query_count = min($_GET['queries'], 500);
     $query_count = min($_GET['queries'], 500);
   }
   }
 
 
-  $arr = [];
   $statement = $pdo->prepare('SELECT randomNumber FROM World WHERE id = ?');
   $statement = $pdo->prepare('SELECT randomNumber FROM World WHERE id = ?');
   $updateStatement = $pdo->prepare('UPDATE World SET randomNumber = ? WHERE id = ?');
   $updateStatement = $pdo->prepare('UPDATE World SET randomNumber = ? WHERE id = ?');
 
 
@@ -21,5 +20,5 @@ function updateraw($pdo) {
     $arr[] = $world;
     $arr[] = $world;
   }
   }
 
 
-  echo json_encode($arr);
+  return json_encode($arr);
 }
 }