Package dev.relism.flash.websocket
Class WebSocketSession
java.lang.Object
dev.relism.flash.websocket.WebSocketSession
Per-connection WebSocket I/O state. One instance per virtual thread.
-
With
TCP_NODELAYenabled on the socket (set inHttpServer), Nagle's algorithm is disabled: the kernel sends data as soon as it lands in the send buffer, without waiting.BufferedOutputStreamwill still batch multiple small writes into one syscall when they happen in the same virtual-thread scheduling quantum — giving us coalescing where it's free, and immediate delivery where it matters.The only place an explicit flush is still needed is after the WS handshake (one-time, not on the hot path) and after the CLOSE frame (end of session). Both are handled in
close(int)and inHttpServer#performHandshake. - Flush on CLOSE frame:
close(int)still flushes explicitly because the CLOSE frame is the last thing written before the stream is abandoned — without a flush, the 4 bytes could sit in the buffer forever.
Thread safety
sendText(byte[], int, int), send(byte[], int, int), and close(int) are synchronized on
out and safe to call from threads other than the session loop.
close(int) uses a CAS on open to guarantee exactly-once CLOSE
frame emission under concurrent calls.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose(int code) Sends a CLOSE frame exactly once.intvoidcloseFromPeer(WebSocketFrame frame) voidecho(WebSocketFrame frame) voidbooleanisOpen()booleanreadFrame(WebSocketFrame frame) voidsend(byte[] payload, int off, int len) voidsendPong(WebSocketFrame ping) voidsendText(byte[] utf8, int off, int len)
-
Constructor Details
-
WebSocketSession
-
-
Method Details
-
isOpen
public boolean isOpen() -
closeCode
public int closeCode() -
sendText
- Throws:
IOException
-
send
- Throws:
IOException
-
close
Sends a CLOSE frame exactly once. No flush needed —outis the raw socket OutputStream (not buffered); each write goes directly to the kernel send buffer, and TCP_NODELAY ensures it's transmitted immediately.- Throws:
IOException
-
readFrame
- Throws:
IOException
-
sendPong
- Throws:
IOException
-
echo
- Throws:
IOException
-
closeFromPeer
-
forceClose
public void forceClose()
-