Package rosequartz.net
Class WebSocket
java.lang.Object
rosequartz.net.WebSocket
Represents a socket using the WebSocket protocol.
Sockets are used to communicate between computers.
This type of socket is supported on all build targets.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classRepresents the current state of the socket. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaccept(int port)Launches a new thread and accepts one connection from any socket on the given port.acceptBlocking(int port)Accepts one connection from any socket on the given port.close()Closes the socket, ending its current connection.Launches a new thread and connects this socket to another socket given its full url.connectBlocking(String url)Connects this socket to another socket given its full url, blocking this thread until a connection is established.getState()Gets the current state of the socket.protected voidMethod called when the socket is closed.Set a function to be called when the socket is closed.protected voidMethod called when a connection is established.Set a function to be called when the socket is connected.protected voidMethod called when a message is received.Set a function to be called when a message is received.Sends a message to the connected socket.
-
Constructor Details
-
WebSocket
public WebSocket()Creates a new WebSocket.
-
-
Method Details
-
connect
Launches a new thread and connects this socket to another socket given its full url.- Parameters:
url- the full url of the socket to connect to- Returns:
- this
- Throws:
IllegalStateException- if the socket is not closed
-
connectBlocking
Connects this socket to another socket given its full url, blocking this thread until a connection is established.- Parameters:
url- the full url of the socket to connect to- Returns:
- this
- Throws:
IllegalStateException- if the socket is not closed
-
accept
Launches a new thread and accepts one connection from any socket on the given port. To handle multiple connections to sockets from the same port, create a new socket with the same port.
WARNING! WebSocket server-behavior is not supported by the browser runtime. The browser-runtime will throw a 'PlatformSpecificUnsupportedException'.- Parameters:
port- port to listen on- Returns:
- this
- Throws:
IllegalStateException- if the socket is not closedPlatformSpecificUnsupportedException- if called in browser build target
-
acceptBlocking
Accepts one connection from any socket on the given port. To handle multiple connections to sockets from the same port, create a new socket with the same port.
WARNING! WebSocket server-behavior is not supported by the browser runtime. The browser-runtime will throw a 'PlatformSpecificUnsupportedException'.- Parameters:
port- port to listen on- Returns:
- this
- Throws:
IllegalStateException- if the socket is not closedPlatformSpecificUnsupportedException- if called in browser build target
-
onConnect
Set a function to be called when the socket is connected.- Parameters:
action- function to call on socket connection- Returns:
- this
-
onConnect
protected void onConnect()Method called when a connection is established. By default, this method calls the configured function by 'onConnect(Runnable action)', but it may be overridden. -
onMessage
Set a function to be called when a message is received.- Parameters:
action- function to call on message receive- Returns:
- this
-
onMessage
Method called when a message is received. By default, this method calls the configured function by 'onMessage(Consumeraction)', but it may be overridden. -
onClose
Set a function to be called when the socket is closed.- Parameters:
action- function to call on socket close- Returns:
- this
-
onClose
Method called when the socket is closed. By default, this method calls the configured function by 'onClose(Consumeraction)', but it may be overridden. -
send
Sends a message to the connected socket.- Parameters:
message- message to send- Returns:
- this
- Throws:
IllegalStateException- if the socket is not connected
-
close
Closes the socket, ending its current connection.- Returns:
- this
- Throws:
IllegalStateException- if the socket is closed
-
getState
Gets the current state of the socket.- Returns:
- state
-