| 49 | } |
| 50 | |
| 51 | function waitForPackets(socket, count) { |
| 52 | const packets = []; |
| 53 | |
| 54 | return new Promise((resolve) => { |
| 55 | const handler = (event) => { |
| 56 | if (event.data === "2") { |
| 57 | // ignore PING packets |
| 58 | return; |
| 59 | } |
| 60 | packets.push(event.data); |
| 61 | if (packets.length === count) { |
| 62 | socket.removeEventListener("message", handler); |
| 63 | resolve(packets); |
| 64 | } |
| 65 | }; |
| 66 | socket.addEventListener("message", handler); |
| 67 | }); |
| 68 | } |
| 69 | |
| 70 | async function initLongPollingSession() { |
| 71 | const response = await fetch(`${URL}/socket.io/?EIO=4&transport=polling`); |