mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-11-15 03:31:15 +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;
|
package ru.gravit.utils.websocket;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
@ -15,6 +16,7 @@
|
||||||
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
|
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
|
||||||
import ru.gravit.launcher.Launcher;
|
import ru.gravit.launcher.Launcher;
|
||||||
import ru.gravit.launcher.serialize.HInput;
|
import ru.gravit.launcher.serialize.HInput;
|
||||||
|
import ru.gravit.launcher.serialize.HOutput;
|
||||||
import ru.gravit.utils.helper.IOHelper;
|
import ru.gravit.utils.helper.IOHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -26,10 +28,11 @@ public class WebSocketClient
|
||||||
private final CountDownLatch closeLatch;
|
private final CountDownLatch closeLatch;
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
private Session session;
|
private Session session;
|
||||||
|
private final MessageInterface adapter;
|
||||||
public WebSocketClient()
|
public WebSocketClient(MessageInterface adapter)
|
||||||
{
|
{
|
||||||
this.closeLatch = new CountDownLatch(1);
|
this.closeLatch = new CountDownLatch(1);
|
||||||
|
this.adapter = adapter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean awaitClose(int duration, TimeUnit unit) throws InterruptedException
|
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);
|
System.out.printf("Got connect: %s%n",session);
|
||||||
this.session = 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 {
|
public void request(ByteBuffer buffer) throws IOException {
|
||||||
Future<Void> fut;
|
session.getRemote().sendBytes(buffer);
|
||||||
fut = session.getRemote().sendBytesByFuture(buffer);
|
|
||||||
fut.get(2,TimeUnit.SECONDS);
|
|
||||||
}
|
}
|
||||||
@OnWebSocketMessage
|
@OnWebSocketMessage
|
||||||
public void onMessage(String msg) throws IOException {
|
public void onMessage(String msg) throws IOException {
|
||||||
byte[] bytes = msg.getBytes();
|
byte[] bytes = msg.getBytes();
|
||||||
InputStream stream = new ByteArrayInputStream(bytes);
|
InputStream stream = new ByteArrayInputStream(bytes);
|
||||||
HInput input = new HInput(stream);
|
HInput input = new HInput(stream);
|
||||||
long handshake = input.readLong();
|
adapter.request(input);
|
||||||
if(handshake != Launcher.PROTOCOL_MAGIC)
|
|
||||||
{
|
|
||||||
throw new IOException("INVALID HANDSHAKE");
|
|
||||||
}
|
|
||||||
int type = input.readInt();
|
|
||||||
System.out.printf("Got msg: %l %d",handshake,type);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue