aboutsummaryrefslogtreecommitdiff
path: root/websocket/src/test
diff options
context:
space:
mode:
authorNiko Fink <nfink95@gmail.com>2014-01-30 12:58:54 +0100
committerNiko Fink <nfink95@gmail.com>2014-01-30 12:58:54 +0100
commit6d3737c76afdb1c991bf8d96e3dd2cc622435f09 (patch)
treeefc5e09a6a4815b2e13d431be322aea13972b2a2 /websocket/src/test
parent852318439539b54ee6b4ce048df63b6c12cf0417 (diff)
downloadnanohttpd-6d3737c76afdb1c991bf8d96e3dd2cc622435f09.tar.gz
Initial commit of NanoWSD
Diffstat (limited to 'websocket/src/test')
-rw-r--r--websocket/src/test/java/fi/iki/elonen/EchoSocket.java70
1 files changed, 70 insertions, 0 deletions
diff --git a/websocket/src/test/java/fi/iki/elonen/EchoSocket.java b/websocket/src/test/java/fi/iki/elonen/EchoSocket.java
new file mode 100644
index 0000000..ef39796
--- /dev/null
+++ b/websocket/src/test/java/fi/iki/elonen/EchoSocket.java
@@ -0,0 +1,70 @@
+package fi.iki.elonen;
+
+import java.io.IOException;
+
+public class EchoSocket {
+ public static void main(String[] args) throws IOException {
+ final boolean DEBUG = args.length >= 2 && args[1].toLowerCase().equals("-d");
+ NanoWSD ws = new NanoWSD(Integer.parseInt(args[0])) {
+ @Override
+ protected WebSocket openWebSocket(IHTTPSession handshake) {
+ return new WebSocket(handshake) {
+ @Override
+ protected void onPong(WebSocketFrame pongFrame) {
+ if (DEBUG) {
+ System.out.println("P " + pongFrame);
+ }
+ }
+
+ @Override
+ protected void onMessage(WebSocketFrame messageFrame) {
+ try {
+ messageFrame.setUnmasked();
+ sendFrame(messageFrame);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ protected void onClose(WebSocketFrame.CloseCode code, String reason, boolean initiatedByRemote) {
+ if (DEBUG) {
+ System.out.println("C [" + (initiatedByRemote ? "Remote" : "Self") + "] " + (code != null ? code : "UnknownCloseCode[" + code + "]") + (reason != null && !reason.isEmpty() ? ": " + reason : ""));
+ }
+ }
+
+ @Override
+ protected void onException(IOException e) {
+ e.printStackTrace();
+ }
+
+ @Override
+ protected void handleWebsocketFrame(WebSocketFrame frame) throws IOException {
+ if (DEBUG) {
+ System.out.println("R " + frame);
+ }
+ super.handleWebsocketFrame(frame);
+ }
+
+ @Override
+ public synchronized void sendFrame(WebSocketFrame frame) throws IOException {
+ if (DEBUG) {
+ System.out.println("S " + frame);
+ }
+ super.sendFrame(frame);
+ }
+ };
+ }
+ };
+
+ ws.start();
+ System.out.println("Server started, hit Enter to stop.\n");
+ try {
+ System.in.read();
+ } catch (IOException ignored) {
+ }
+ ws.stop();
+ System.out.println("Server stopped.\n");
+ }
+}
+