Class ConnectionHandler
- java.lang.Object
-
- page.codeberg.terratactician_expandoria.bots.ConnectionHandler
-
public class ConnectionHandler extends java.lang.Object
Handles the connection with a game. Can be used to receive or send messages. Receiving messages works with registered Events.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
ConnectionHandler.EventCallback
Interface for callable thinks.static class
ConnectionHandler.EventRef
EventRef is a smart reference for a specific registered event.
-
Constructor Summary
Constructors Constructor Description ConnectionHandler(org.java_websocket.WebSocket ws)
Creates a new ConnectionHandler, with an open and validated WebSocket
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
connectionClosed()
Notifies the ConnectionHandler, that the Game connection was closed.ConnectionHandler.EventRef
listen(Event.EventType event, ConnectionHandler.EventCallback callback)
Listen to an Event of the game.boolean
receive(Event msg)
Message received from ws, for in game.ConnectionHandler.EventRef
register(Event.EventType event, ConnectionHandler.EventCallback callback)
Register an callback to an Event by the Game.void
send(Action ac)
Send action to Game.void
send(ActionsList ac)
Send actions to Game.void
unregister()
Unregister all Events.void
unregister(ConnectionHandler.EventRef ref)
Unregister a specific Event.void
unregister(Event.EventType event)
Unregister all callbacks of a specific event type.
-
-
-
Method Detail
-
register
public ConnectionHandler.EventRef register(Event.EventType event, ConnectionHandler.EventCallback callback)
Register an callback to an Event by the Game. This method will automatically send the registration action.- Parameters:
event
- event type to registercallback
- callback to call on event- Returns:
- EventRef for unregistering
-
listen
public ConnectionHandler.EventRef listen(Event.EventType event, ConnectionHandler.EventCallback callback)
Listen to an Event of the game. This method WILL NOT send an registration action. This action is intended to use for listening to events that are not requested. Useregister
if you are not sure this method is the right one.- Parameters:
event
- event type to registercallback
- callback to call on event- Returns:
- EventRef for unregistering
-
unregister
public void unregister()
Unregister all Events.
-
unregister
public void unregister(Event.EventType event)
Unregister all callbacks of a specific event type.- Parameters:
event
- EventRef returned while registering.
-
unregister
public void unregister(ConnectionHandler.EventRef ref)
Unregister a specific Event.- Parameters:
ref
- EventRef returned while registering.
-
send
public void send(ActionsList ac) throws org.java_websocket.exceptions.WebsocketNotConnectedException
Send actions to Game.- Parameters:
ac
- action to send- Throws:
org.java_websocket.exceptions.WebsocketNotConnectedException
- gets thrown when action limit is exceeded
-
send
public void send(Action ac) throws org.java_websocket.exceptions.WebsocketNotConnectedException
Send action to Game.- Parameters:
ac
- action to send- Throws:
org.java_websocket.exceptions.WebsocketNotConnectedException
- gets thrown when action limit is exceeded
-
receive
public boolean receive(Event msg)
Message received from ws, for in game.- Parameters:
msg
- pure String received- Returns:
- if the game is still running
-
connectionClosed
public void connectionClosed()
Notifies the ConnectionHandler, that the Game connection was closed.
-
-