Fix WebSockets (hopefully 😭)

This commit is contained in:
PeytonPlayz595 2023-12-21 03:01:40 -05:00
parent 2206e940a4
commit 3cd22f664f
5 changed files with 3167 additions and 3061 deletions

6126
js/app.js

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -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;

View File

@ -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;
} }

View File

@ -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,51 +51,46 @@ 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); while (remaining > 0 && !byteQueue.isEmpty()) {
if (!event.isBubbles() && !event.isCancelable()) { byte[] currentByteArray = byteQueue.peek();
messageQueue.poll(); int currentPosition = position % currentByteArray.length;
dst.put(currentByteArray[currentPosition]);
bytesRead++;
position++;
remaining--;
if (currentPosition == currentByteArray.length - 1) {
byteQueue.poll();
} }
} }
private void readData(ByteBuffer buffer, MessageEvent event) { return bytesRead;
DataView data = DataView.create(event.getDataAsArray());
int length = Math.min(buffer.remaining(), data.getByteLength());
for (int i = 0; i < length; i++) {
buffer.put((byte) data.getInt8(i));
}
if (reading) {
return;
}
if (buffer.hasRemaining()) {
readBuffer.clear();
readBuffer.put(buffer);
reading = true;
} else {
buffer.flip();
}
} }
@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[] data = new byte[buffer.remaining()]; byte[] newByteArray = new byte[bytesWritten];
buffer.get(data); src.get(newByteArray);
Int8Array array = Int8Array.create(data.length); Int8Array array = Int8Array.create(newByteArray.length);
for (int i = 0; i < data.length; i++) { for (int i = 0; i < newByteArray.length; i++) {
array.set(i, data[i]); array.set(i, newByteArray[i]);
} }
nativeBinarySend(socket, array.getBuffer()); nativeBinarySend(socket, array.getBuffer());
} }
return bytesWritten;
}
public boolean connectionOpen() { public boolean connectionOpen() {
return isConnected && socket.getReadyState() != 3; return isConnected && socket.getReadyState() != 3;
} }