wspromisify
Version:
Wraps your WebSockets into Promise-based class with full d.ts typings on client & server
30 lines (27 loc) • 770 B
text/typescript
import { createNew } from '../utils'
import mockServer from '../mock/server'
import { test } from '../suite'
/** Rejects messages by timout */
test('drops', () => new Promise(async (ff, rj) => {
const {port, shutDown} = await mockServer()
const ws = createNew({timeout: 500}, port)
await shutDown()
const lauchServ = async () => await mockServer(port)
setTimeout(async () => {
const msg = {echo: true, msg: 'hello!'}
let to: NodeJS.Timeout
try {
to = setTimeout(() => {
return rj()
}, 600)
await ws.send(msg)
if(to) clearTimeout(to)
await lauchServ()
return rj()
} catch(e) {
if(to!) clearTimeout(to)
await lauchServ()
return ff()
}
}, 200)
}))