Question:
So, im trying to make thing, that will send data from my laptop to the android phone. First of all im trying to connect to the server, i made server using Python, then, i made java class, that had to connect to the server, but when im trying - i get “Handshake response not received.” after few seconds of trying connection. What am i doing wrong?!
Repl link:
idk what meant by repl link, so i’ll put this https://gameplaytogether.akkdliasierva.repl.co
import asyncio
import websockets
async def server(websocket, path):
while True:
message = await websocket.recv()
print(f"Received message: {message}")
response = f"Hello from the server! You said: {message}"
await websocket.send(response)
start_server = websockets.serve(server, "0.0.0.0", 8000)
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(start_server)
loop.run_forever()
import org.json.JSONObject;
import javax.websocket.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.CountDownLatch;
@ClientEndpoint
public class ServerConnection {
private static CountDownLatch latch;
private String url;
public ServerConnection(String url)
{
this.url = url;
}
@OnOpen
public void onOpen(Session session) {
System.out.println("Connected to server");
}
@OnMessage
public void onMessage(String message) {
System.out.println("Received message: " + message);
}
@OnClose
public void onClose(Session session, CloseReason closeReason) {
System.out.println("Session closed: " + closeReason.getReasonPhrase());
latch.countDown();
}
public boolean start()
{
latch = new CountDownLatch(1);
try {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
String uri = "wss://gameplaytogether.akkdliasierva.repl.co";// did it for debug
container.connectToServer(this, URI.create(uri));
latch.await();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}