1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-29 18:49:07 -05:00
denoland-deno/tests/testdata/run/websocket_server_idletimeout.ts
Asher Gomez 7937ae3f2f
chore(net): soft-remove Deno.serveHttp() (#25451)
Towards #22079

---------

Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
2024-09-06 08:15:00 +10:00

30 lines
929 B
TypeScript

import { assertEquals } from "@std/assert";
const errorDeferred = Promise.withResolvers<void>();
const closeDeferred = Promise.withResolvers<void>();
const listener = Deno.listen({ port: 4509 });
console.log("READY");
// @ts-ignore `Deno.serveHttp()` was soft-removed in Deno 2.
const httpConn = Deno.serveHttp(await listener.accept());
const { request, respondWith } = (await httpConn.nextRequest())!;
const { response, socket } = Deno.upgradeWebSocket(request, {
idleTimeout: 1,
});
socket.onerror = (e) => {
console.log(e);
assertEquals((e as ErrorEvent).message, "No response from ping frame.");
errorDeferred.resolve();
};
socket.onclose = (e) => {
console.log(e);
assertEquals(e.reason, "No response from ping frame.");
closeDeferred.resolve();
};
await respondWith(response);
await errorDeferred.promise;
await closeDeferred.promise;
// TODO(mmastrac): this doesn't exit on its own. Why?
Deno.exit(123);