使用 前端 js 爬取斗鱼弹幕

     var ws=new WebSocket("wss://danmuproxy.douyu.com:8506/")
     ws.onopen =function(){
        var str = 'type@=loginreq/roomid@=74751/ver@=20190610/'+'\0'
       var buffer =message(str)
       // console.log(buffer)
        ws.send(buffer)
        var str1 = "type@=joingroup/rid@=74751/gid@=-9999/"+"\0"
        var buffer1 = message(str1)
        setTimeout(() => {
            ws.send(buffer1)
        }, 1000);
        setInterval(() => {
            ws.send(message("type@=mrkl/" + '\0'))
        }, 45000);
        //msg = 'type@=loginreq/roomid@=74751/'
       // var data_len = msg.length+9;
     }

function message(str){
//仿照 ws里边的 16位数据生成
var len = str.length
var buffer = new ArrayBuffer(len+12)
buf = new DataView(buffer).setInt16(0,len+8,true)
new DataView(buffer).setInt16(4,len+8,true)
new DataView(buffer).setInt16(8,45314,false)
//new DataView(buffer).setInt16(12,45314,false)
var bufView = new Uint8Array(buffer,12);
for (var i=0; i bufView[i] = str.charCodeAt(i);
}
return buffer;
}
//详细的都不介绍了,懂得自然一看就懂

你可能感兴趣的:(js)