123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
- /**
- * This file is part of workerman.
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the MIT-LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @author walkor<walkor@workerman.net>
- * @copyright walkor<walkor@workerman.net>
- * @link http://www.workerman.net/
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- /**
- * 用于检测业务代码死循环或者长时间阻塞等问题
- * 如果发现业务卡死,可以将下面declare打开(去掉//注释),并执行php start.php reload
- * 然后观察一段时间workerman.log看是否有process_timeout异常
- */
- //declare(ticks=1);
- /**
- * 聊天主逻辑
- * 主要是处理 onMessage onClose
- */
- use \GatewayWorker\Lib\Gateway;
- //use \GatewayWorker\Lib\DbConnection;
- use think\Db;
- use think\App;
- class Events
- {
- public static function onWorkerStart(){
- App::initCommon();
- //$info = Db::name('gateway_test')->insertGetId(['msg'=>'onWorkerStart','datetime'=>date('Y-m-d H:i:s')]);
- }
- /**
- * 有消息时
- * @param int $client_id
- * @param mixed $message
- */
- public static function onMessage($client_id, $message)
- {
- //echo $message.PHP_EOL;
- //file_put_contents('1.json',$message.PHP_EOL,FILE_APPEND);
- //Gateway::sendToAll($client_id.':'.$message);
- //php版本用7.3
- try{
- $data = self::get_data($message);
- if(!empty($data)){
- //file_put_contents('1.json',json_encode($data).PHP_EOL,FILE_APPEND);
- $info = Db::name('device_sanheyi')->insert($data);
- }
- }catch (\Exception $e)
- {
- //echo $e->getMessage();
- }
- //Gateway::sendToAll($client_id.':'.$message);
- return;
- /* $message = json_decode($message,true);
- if(is_array($message)){
- self::changestatus($message);
- }else{
- //echo 'default';
- }*/
- //return ;
- /*if(is_array($message) && isset($message['action']) && $message['action'] == 'changestatus'){
- self::changestatus($message);
- }else{
- echo 'default';
- }
- return ;*/
- }
- /**
- * 当客户端连接时触发
- * 如果业务不需此回调可以删除onConnect
- * @param int $client_id 连接id
- */
- public static function onConnect($client_id)
- {
- // 向当前client_id发送数据
- // Gateway::sendToClient($client_id, "Hello $client_id");
- // 向所有人发送
- // Gateway::sendToAll("$client_id login");
- }
- /*{
- 'action':'changestatus',
- "uid": "1",
- "type": "video",'audio'
- "value": "0",或‘1’
- "nowtime": "1234567890 ",
- "token": "edbf8a75-8e36-41f5-989f-7b3d067ccc83"
- }*/
- //修改用的通话状态
- private static function changestatus($message){
- //dump($message);
- if(isset($message['uid']) && isset($message['value'])){
- $redis = newredis();
- $redis->set('gg_matching_uid_'.$message['uid'], $message['value'], null);
- }
- return;
- }
- //聊天扣钱
- private static function userchat(){
- $price = config('site.typing_min_price');
- //扣费
- Db::startTrans();
- if(isset($message['uid'])){
- $rs = model('wallet')->lockChangeAccountRemain($message['uid'],'gold',-$price,13,'','',0);
- if($rs['status'] === false){
- Db::rollback();
- }
- }
- Db::commit();
- }
- public static function test31(){
- //血糖,5秒
- $str = ' 7.2mmol/L (GLUC 2023/12/29 06:05 SN:PMA231223000006 )';
- //尿酸,16秒
- //$str = ' 381umol/L ( UA 2023/12/29 06:21 SN:PMA231223000006 )';
- //胆总,26秒
- //$str = ' 3.91mmol/L (CHOL 2023/12/29 06:25 SN:PMA231223000006 )';
- $result = self::get_data($str);
- dump($result);
- }
- private static function get_data($str = ''){
- $str = trim($str);
- //判断为空
- if(empty($str)){
- return [];
- }
- //找到编号起始点
- $SN_start = strpos($str,'SN:');
- if($SN_start === false){
- return [];
- }
- //编号
- $SN = mb_substr($str,$SN_start+3,15);
- if(empty($SN)){
- return [];
- }
- //判断类型
- $type_gluc = strpos($str,'GLUC');
- $type_ua = strpos($str,'UA');
- $type_chol = strpos($str,'CHOL');
- if($type_gluc === false && $type_ua === false && $type_chol === false){
- return [];
- }
- //确认类型
- $type = '';
- $type_text = '';
- if($type_gluc){
- $type = 'GLUC';
- $type_text = '血糖';
- }
- if($type_ua){
- $type = 'UA';
- $type_text = '尿酸';
- }
- if($type_chol){
- $type = 'CHOL';
- $type_text = '总胆固醇';
- }
- if(empty($type) || empty($type_text)){
- return [];
- }
- //数值
- $value_end = strpos($str,'/L');
- if($value_end === false){
- return [];
- }
- $value = mb_substr($str,0,$value_end+2);
- if(empty($value)){
- return [];
- }
- //返回
- $data = [
- 'msg' => $str,
- 'datetime' =>date('Y-m-d H:i:s'),
- 'createtime' =>time(),
- 'sn' => $SN,
- 'type' => $type,
- 'type_text' => $type_text,
- 'value' => $value,
- ];
- return $data;
- }
-
- /**
- * 当客户端断开连接时
- * @param integer $client_id 客户端id
- */
- public static function onClose($client_id)
- {
- // debug
- /* echo "client:{$_SERVER['REMOTE_ADDR']}:{$_SERVER['REMOTE_PORT']} gateway:{$_SERVER['GATEWAY_ADDR']}:{$_SERVER['GATEWAY_PORT']} client_id:$client_id onClose:''\n";
-
- // 从房间的客户端列表中删除
- if(isset($_SESSION['room_id']))
- {
- $room_id = $_SESSION['room_id'];
- $new_message = array('type'=>'logout', 'from_client_id'=>$client_id, 'from_client_name'=>$_SESSION['client_name'], 'time'=>date('Y-m-d H:i:s'));
- Gateway::sendToGroup($room_id, json_encode($new_message));
- }*/
- }
- public static function onWorkerStop(){
- //$info = Db::name('gateway_test')->insertGetId(['msg'=>'onWorkerStop','datetime'=>date('Y-m-d H:i:s')]);
- }
-
- }
|