Last active
April 27, 2020 06:28
-
-
Save PhilipRoman/e5143498994564ef9361e428115f80ec to your computer and use it in GitHub Desktop.
WebSocketServer port test
This file contains hidden or 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 java.net.*; | |
import java.io.*; | |
import org.java_websocket.server.*; | |
import org.java_websocket.*; | |
import org.java_websocket.handshake.*; | |
public class Main extends WebSocketServer { | |
public static final int PORT = Integer.getInteger("port", 3141); | |
public static void main(String[] args) throws Exception { | |
for(int i = 1; i <= 10; i++) { | |
var server = new Main(PORT); | |
server.setReuseAddr(true); | |
server.run(); | |
} | |
} | |
public Main(int port) { | |
super(new InetSocketAddress("localhost", port)); | |
} | |
@Override | |
public void onStart() { | |
System.out.println("Listening on port " + getPort()); | |
new Thread(() -> { | |
try { | |
Thread.sleep(200); | |
stop(); | |
} catch(Exception e) { | |
e.printStackTrace(System.out); | |
} | |
}).start(); | |
} | |
@Override | |
public void onError(WebSocket socket, Exception e) { | |
e.printStackTrace(System.out); | |
} | |
@Override | |
public void onMessage(WebSocket socket, String msg) { | |
} | |
@Override | |
public void onClose(WebSocket socket, int code, String msg, boolean remote) { | |
} | |
@Override | |
public void onOpen(WebSocket socket, ClientHandshake handshake) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment