mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-22 16:41:46 +03:00
WebSocket продолжаем
This commit is contained in:
parent
96507dfee3
commit
10e40035ee
2 changed files with 16 additions and 28 deletions
|
@ -0,0 +1,8 @@
|
|||
package ru.gravit.utils.websocket;
|
||||
|
||||
import ru.gravit.launcher.serialize.HInput;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface MessageInterface {
|
||||
void request(HInput input);
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package ru.gravit.utils.websocket;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
@ -15,6 +16,7 @@
|
|||
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
|
||||
import ru.gravit.launcher.Launcher;
|
||||
import ru.gravit.launcher.serialize.HInput;
|
||||
import ru.gravit.launcher.serialize.HOutput;
|
||||
import ru.gravit.utils.helper.IOHelper;
|
||||
|
||||
/**
|
||||
|
@ -26,10 +28,11 @@ public class WebSocketClient
|
|||
private final CountDownLatch closeLatch;
|
||||
@SuppressWarnings("unused")
|
||||
private Session session;
|
||||
|
||||
public WebSocketClient()
|
||||
private final MessageInterface adapter;
|
||||
public WebSocketClient(MessageInterface adapter)
|
||||
{
|
||||
this.closeLatch = new CountDownLatch(1);
|
||||
this.adapter = adapter;
|
||||
}
|
||||
|
||||
public boolean awaitClose(int duration, TimeUnit unit) throws InterruptedException
|
||||
|
@ -50,38 +53,15 @@ public void onConnect(Session session)
|
|||
{
|
||||
System.out.printf("Got connect: %s%n",session);
|
||||
this.session = session;
|
||||
try
|
||||
{
|
||||
Future<Void> fut;
|
||||
fut = session.getRemote().sendStringByFuture("Hello");
|
||||
fut.get(2,TimeUnit.SECONDS); // wait for send to complete.
|
||||
|
||||
fut = session.getRemote().sendStringByFuture("Thanks for the conversation.");
|
||||
fut.get(2,TimeUnit.SECONDS); // wait for send to complete.
|
||||
|
||||
session.close(StatusCode.NORMAL,"I'm done");
|
||||
}
|
||||
catch (Throwable t)
|
||||
{
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
public void request(ByteBuffer buffer) throws InterruptedException, ExecutionException, TimeoutException {
|
||||
Future<Void> fut;
|
||||
fut = session.getRemote().sendBytesByFuture(buffer);
|
||||
fut.get(2,TimeUnit.SECONDS);
|
||||
public void request(ByteBuffer buffer) throws IOException {
|
||||
session.getRemote().sendBytes(buffer);
|
||||
}
|
||||
@OnWebSocketMessage
|
||||
public void onMessage(String msg) throws IOException {
|
||||
byte[] bytes = msg.getBytes();
|
||||
InputStream stream = new ByteArrayInputStream(bytes);
|
||||
HInput input = new HInput(stream);
|
||||
long handshake = input.readLong();
|
||||
if(handshake != Launcher.PROTOCOL_MAGIC)
|
||||
{
|
||||
throw new IOException("INVALID HANDSHAKE");
|
||||
}
|
||||
int type = input.readInt();
|
||||
System.out.printf("Got msg: %l %d",handshake,type);
|
||||
adapter.request(input);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue