import { WebSocket , acceptWebSocket , isWebSocketCloseEvent , acceptable , isWebSocketPingEvent } from 'https://deno.land/std/ws/mod.ts' ; import { serve } from "https://deno.land/std/http/server.ts" ; if ( import . meta . main ) { /** websocket echo server */ const port = Deno . args [ 0 ] || "8080" ; console . log ( `websocket server is running on :${ port }` ) ; for await ( const req of serve ( `:${ port }` ) ) { const { conn , r : bufReader , w : bufWriter , headers } = req ; console . log ( req . headers . get ( 'Upgrade' ) ) ; try { const sock = await acceptWebSocket ( { conn , bufReader , bufWriter , headers } ) ; console . log ( "socket connected!" ) ; try { for await ( const ev of sock ) { console . log ( ev ) ; if ( typeof ev === "string" ) { console . log ( "ws:Text" , ev ) ; await sock . send ( ev ) ; } else if ( ev instanceof Uint8Array ) { console . log ( "ws:Binary" , ev ) ; } else if ( isWebSocketPingEvent ( ev ) ) { const [ , body ] = ev ; console . log ( "ws:Ping" , body ) ; } else if ( isWebSocketCloseEvent ( ev ) ) { const { code , reason } = ev ; console . log ( "ws:Close" , code , reason ) ; } } } catch ( err ) { console . error ( `failed to receive frame: ${ err }` ) ; if ( ! sock . isClosed ) { await sock . close ( 1000 ) . catch ( console . error ) ; } } } catch ( err ) { console . error ( `failed to accept websocket: ${ err }` ) ; await req . respond ( { status : 400 } ) ; } } }