import * as net from "./net" ; import { prot } from "./protocol" ; import * as ui from "./ui" ; import * as util from "./util" ; import { dce } from "./util" ; export function recvChat ( text ) { ui . ui . chat . wrapper . style . display = "" ; const line = dce ( "div" ) ; line . innerText = text ; ui . ui . chat . incoming . appendChild ( line ) ; ui . ui . chat . incoming . scroll ( 0 , 1000000 ) ; } util . netEvent ( "data" , "text" , function ( ev ) { const msg = ev . detail ; const p = prot . parts . text ; const text = util . decodeText ( msg . buffer . slice ( p . text ) ) ; recvChat ( text ) ; } ) ; function sendChat ( text ) { const textBuf = util . encodeText ( text ) ; const p = prot . parts . text ; const out = new DataView ( new ArrayBuffer ( p . length + textBuf . length ) ) ; out . setUint32 ( 0 , prot . ids . text , true ) ; out . setUint32 ( p . reserved , 0 , true ) ; new Uint8Array ( out . buffer ) . set ( textBuf , p . text ) ; net . dataSock . send ( out . buffer ) ; } export function mkChatBox ( ) { const chat = ui . ui . chat ; const outgoing = chat . outgoing ; function handleOutgoing ( ) { sendChat ( outgoing . value ) ; recvChat ( "You: " + outgoing . value ) ; outgoing . value = "" ; } chat . outgoingB . onclick = handleOutgoing ; }