src/danmu/bilibilitcp.js
/**
         * Upon receiving a complete message, decode and handle tasks
         *
         * @param   {Buffer}    buffer  - Complete message
         */
Upon receiving a complete message, decode and handle tasks
@param   {Buffer}    buffer  - Complete message
Upon receiving a complete message decode and handle tasks @param { Buffer } buffer - Complete message
onMessage(buffer) {
            const totalLength = buffer.readUInt32BE(0);
            const headerLength = buffer.readUInt16BE(4);
            const cmd = buffer.readUInt32BE(8);
            let jsonStr = '';
            let msg = null;
            switch (cmd) {
                case 3:
                    const popularity = buffer.readUInt32BE(headerLength);
                    this.onPopularity(popularity);
                    break;
                case 5:
                    jsonStr = buffer.toString('utf8', headerLength, totalLength);
                    msg = JSON.parse(jsonStr);
                    this.processMsg(msg);
                    break;
                case 8:
                    if (this.heartbeatTask === null) {
                        this.socket.write(this.heartbeat);
                        this.heartbeatTask = setInterval(() => {
                            this.socket && this.socket.write(this.heartbeat);
                        }, 30 * 1000);
                    }
                    break;
            }
        }
onMessage ( buffer ) { const totalLength = buffer . readUInt32BE ( 0 ) ; const headerLength = buffer . readUInt16BE ( 4 ) ; const cmd = buffer . readUInt32BE ( 8 ) ; let jsonStr = '' ; let msg = null ; switch ( cmd ) { case 3 : const popularity = buffer . readUInt32BE ( headerLength ) ; this . onPopularity ( popularity ) ; break ; case 5 : jsonStr = buffer . toString ( 'utf8' , headerLength , totalLength ) ; msg = JSON . parse ( jsonStr ) ; this . processMsg ( msg ) ; break ; case 8 : if ( this . heartbeatTask === null ) { this . socket . write ( this . heartbeat ) ; this . heartbeatTask = setInterval ( ( ) => { this . socket && this . socket . write ( this . heartbeat ) ; } , 30 * 1000 ) ; } break ; } }
Upon receiving a complete message, decode and handle tasks
 @param   {Buffer}    buffer  - Complete message
Upon receiving a complete message decode and handle tasks @param { Buffer } buffer - Complete message