@@ -96,7 +96,16 @@ export class BaseClient extends EventEmitter {
96
96
sig_session : BUF0 ,
97
97
session_key : BUF0 ,
98
98
} ,
99
- hb480 : BUF0 ,
99
+ hb480 : ( ( ) => {
100
+ const buf = Buffer . alloc ( 9 )
101
+ buf . writeUInt32BE ( this . uin )
102
+ buf . writeInt32BE ( 0x19e39 , 5 )
103
+ return pb . encode ( {
104
+ 1 : 1152 ,
105
+ 2 : 9 ,
106
+ 4 : buf
107
+ } )
108
+ } ) ( ) ,
100
109
emp_time : 0 ,
101
110
}
102
111
readonly pskey : { [ domain : string ] : Buffer } = { }
@@ -527,8 +536,6 @@ function ssoListener(this: BaseClient, cmd: string, payload: Buffer, seq: number
527
536
case "StatSvc.ReqMSFOffline" :
528
537
case "MessageSvc.PushForceOffline" :
529
538
{
530
- this [ IS_ONLINE ] = false
531
- clearInterval ( this [ HEARTBEAT ] )
532
539
const nested = jce . decodeWrapper ( payload )
533
540
const msg = nested [ 4 ] ? `[${ nested [ 4 ] } ]${ nested [ 3 ] } ` : `[${ nested [ 1 ] } ]${ nested [ 2 ] } `
534
541
this . emit ( EVENT_KICKOFF , msg )
@@ -564,31 +571,14 @@ function onlineListener(this: BaseClient) {
564
571
if ( ! this . listenerCount ( EVENT_KICKOFF ) ) {
565
572
this . once ( EVENT_KICKOFF , ( msg : string ) => {
566
573
this [ IS_ONLINE ] = false
574
+ clearInterval ( this [ HEARTBEAT ] )
567
575
this . emit ( "internal.kickoff" , msg )
568
576
} )
569
577
}
570
- const buf = Buffer . alloc ( 9 )
571
- buf . writeUInt32BE ( this . uin )
572
- buf . writeInt32BE ( 0x19e39 , 5 )
573
- this . sig . hb480 = Buffer . from ( pb . encode ( {
574
- 1 : 1152 ,
575
- 2 : 9 ,
576
- 4 : buf
577
- } ) )
578
- this [ HEARTBEAT ] = setInterval ( async ( ) => {
579
- if ( typeof this . heartbeat === "function" )
580
- await this . heartbeat ( )
581
- this . sendUni ( "OidbSvc.0x480_9_IMCore" , this . sig . hb480 ) . catch ( ( ) => {
582
- this . emit ( "internal.verbose" , "heartbeat timeout" , VerboseLevel . Warn )
583
- this . sendUni ( "OidbSvc.0x480_9_IMCore" , this . sig . hb480 ) . catch ( ( ) => {
584
- this . emit ( "internal.verbose" , "heartbeat timeout x 2" , VerboseLevel . Error )
585
- this [ NET ] . destroy ( )
586
- } )
587
- } ) . then ( refreshToken . bind ( this ) )
588
- } , this . interval * 1000 )
589
578
}
590
579
591
580
function lostListener ( this : BaseClient ) {
581
+ clearInterval ( this [ HEARTBEAT ] )
592
582
if ( this [ IS_ONLINE ] ) {
593
583
this [ IS_ONLINE ] = false
594
584
this . statistics . lost_times ++
@@ -653,6 +643,8 @@ async function packetListener(this: BaseClient, pkt: Buffer) {
653
643
}
654
644
655
645
async function register ( this : BaseClient , logout = false ) {
646
+ this [ IS_ONLINE ] = false
647
+ clearInterval ( this [ HEARTBEAT ] )
656
648
const pb_buf = pb . encode ( {
657
649
1 : [
658
650
{ 1 : 46 , 2 : timestamp ( ) } ,
@@ -679,15 +671,24 @@ async function register(this: BaseClient, logout = false) {
679
671
const rsp = jce . decodeWrapper ( payload )
680
672
const result = rsp [ 9 ] ? true : false
681
673
if ( ! result ) {
682
- clearInterval ( this [ HEARTBEAT ] )
683
674
this . emit ( "internal.error.token" )
684
675
} else {
685
676
this [ IS_ONLINE ] = true
677
+ this [ HEARTBEAT ] = setInterval ( async ( ) => {
678
+ if ( typeof this . heartbeat === "function" )
679
+ await this . heartbeat ( )
680
+ this . sendUni ( "OidbSvc.0x480_9_IMCore" , this . sig . hb480 ) . catch ( ( ) => {
681
+ this . emit ( "internal.verbose" , "heartbeat timeout" , VerboseLevel . Warn )
682
+ this . sendUni ( "OidbSvc.0x480_9_IMCore" , this . sig . hb480 ) . catch ( ( ) => {
683
+ this . emit ( "internal.verbose" , "heartbeat timeout x 2" , VerboseLevel . Error )
684
+ this [ NET ] . destroy ( )
685
+ } )
686
+ } ) . then ( refreshToken . bind ( this ) )
687
+ } , this . interval * 1000 )
686
688
}
687
689
} catch {
688
- if ( logout ) return
689
- clearInterval ( this [ HEARTBEAT ] )
690
- this . emit ( "internal.error.network" , - 3 , "server is busy(register)" )
690
+ if ( ! logout )
691
+ this . emit ( "internal.error.network" , - 3 , "server is busy(register)" )
691
692
}
692
693
}
693
694
@@ -726,7 +727,6 @@ async function refreshToken(this: BaseClient) {
726
727
const t = readTlv ( stream )
727
728
if ( type === 0 ) {
728
729
const { token } = decodeT119 . call ( this , t [ 0x119 ] )
729
- this [ IS_ONLINE ] = false
730
730
await register . call ( this )
731
731
if ( this [ IS_ONLINE ] )
732
732
this . emit ( "internal.token" , token )
0 commit comments