1.定義方法名
options.detail = '測試'; /** * 自定義事件, websoketCreate接收 */ const event = new CustomEvent('watchWebsocket', options); window.dispatchEvent(event);
2.接收
/** * 自定義事件, 接收onmessage返回的數據 */ window.addEventListener( 'watchWebsocket', (options) => { // console.log('subWsDepth訂閱深度', options['detail']); const data = options['detail']; const channel = data.channel; const regDepth = /.*depth.*/; // 深度數據正則 const regDeal = /^market_[a-z0-9]*_trade_ticker$/; // 成交數據正則 const regMiner = /^push_time_miner_message_/; // 時間礦工 // 深度圖 if (regDepth.test(channel)) { if (data.event_rep) return; // console.log('subWsDepth訂閱深度', data); bus.$emit('regdepth', data); } }, false );