From 8d09794bc8b9f49120b7a4137617980acefe9717 Mon Sep 17 00:00:00 2001 From: lin <465382251@qq.com> Date: Thu, 15 Jul 2021 09:29:20 +0800 Subject: [PATCH] Private and Public channel reconnect --- src/Api/WebSocket/SocketClient.php | 27 +++++++++++++++------------ src/Api/WebSocket/SocketServer.php | 8 +++++++- src/BitmexWebSocket.php | 14 ++++++++++++++ tests/websocket/client.php | 5 ++--- 4 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/Api/WebSocket/SocketClient.php b/src/Api/WebSocket/SocketClient.php index b9a0539..2f396b8 100644 --- a/src/Api/WebSocket/SocketClient.php +++ b/src/Api/WebSocket/SocketClient.php @@ -168,6 +168,21 @@ protected function getData($global,$callback=null,$sub=[]){ return $temp; } + /* + * + * */ + function reconPrivate(string $key){ + $this->client->debug=[ + 'private'=>[$key=>'close'], + ]; + } + + function reconPublic(){ + $this->client->debug=[ + 'public'=>['public'=>'close','kline'=>'close'], + ]; + } + function test(){ print_r($this->client->all_sub); print_r($this->client->add_sub); @@ -185,19 +200,7 @@ function test2(){ } } - function test_reconnection(){ - $this->client->debug=[ - 'public'=>['public'=>'close','kline'=>'close'], - ]; - } - function test_reconnection2(){ $this->client->debug2=1; } - - function test_reconnection3($key){ - $this->client->debug=[ - 'private'=>[$key=>'close'], - ]; - } } diff --git a/src/Api/WebSocket/SocketServer.php b/src/Api/WebSocket/SocketServer.php index 253338d..5086202 100644 --- a/src/Api/WebSocket/SocketServer.php +++ b/src/Api/WebSocket/SocketServer.php @@ -253,6 +253,12 @@ private function debug($con,$global){ $debug['private'][$con->tag_keysecret['key']]='recon'; $global->save('debug',$debug); + //更改登录状态 + $this->keysecretInit($con->tag_keysecret,[ + 'connection'=>2, + 'auth'=>0, + ]); + $con->close(); } } @@ -303,7 +309,7 @@ private function subscribe($con,$global){ if($con->tag!='public' && !empty($temp['private'])){ //判断是否鉴权登录 $keysecret=$global->get('keysecret'); - if($keysecret[$con->tag_keysecret['key']]['auth']==0) { + if($keysecret[$con->tag_keysecret['key']]['auth']!=1 || $keysecret[$con->tag_keysecret['key']]['key']!=$con->tag_keysecret['key']) { $this->log($con->tag_keysecret['key'].' subscribe need login '); return; } diff --git a/src/BitmexWebSocket.php b/src/BitmexWebSocket.php index 2b0f1b7..bbaabea 100644 --- a/src/BitmexWebSocket.php +++ b/src/BitmexWebSocket.php @@ -84,4 +84,18 @@ public function getSubscribes($callback=null,$daemon=false){ return $this->client()->getSubscribes($callback,$daemon); } + /** + * Private channel reconnect + * @param string $key + */ + public function reconPrivate(string $key){ + $this->client()->reconPrivate($key); + } + + /** + * Public channel reconnect + */ + public function reconPublic(){ + $this->client()->reconPublic(); + } } diff --git a/tests/websocket/client.php b/tests/websocket/client.php index 737368d..61aa1cd 100644 --- a/tests/websocket/client.php +++ b/tests/websocket/client.php @@ -272,8 +272,7 @@ } case 10005:{ - $bitmex->client()->test_reconnection(); - + $bitmex->reconPublic(); break; } @@ -283,7 +282,7 @@ } case 10008:{ - $bitmex->client()->test_reconnection3($key_secret[0]['key']); + $bitmex->reconPrivate($key_secret[0]['key']); break; } }