Appearance
代码示例
js
// 打开浏览器,打开新页面
const puppeteer = require("puppeteer");
const protobuf = require("protobufjs");
const pako = require("pako");
var root = protobuf.Root.fromJSON(require("./message.json"));
(async () => {
browser = await puppeteer.launch()
page = await browser.newPage()
const client = await page.target().createCDPSession()
await client.send('Network.enable')
// client.on('Network.webSocketCreated',
// function(params){
// console.log(`创建 WebSocket 连接:${params.url}`)
// }
// )
// client.on('Network.webSocketClosed',
// function(params){
// console.log(`WebSocket 连接关闭`)
// }
// )
// client.on('Network.webSocketFrameSent',
// function(params){
// console.log(`发送 WebSocket 消息:${params.response.payloadData}`)
// }
// )
let PushFrame = root.lookup("PushFrame")
let Response = root.lookupType("Response")
let ChatMessage = root.lookupType("ChatMessage")
let MemberMessage = root.lookupType("MemberMessage")
client.on('Network.webSocketFrameReceived',
function (params) {
var buf = Buffer.from(params.response.payloadData, 'base64');
let o = PushFrame.decode(new Uint8Array(buf))
var payload = o.payload;
var isZip = Object.values(o.headersList).filter(t => "compress_type" === t.key && "gzip" === t.value).length > 0 ? true : false;
payload = isZip ? pako.inflate(payload) : payload;
var r = Response.decode(payload)
var messageList = r.messagesList || [];
messageList.forEach(message => {
var p = message.payload;
var msg = ''
if (message.method === 'WebcastChatMessage') {
msg = ChatMessage.decode(p)
} else
if (message.method === 'WebcastMemberMessage') {
msg = MemberMessage.decode(p)
}
console.log(msg)
})
}
)
await page.goto(`https://live.douyin.com/168465302284`, { timeout: 0 })
})();