use of com.esotericsoftware.kryonet.Listener.ConnectionListener in project ProjektGG by eskalon.
the class BaseGameServer method start.
/**
* Sets up a server asynchronously. After it is finished the callback is
* informed.
*
* @param callback
* the callback that is informed when the server is started.
*/
public void start(ISuccessCallback callback) {
Preconditions.checkNotNull(callback, "callback cannot be null.");
Log.info("Server", "--- Server is starting ---");
this.server = new Server();
this.server.start();
// ON NEW CONNECTION & ON DICONNECTED
this.server.addListener(new ConnectionListener() {
@Override
public void connected(Connection con) {
onClientConnected(con);
}
@Override
public void disconnected(Connection con) {
onClientDisconnected(con);
}
});
TypeListener typeListener = new TypeListener();
typeListener.addTypeHandler(ClientHandshakeRequest.class, (con, msg) -> onClientHandshake(con, msg));
server.addListener(typeListener);
onCreation();
ThreadHandler.getInstance().executeRunnable(() -> {
try {
// Start the server
server.bind(serverSetup.getPort());
Log.info("Server", "Server started");
// Create & start the broadcast server
if (serverSetup.isPublic()) {
startBroadcastServer();
}
// Host successfully started
callback.onSuccess(null);
} catch (IOException | IllegalArgumentException e2) {
Log.error("Server", "Server could not be started: %s", e2);
// Something went wrong
callback.onFailure(e2);
}
});
}
Aggregations