I'm working with a game called "Pixels". My goal is to programmatically monitor when trees re-spawn within the game. A developer provided me with Java code for this, and I've attempted to convert it to Python. I'm able to successfully retrieve my session ID, room ID, and other relevant server details.When I attempt to establish a WebSocket connection, I consistently receive a 502 error. I'm not sure what's causing this.
public void openWebSocketAndProcessMessages(String server, String roomId, String sessionId) {
ReactorNettyWebSocketClient client = new ReactorNettyWebSocketClient();
client.setMaxFramePayloadLength(2621440);
URI uri = URI.create(
String.format("wss://pixels-server.pixels.xyz/%s/%s?sessionId=%s", server, roomId,
sessionId));
AtomicBoolean firstMessageReceived = new AtomicBoolean(false);
// Capture the start time of the session
Instant sessionStart = Instant.now();
client.execute(uri, session ->
session.receive()
.doOnNext(message -> {
// Check if the first message has been received and send a confirmation message
if (firstMessageReceived.compareAndSet(false, true)) {
System.out.println("First message received, sending confirmation...");
byte[] confirmationBytes = {'\n'}; // Newline character as the confirmation message
session.send(
Mono.just(session.binaryMessage(factory -> factory.wrap(confirmationBytes))))
.subscribe();
}
// Convert the received WebSocketMessage to a byte array (requires proper handling based on actual message type and content)
ByteBuffer buffer = message.getPayload().asByteBuffer();
byte[] messageBytes = new byte[buffer.remaining()];
buffer.get(messageBytes);
String encodedString = new String(Base64.getDecoder().decode(Base64.getEncoder().encodeToString(messageBytes)));
}
Websocket connection python code that I am getting 502 error:
async def connect_websocket(server, roomid, sessionid):
uri = f"wss://pixels-server.pixels.xyz/{server}/{roomid}?sessionId={sessionid}"
print(uri)
headers = {
"Host": "pixels-server.pixels.xyz",
"Connection": "Upgrade",
"Upgrade": "websocket",
"Origin": "https://play.pixels.xyz",
"Sec-WebSocket-Version": "13",
"Sec-WebSocket-Key": "lAVNhobAP9AaShcdp/BegA==",
"Sec-WebSocket-Extensions": "permessage-deflate; client_max_window_bits",
"User-Agent": "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Mobile Safari/537.36",
"Pragma": "no-cache",
"Cache-Control": "no-cache",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Accept-Language": "en-GB,en-US;q=0.9,en;q=0.8",
"Cookie": "intercom-id-grzhbhyr=e4b3442d-7fbd-4660-b003-9796901fb523; intercom-device-id-grzhbhyr=48118693-0661-4e43-b3b9-e140ae8a692d; _ga=GA1.1.523002009.1710326782; _ga_QGP383C71B=GS1.1.1711873973.8.1.1711874126.0.0.0"
}
async with websockets.connect(uri, extra_headers=headers) as websocket:
while True:
message = await websocket.recv()
print(f"Received message: {message}
")
I'm unsure if there are any specific authentication requirements for the WebSocket connection that I might be missing.