Created
September 5, 2018 20:58
-
-
Save marci4/a4b35949264ea3f59e37fba5c014420c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.java_websocket.client.WebSocketClient; | |
import org.java_websocket.handshake.ServerHandshake; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
public class PingClient extends WebSocketClient { | |
public PingClient(URI uri) { | |
super(uri); | |
} | |
@Override | |
public void onOpen(ServerHandshake handshakedata) { | |
send(Long.toString(System.currentTimeMillis())); | |
} | |
@Override | |
public void onMessage(String message) { | |
long current = System.currentTimeMillis(); | |
try { | |
long client = Long.parseLong(message); | |
System.out.println(current-client); | |
} catch (Exception e) { | |
//To handle | |
} | |
} | |
@Override | |
public void onClose(int code, String reason, boolean remote) { | |
} | |
@Override | |
public void onError(Exception ex) { | |
} | |
public static void main(String[] args) throws URISyntaxException { | |
WebSocketClient client = new PingClient(new URI("ws://localhost:8887")); | |
client.connect(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.java_websocket.WebSocket; | |
import org.java_websocket.handshake.ClientHandshake; | |
import org.java_websocket.server.WebSocketServer; | |
import java.net.InetSocketAddress; | |
public class PingServer extends WebSocketServer { | |
public PingServer(InetSocketAddress inetSocketAddress) { | |
super(inetSocketAddress); | |
} | |
@Override | |
public void onOpen(WebSocket conn, ClientHandshake handshake) { | |
conn.send(Long.toString(System.currentTimeMillis())); | |
} | |
@Override | |
public void onClose(WebSocket conn, int code, String reason, boolean remote) { | |
} | |
@Override | |
public void onMessage(WebSocket conn, String message) { | |
long current = System.currentTimeMillis(); | |
try { | |
long client = Long.parseLong(message); | |
System.out.println(current-client); | |
} catch (Exception e) { | |
//To handle | |
} | |
} | |
@Override | |
public void onError(WebSocket conn, Exception ex) { | |
} | |
@Override | |
public void onStart() { | |
} | |
public static void main(String[] args) { | |
String host = "localhost"; | |
int port = 8887; | |
WebSocketServer server = new PingServer(new InetSocketAddress(host, port)); | |
server.run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment