Package dev.gmitch215.bytebox.socket
Class Socket
java.lang.Object
dev.gmitch215.bytebox.socket.Socket
- All Implemented Interfaces:
AutoCloseable
One TCP connection.
Reads and writes block. Closing is what releases the connection, and the six-simultaneous-
connection limit every plan has makes that matter: a socket left open holds a sixth of the
invocation's budget. This is AutoCloseable so try-with-resources can do it.
try (Socket socket = Sockets.connectTLS("example.com", 443)) {
socket.write("GET / HTTP/1.0\r\nHost: example.com\r\n\r\n");
System.out.println(socket.readAll());
}
- Since:
- 1.0.0
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()org.teavm.jso.core.JSPromise<org.teavm.jso.JSObject> closed()Returns a promise that settles when the connection closes, forctx.waitUntil.read()Reads whatever has arrived, up to the next chunk boundary.readAll()Reads until the peer closes the connection.Reads until a delimiter appears, which is how a line-oriented protocol is framed.startTLS()Upgrades a connection opened withSockets.connectStartTLS(String, int)to TLS.org.teavm.jso.JSObjectunwrap()Returns the platform's own socket object, for anything this surface does not cover.voidWrites text, UTF-8 encoded.
-
Method Details
-
write
Writes text, UTF-8 encoded.- Parameters:
text- the text
-
read
Reads whatever has arrived, up to the next chunk boundary.- Returns:
- the text, or
nullat end of stream
-
readAll
Reads until the peer closes the connection.- Returns:
- everything that arrived
-
readUntil
Reads until a delimiter appears, which is how a line-oriented protocol is framed.- Parameters:
delimiter- the delimiter, such as"\r\n"- Returns:
- the text up to but not including the delimiter, or
nullat end of stream
-
startTLS
Upgrades a connection opened withSockets.connectStartTLS(String, int)to TLS.Returns a new socket. The original is unusable afterwards, which is why the result has to be kept rather than discarded.
- Returns:
- the encrypted socket
-
closed
public org.teavm.jso.core.JSPromise<org.teavm.jso.JSObject> closed()Returns a promise that settles when the connection closes, forctx.waitUntil.- Returns:
- a promise that settles when the connection closes, for
ctx.waitUntil
-
unwrap
public org.teavm.jso.JSObject unwrap()Returns the platform's own socket object, for anything this surface does not cover.- Returns:
- the platform's own socket object, for anything this surface does not cover
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-