Skip to content

Instantly share code, notes, and snippets.

@stavalfi
Created October 1, 2020 08:23
Show Gist options
  • Save stavalfi/4ad0273f6e41234b3daaea51d14348c1 to your computer and use it in GitHub Desktop.
Save stavalfi/4ad0273f6e41234b3daaea51d14348c1 to your computer and use it in GitHub Desktop.
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