Fix WebSockets (hopefully 😭)
This commit is contained in:
parent
2206e940a4
commit
3cd22f664f
File diff suppressed because one or more lines are too long
|
@ -69,7 +69,6 @@ public final class Minecraft {
|
||||||
public NetworkManager networkManager;
|
public NetworkManager networkManager;
|
||||||
public MovingObjectPosition selected;
|
public MovingObjectPosition selected;
|
||||||
public static GameSettings settings;
|
public static GameSettings settings;
|
||||||
public String server;
|
|
||||||
volatile boolean running;
|
volatile boolean running;
|
||||||
public String debug;
|
public String debug;
|
||||||
public boolean hasMouse;
|
public boolean hasMouse;
|
||||||
|
@ -88,7 +87,6 @@ public final class Minecraft {
|
||||||
this.online = false;
|
this.online = false;
|
||||||
new HumanoidModel(0.0F);
|
new HumanoidModel(0.0F);
|
||||||
this.selected = null;
|
this.selected = null;
|
||||||
this.server = null;
|
|
||||||
this.running = false;
|
this.running = false;
|
||||||
this.debug = "";
|
this.debug = "";
|
||||||
this.hasMouse = false;
|
this.hasMouse = false;
|
||||||
|
@ -171,9 +169,6 @@ public final class Minecraft {
|
||||||
this.particleManager = new ParticleManager(this.level);
|
this.particleManager = new ParticleManager(this.level);
|
||||||
checkGLError("Post startup");
|
checkGLError("Post startup");
|
||||||
this.hud = new HUDScreen(this, this.width, this.height);
|
this.hud = new HUDScreen(this, this.width, this.height);
|
||||||
if(this.server != null) {
|
|
||||||
this.networkManager = new NetworkManager(this, this.server, this.session.username, this.session.mppass);
|
|
||||||
}
|
|
||||||
|
|
||||||
long var13 = System.currentTimeMillis();
|
long var13 = System.currentTimeMillis();
|
||||||
int var15 = 0;
|
int var15 = 0;
|
||||||
|
|
|
@ -109,7 +109,7 @@ public class LevelUtils {
|
||||||
|
|
||||||
public void save() {
|
public void save() {
|
||||||
|
|
||||||
if(Minecraft.getMinecraft().server != null || Minecraft.getMinecraft().networkManager != null || Minecraft.getMinecraft().level == null) {
|
if(Minecraft.getMinecraft().networkManager != null || Minecraft.getMinecraft().level == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,30 +15,34 @@ import java.util.Queue;
|
||||||
|
|
||||||
public class WebSocketChannel {
|
public class WebSocketChannel {
|
||||||
private final WebSocket socket;
|
private final WebSocket socket;
|
||||||
private final Queue<MessageEvent> messageQueue;
|
|
||||||
private final ByteBuffer readBuffer;
|
|
||||||
private boolean reading;
|
|
||||||
private boolean isConnected = false;
|
private boolean isConnected = false;
|
||||||
|
private Queue<byte[]> byteQueue;
|
||||||
|
|
||||||
public WebSocketChannel(String url) {
|
public WebSocketChannel(String url) {
|
||||||
socket = WebSocket.create(url);
|
socket = WebSocket.create(url);
|
||||||
messageQueue = new LinkedList<>();
|
byteQueue = new LinkedList<byte[]>();
|
||||||
readBuffer = ByteBuffer.allocate(4096);
|
|
||||||
reading = false;
|
|
||||||
|
|
||||||
socket.onMessage(event -> {
|
socket.onMessage(event -> {
|
||||||
isConnected = true;
|
ArrayBuffer arrayBuffer = event.getData().cast();
|
||||||
messageQueue.offer(event);
|
Int8Array int8Array = Int8Array.create(arrayBuffer);
|
||||||
|
ByteBuffer byteBuffer = ByteBuffer.allocate(int8Array.getLength());
|
||||||
|
for (int i = 0; i < int8Array.getLength(); i++) {
|
||||||
|
byteBuffer.put(i, (byte) int8Array.get(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] data = new byte[byteBuffer.remaining()];
|
||||||
|
byteBuffer.get(data);
|
||||||
|
byteQueue.offer(data);
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.onOpen(event -> {
|
socket.onOpen(event -> {
|
||||||
isConnected = true;
|
isConnected = true;
|
||||||
messageQueue.clear();
|
byteQueue.clear();
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.onClose((event) -> {
|
socket.onClose((event) -> {
|
||||||
isConnected = false;
|
isConnected = false;
|
||||||
messageQueue.clear();
|
byteQueue.clear();
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.onError((event) -> {
|
socket.onError((event) -> {
|
||||||
|
@ -47,49 +51,44 @@ public class WebSocketChannel {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void read(ByteBuffer buffer) {
|
public int read(ByteBuffer dst) {
|
||||||
if (reading || messageQueue.isEmpty()) {
|
int bytesRead = 0;
|
||||||
return;
|
int remaining = dst.remaining();
|
||||||
}
|
int position = 0;
|
||||||
MessageEvent event = messageQueue.peek();
|
|
||||||
readData(buffer, event);
|
|
||||||
if (!event.isBubbles() && !event.isCancelable()) {
|
|
||||||
messageQueue.poll();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void readData(ByteBuffer buffer, MessageEvent event) {
|
while (remaining > 0 && !byteQueue.isEmpty()) {
|
||||||
DataView data = DataView.create(event.getDataAsArray());
|
byte[] currentByteArray = byteQueue.peek();
|
||||||
int length = Math.min(buffer.remaining(), data.getByteLength());
|
int currentPosition = position % currentByteArray.length;
|
||||||
for (int i = 0; i < length; i++) {
|
dst.put(currentByteArray[currentPosition]);
|
||||||
buffer.put((byte) data.getInt8(i));
|
bytesRead++;
|
||||||
}
|
position++;
|
||||||
if (reading) {
|
remaining--;
|
||||||
return;
|
|
||||||
}
|
if (currentPosition == currentByteArray.length - 1) {
|
||||||
if (buffer.hasRemaining()) {
|
byteQueue.poll();
|
||||||
readBuffer.clear();
|
}
|
||||||
readBuffer.put(buffer);
|
|
||||||
reading = true;
|
|
||||||
} else {
|
|
||||||
buffer.flip();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return bytesRead;
|
||||||
}
|
}
|
||||||
|
|
||||||
@JSBody(params = { "sock", "buffer" }, script = "sock.send(buffer);")
|
@JSBody(params = { "sock", "buffer" }, script = "sock.send(buffer);")
|
||||||
private static native void nativeBinarySend(WebSocket sock, ArrayBuffer buffer);
|
private static native void nativeBinarySend(WebSocket sock, ArrayBuffer buffer);
|
||||||
|
|
||||||
public void write(ByteBuffer buffer) {
|
public int write(ByteBuffer src) {
|
||||||
if (socket.getReadyState() == 3) {
|
int bytesWritten = src.remaining();
|
||||||
return;
|
|
||||||
|
if (bytesWritten > 0) {
|
||||||
|
byte[] newByteArray = new byte[bytesWritten];
|
||||||
|
src.get(newByteArray);
|
||||||
|
Int8Array array = Int8Array.create(newByteArray.length);
|
||||||
|
for (int i = 0; i < newByteArray.length; i++) {
|
||||||
|
array.set(i, newByteArray[i]);
|
||||||
|
}
|
||||||
|
nativeBinarySend(socket, array.getBuffer());
|
||||||
}
|
}
|
||||||
byte[] data = new byte[buffer.remaining()];
|
|
||||||
buffer.get(data);
|
return bytesWritten;
|
||||||
Int8Array array = Int8Array.create(data.length);
|
|
||||||
for (int i = 0; i < data.length; i++) {
|
|
||||||
array.set(i, data[i]);
|
|
||||||
}
|
|
||||||
nativeBinarySend(socket, array.getBuffer());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean connectionOpen() {
|
public boolean connectionOpen() {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user