1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
   | <?php
 
 
 
 
  namespace app\http;
  use think\Db; use GatewayWorker\Lib\Gateway;
  class Event {
      public static function onConnect($client_id)     {         Gateway::sendToClient($client_id, json_encode([             'type'      => 'init',//客户连接后向用户发送绑定uid请求(1)             'client_id' => $client_id         ]));     }
      
 
 
 
 
      public static function onMessage($client_id, $message)     {         $message_data = json_decode($message, true);         if ( ! $message_data) {             return;         }         $shop_id     = ! empty($message_data['shop_id']) ? $message_data['shop_id'] : 0;         $admin_id    = ! empty($message_data['admin_id']) ? $message_data['admin_id'] : 0;         $table_id    = ! empty($message_data['table_id']) ? $message_data['table_id'] : 0;         $client_type = ! empty($message_data['client_type']) ? $message_data['client_type'] : 0;         $line_num    = ! empty($message_data['line_num']) ? $message_data['line_num'] : 0;
                   $uid = $shop_id.'-'.$client_type.'-'.$admin_id.'-'.$table_id;
          $from_id = $message_data['from_id'];                  $to_id = isset($message_data['to_id']) ? $message_data['to_id'] : 0;
          switch ($message_data['type']) {             case "bind":                 Gateway::bindUid($client_id, $uid);                 $findClient = Db::name('socket_client')->where([                     'type'     => $client_type,                     'shop_id'  => $shop_id,                     'admin_id' => $admin_id,                     'table_id' => $table_id                 ])->find();                 if (empty($findClient)) {                     Db::name('socket_client')->insert([                         'shop_id'     => $shop_id,                         'admin_id'    => $admin_id,                         'table_id'    => $table_id,                         'client'      => $client_id,                         'type'        => $client_type,                         'create_time' => time()                     ]);                 } else {                     Db::name('socket_client')->where([                         'type'     => $client_type,                         'shop_id'  => $shop_id,                         'admin_id' => $admin_id,                         'table_id' => $table_id                     ])->data(['client' => $client_id, 'update_time' => time()])->update();                 }
                  break;             case "online":                                  $onlineStatusForFrom_id = Gateway::isUidOnline($uid);                 Gateway::sendToUid($uid, json_encode(['type' => 'online', 'status' => $onlineStatusForFrom_id]));                 break;             case "say":                                  $text    = nl2br(htmlspecialchars($message_data['content']));                 $sayData = [                     'type'     => 'say',                     'content'  => $text,                     'from_id'  => $from_id,                     'to_id'    => $to_id,                     'log_time' => time()                 ];                 if (Gateway::isUidOnline($to_id)) {                     $sayData['is_read'] = 1;                     Gateway::sendToUid($to_id, json_encode($sayData));                 } else {                     $sayData['is_read'] = 0;                 }                 Gateway::sendToUid($from_id, json_encode($sayData));                 
                  break;             case "middleware":                                  $middleData = [                     'type'     => 'middleware',                     'shop_id'  => $shop_id,                     'admin_id' => $admin_id,                     'table_id' => $table_id,                     'line_num' => $line_num                 ];                 Gateway::sendToAll(json_encode($middleData));                 break;             case "pong":                 return;                 break;             default:                 Gateway::sendToAll(json_encode(['content' => '发送所有数据']));         }     }
      
 
 
 
      public static function onClose($client_id)     {                  GateWay::sendToAll("$client_id logout\r\n");     }
  }
 
   |