Created
October 1, 2020 08:23
-
-
Save stavalfi/4ad0273f6e41234b3daaea51d14348c1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import WebSocket from 'ws' | |
function throttle<T>(callback: (message: T) => Promise<void>, interval = 1000): (message: T) => Promise<void> { | |
return (message: T) => Promise.resolve() | |
} | |
interface Message { | |
type: string | |
content: string | |
} | |
async function processMessage(m: Message) { | |
console.log(m) | |
} | |
async function main() { | |
const ws1 = new WebSocket('wss://echo.websocket.org') | |
await new Promise(res => { | |
ws1.onopen = res | |
}) | |
ws1.onmessage = async x => { | |
if (typeof x.data === 'string') { | |
const message: Message = JSON.parse(x.data) | |
} | |
} | |
ws1.send(JSON.stringify({ type: '1', content: 'content1' })) | |
ws1.send(JSON.stringify({ type: '1', content: 'content2' })) | |
ws1.send(JSON.stringify({ type: '2', content: 'content3' })) | |
ws1.send(JSON.stringify({ type: '2', content: 'content4' })) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment