[FEATURE] Восстановление WebSockets соеденения при разрыве

This commit is contained in:
Gravit 2019-04-04 15:59:41 +07:00
parent 30645741f4
commit 0b0a99966b
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
3 changed files with 55 additions and 0 deletions

View file

@ -13,6 +13,7 @@
import ru.gravit.launcher.request.Request; import ru.gravit.launcher.request.Request;
import ru.gravit.launcher.request.auth.RestoreSessionRequest; import ru.gravit.launcher.request.auth.RestoreSessionRequest;
import ru.gravit.launcher.request.update.LegacyLauncherRequest; import ru.gravit.launcher.request.update.LegacyLauncherRequest;
import ru.gravit.launcher.request.websockets.LegacyRequestBridge;
import ru.gravit.launcher.serialize.HInput; import ru.gravit.launcher.serialize.HInput;
import ru.gravit.launcher.serialize.HOutput; import ru.gravit.launcher.serialize.HOutput;
import ru.gravit.launcher.serialize.stream.StreamObject; import ru.gravit.launcher.serialize.stream.StreamObject;
@ -472,6 +473,22 @@ public static void main(String... args) throws Throwable {
{ {
RestoreSessionRequest request = new RestoreSessionRequest(Request.getSession()); RestoreSessionRequest request = new RestoreSessionRequest(Request.getSession());
request.request(); request.request();
LegacyRequestBridge.service.reconnectCallback = () ->
{
LogHelper.debug("WebSocket connect closed. Try reconnect");
try {
if (!LegacyRequestBridge.service.reconnectBlocking()) LogHelper.error("Error connecting");
LogHelper.debug("Connect to %s", Launcher.getConfig().nettyAddress);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
RestoreSessionRequest request1 = new RestoreSessionRequest(Request.getSession());
request1.request();
} catch (Exception e) {
LogHelper.error(e);
}
};
} }
LogHelper.debug("Starting JVM and client WatchService"); LogHelper.debug("Starting JVM and client WatchService");
FileNameMatcher assetMatcher = profile.getAssetUpdateMatcher(); FileNameMatcher assetMatcher = profile.getAssetUpdateMatcher();

View file

@ -18,6 +18,8 @@
public class ClientWebSocketService extends ClientJSONPoint { public class ClientWebSocketService extends ClientJSONPoint {
public final GsonBuilder gsonBuilder; public final GsonBuilder gsonBuilder;
public final Gson gson; public final Gson gson;
public OnCloseCallback onCloseCallback;
public ReconnectCallback reconnectCallback;
private HashMap<String, Class<? extends RequestInterface>> requests; private HashMap<String, Class<? extends RequestInterface>> requests;
private HashMap<String, Class<? extends ResultInterface>> results; private HashMap<String, Class<? extends ResultInterface>> results;
private HashSet<EventHandler> handlers; private HashSet<EventHandler> handlers;
@ -57,6 +59,23 @@ public void onError(Exception e) {
LogHelper.error(e); LogHelper.error(e);
} }
@Override
public void onClose(int code, String reason, boolean remote)
{
LogHelper.debug("Disconnected: " + code + " " + remote + " " + (reason != null ? reason : "no reason"));
if(onCloseCallback != null)
onCloseCallback.onClose(code, reason, remote);
}
@FunctionalInterface
public interface OnCloseCallback
{
void onClose(int code, String reason, boolean remote);
}
public interface ReconnectCallback
{
void onReconnect() throws IOException;
}
public Class<? extends RequestInterface> getRequestClass(String key) { public Class<? extends RequestInterface> getRequestClass(String key) {
return requests.get(key); return requests.get(key);
} }
@ -99,10 +118,14 @@ public void registerHandler(EventHandler eventHandler) {
} }
public void sendObject(Object obj) throws IOException { public void sendObject(Object obj) throws IOException {
if(isClosed() && reconnectCallback != null)
reconnectCallback.onReconnect();
send(gson.toJson(obj, RequestInterface.class)); send(gson.toJson(obj, RequestInterface.class));
} }
public void sendObject(Object obj, Type type) throws IOException { public void sendObject(Object obj, Type type) throws IOException {
if(isClosed() && reconnectCallback != null)
reconnectCallback.onReconnect();
send(gson.toJson(obj, type)); send(gson.toJson(obj, type));
} }

View file

@ -10,6 +10,7 @@
import ru.gravit.launcher.request.auth.AuthRequest; import ru.gravit.launcher.request.auth.AuthRequest;
import ru.gravit.launcher.request.auth.AuthServerRequest; import ru.gravit.launcher.request.auth.AuthServerRequest;
import ru.gravit.launcher.request.update.ProfilesRequest; import ru.gravit.launcher.request.update.ProfilesRequest;
import ru.gravit.launcher.request.websockets.LegacyRequestBridge;
import ru.gravit.launcher.server.setup.ServerWrapperSetup; import ru.gravit.launcher.server.setup.ServerWrapperSetup;
import ru.gravit.utils.PublicURLClassLoader; import ru.gravit.utils.PublicURLClassLoader;
import ru.gravit.utils.config.JsonConfigurable; import ru.gravit.utils.config.JsonConfigurable;
@ -169,6 +170,20 @@ public void run(String... args) throws Throwable {
else mainClass = Class.forName(classname); else mainClass = Class.forName(classname);
MethodHandle mainMethod = MethodHandles.publicLookup().findStatic(mainClass, "main", MethodType.methodType(void.class, String[].class)); MethodHandle mainMethod = MethodHandles.publicLookup().findStatic(mainClass, "main", MethodType.methodType(void.class, String[].class));
modulesManager.postInitModules(); modulesManager.postInitModules();
if(config.websocket.enabled)
{
LegacyRequestBridge.service.reconnectCallback = () ->
{
LogHelper.debug("WebSocket connect closed. Try reconnect");
try {
if (!LegacyRequestBridge.service.reconnectBlocking()) LogHelper.error("Error connecting");
LogHelper.debug("Connect to %s", config.websocket.address);
} catch (InterruptedException e) {
e.printStackTrace();
}
auth();
};
}
LogHelper.info("ServerWrapper: Project %s, LaunchServer address: %s port %d. Title: %s", config.projectname, config.address, config.port, config.title); LogHelper.info("ServerWrapper: Project %s, LaunchServer address: %s port %d. Title: %s", config.projectname, config.address, config.port, config.title);
LogHelper.info("Minecraft Version (for profile): %s", wrapper.profile == null ? "unknown" : wrapper.profile.getVersion().name); LogHelper.info("Minecraft Version (for profile): %s", wrapper.profile == null ? "unknown" : wrapper.profile.getVersion().name);
LogHelper.info("Start Minecraft Server"); LogHelper.info("Start Minecraft Server");