Created
September 19, 2023 13:17
-
-
Save tuhinpal/08227007b15016508223bdd53d4d2ec9 to your computer and use it in GitHub Desktop.
Next conf 2023, say hi (Browser Shell)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This need to be pasted fast (before ws connects!) | |
let queue = []; | |
let initial = true; | |
function handler(wsUrl) { | |
const ws = new WebSocket(wsUrl); | |
console.log("Type hi('msg') to send a message!"); | |
setInterval(() => { | |
if (queue.length) { | |
const data = queue.shift(); | |
ws.send( | |
JSON.stringify([ | |
{ | |
type: 100, | |
targetActor: -1, | |
data: { | |
cursor: null, | |
selectedPieceId: null, | |
name: data, | |
color: "var(--ds-purple-700)", | |
isHost: false, | |
}, | |
}, | |
{ type: 100, data: { cursor: { x: 0.1, y: 0.5 } } }, | |
]) | |
); | |
} | |
}, 100); | |
} | |
function init() { | |
const originalWebSocket = WebSocket; | |
window.WebSocket = function (...args) { | |
const instance = new originalWebSocket(...args); | |
if (initial) { | |
initial = false; | |
handler(instance.url); | |
window.WebSocket = originalWebSocket; | |
} | |
return instance; | |
}; | |
} | |
init(); | |
const hi = (msg) => queue.push(msg); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment