@@ -731,6 +731,34 @@ public function keepAlive() {
731
731
});
732
732
$ loop ->run ();
733
733
}
734
+
735
+ // Issues userDataStream token and keepalive, subscribes to userData WebSocket
736
+ public function userData (&$ balance_callback , &$ execution_callback = false ) {
737
+ $ response = $ this ->apiRequest ("v1/userDataStream " , "POST " );
738
+ $ listenKey = $ this ->options ['listenKey ' ] = $ response ['listenKey ' ];
739
+ $ this ->info ['balanceCallback ' ] = $ balance_callback ;
740
+ $ this ->info ['executionCallback ' ] = $ execution_callback ;
741
+ \Ratchet \Client \connect ('wss://stream.binance.com:9443/ws/ ' .$ listenKey )->then (function ($ ws ) {
742
+ $ ws ->on ('message ' , function ($ data ) use ($ ws ) {
743
+ $ json = json_decode ($ data );
744
+ $ type = $ json ->e ;
745
+ if ( $ type == "outboundAccountInfo " ) {
746
+ $ balances = $ this ->balanceHandler ($ json ->B );
747
+ $ this ->info ['balanceCallback ' ]($ this , $ balances );
748
+ } elseif ( $ type == "executionReport " ) {
749
+ $ report = $ this ->executionHandler ($ json );
750
+ if ( $ this ->info ['executionCallback ' ] ) {
751
+ $ this ->info ['executionCallback ' ]($ this , $ report );
752
+ }
753
+ }
754
+ });
755
+ $ ws ->on ('close ' , function ($ code = null , $ reason = null ) {
756
+ echo "userData: WebSocket Connection closed! ( {$ code } - {$ reason }) " .PHP_EOL ;
757
+ });
758
+ }, function ($ e ) {
759
+ echo "userData: Could not connect: {$ e ->getMessage ()}" .PHP_EOL ;
760
+ });
761
+ }
734
762
735
763
public function miniTicker ($ callback ) {
736
764
\Ratchet \Client \connect ('wss://stream2.binance.com:9443/ws/!miniTicker@arr@1000ms ' )
0 commit comments