aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLordFokas <lordfokas@gmail.com>2015-10-22 22:35:18 +0100
committerLordFokas <lordfokas@gmail.com>2015-10-22 22:35:18 +0100
commitdbb8383a3367aebd46b66252f30d8c6a0cf49de7 (patch)
treee0465c877ed0e304535151d47495bbf608ecb879
parentbb83b42ef8887b3f8ec26b346ba8111cb873dc8f (diff)
downloadnanohttpd-dbb8383a3367aebd46b66252f30d8c6a0cf49de7.tar.gz
Detached the WebSocket from the Server:
It makes more sense if each socket knows how to handle itself.
-rw-r--r--websocket/src/main/java/fi/iki/elonen/NanoWebSocketServer.java68
-rw-r--r--websocket/src/main/java/fi/iki/elonen/samples/echo/DebugWebSocketServer.java94
2 files changed, 77 insertions, 85 deletions
diff --git a/websocket/src/main/java/fi/iki/elonen/NanoWebSocketServer.java b/websocket/src/main/java/fi/iki/elonen/NanoWebSocketServer.java
index 9017a17..420bfc2 100644
--- a/websocket/src/main/java/fi/iki/elonen/NanoWebSocketServer.java
+++ b/websocket/src/main/java/fi/iki/elonen/NanoWebSocketServer.java
@@ -62,7 +62,7 @@ public abstract class NanoWebSocketServer extends NanoHTTPD {
CLOSED
}
- public class WebSocket {
+ public static abstract class WebSocket {
private final InputStream in;
@@ -84,6 +84,7 @@ public abstract class NanoWebSocketServer extends NanoHTTPD {
WebSocket.this.state = State.CONNECTING;
super.send(out);
WebSocket.this.state = State.OPEN;
+ WebSocket.this.onOpen();
readWebsocket();
}
};
@@ -100,33 +101,26 @@ public abstract class NanoWebSocketServer extends NanoHTTPD {
return state == State.OPEN;
}
- protected void onOpen(){
- NanoWebSocketServer.this.onOpen(this);
- }
-
- protected void onClose(CloseCode code, String reason, boolean initiatedByRemote){
- NanoWebSocketServer.this.onClose(this, code, reason, initiatedByRemote);
- }
-
- protected void onMessage(WebSocketFrame message){
- NanoWebSocketServer.this.onMessage(this, message);
- }
+ protected abstract void onOpen();
+ protected abstract void onClose(CloseCode code, String reason, boolean initiatedByRemote);
+ protected abstract void onMessage(WebSocketFrame message);
+ protected abstract void onPong(WebSocketFrame pong);
+ protected abstract void onException(IOException exception);
- protected void onPong(WebSocketFrame pong){
- NanoWebSocketServer.this.onPong(this, pong);
- }
+ /**
+ * Debug method. <b>Do not Override unless for debug purposes!</b>
+ *
+ * @param frame The received WebSocket Frame.
+ */
+ protected void debugFrameReceived(WebSocketFrame frame){}
- protected void onException(IOException exception){
- NanoWebSocketServer.this.onException(this, exception);
- }
-
- protected void debugFrameReceived(WebSocketFrame frame){
- NanoWebSocketServer.this.onFrameReceived(frame);
- }
-
- protected void debugFrameSent(WebSocketFrame frame){
- NanoWebSocketServer.this.onSendFrame(frame);
- }
+ /**
+ * Debug method. <b>Do not Override unless for debug purposes!</b><br>
+ * This method is called before actually sending the frame.
+ *
+ * @param frame The sent WebSocket Frame.
+ */
+ protected void debugFrameSent(WebSocketFrame frame){}
public void close(CloseCode code, String reason, boolean initiatedByRemote) throws IOException {
State oldState = this.state;
@@ -824,29 +818,9 @@ public abstract class NanoWebSocketServer extends NanoHTTPD {
return isUpgrade && isCorrectConnection;
}
- protected abstract void onClose(WebSocket webSocket, CloseCode code, String reason, boolean initiatedByRemote);
-
- protected abstract void onException(WebSocket webSocket, IOException e);
-
// --------------------------------Listener--------------------------------
- protected void onFrameReceived(WebSocketFrame webSocket) {
- // only for debugging
- }
-
- protected abstract void onOpen(WebSocket webSocket);
-
- protected abstract void onMessage(WebSocket webSocket, WebSocketFrame messageFrame);
-
- protected abstract void onPong(WebSocket webSocket, WebSocketFrame pongFrame);
-
- protected void onSendFrame(WebSocketFrame webSocket) {
- // only for debugging
- }
-
- public WebSocket openWebSocket(IHTTPSession handshake) {
- return new WebSocket(handshake);
- }
+ protected abstract WebSocket openWebSocket(IHTTPSession handshake);
@Override
public Response serve(final IHTTPSession session) {
diff --git a/websocket/src/main/java/fi/iki/elonen/samples/echo/DebugWebSocketServer.java b/websocket/src/main/java/fi/iki/elonen/samples/echo/DebugWebSocketServer.java
index 8f6aa78..353d649 100644
--- a/websocket/src/main/java/fi/iki/elonen/samples/echo/DebugWebSocketServer.java
+++ b/websocket/src/main/java/fi/iki/elonen/samples/echo/DebugWebSocketServer.java
@@ -38,6 +38,7 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import fi.iki.elonen.NanoWebSocketServer;
+import fi.iki.elonen.NanoWebSocketServer.WebSocketFrame.CloseCode;
/**
* @author Paul S. Hawke (paul.hawke@gmail.com) On: 4/23/14 at 10:31 PM
@@ -56,47 +57,64 @@ public class DebugWebSocketServer extends NanoWebSocketServer {
this.debug = debug;
}
- @Override
- protected void onClose(WebSocket socket, WebSocketFrame.CloseCode code, String reason, boolean initiatedByRemote) {
- if (this.debug) {
- System.out.println("C [" + (initiatedByRemote ? "Remote" : "Self") + "] " + (code != null ? code : "UnknownCloseCode[" + code + "]")
- + (reason != null && !reason.isEmpty() ? ": " + reason : ""));
- }
- }
+ @Override
+ protected WebSocket openWebSocket(IHTTPSession handshake) {
+ return new DebugWebSocket(this, handshake);
+ }
+
+ private static class DebugWebSocket extends WebSocket{
+ private final DebugWebSocketServer server;
+
+ public DebugWebSocket(DebugWebSocketServer server, IHTTPSession handshakeRequest) {
+ super(handshakeRequest);
+ this.server = server;
+ }
- @Override
- protected void onException(WebSocket socket, IOException e) {
- DebugWebSocketServer.LOG.log(Level.SEVERE, "exception occured", e);
- }
+ @Override
+ protected void onOpen(){}
- @Override
- protected void onFrameReceived(WebSocketFrame frame) {
- if (this.debug) {
- System.out.println("R " + frame);
- }
- }
+ @Override
+ protected void onClose(CloseCode code, String reason, boolean initiatedByRemote) {
+ if(server.debug) {
+ System.out.println("C [" + (initiatedByRemote ? "Remote" : "Self") + "] " + (code != null ? code : "UnknownCloseCode[" + code + "]")
+ + (reason != null && !reason.isEmpty() ? ": " + reason : ""));
+ }
+ }
- @Override
- protected void onMessage(WebSocket socket, WebSocketFrame messageFrame) {
- try {
- messageFrame.setUnmasked();
- socket.sendFrame(messageFrame);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
+ @Override
+ protected void onMessage(WebSocketFrame message) {
+ try {
+ message.setUnmasked();
+ sendFrame(message);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
- @Override
- protected void onPong(WebSocket socket, WebSocketFrame pongFrame) {
- if (this.debug) {
- System.out.println("P " + pongFrame);
- }
- }
+ @Override
+ protected void onPong(WebSocketFrame pong) {
+ if (server.debug) {
+ System.out.println("P " + pong);
+ }
+ }
- @Override
- public void onSendFrame(WebSocketFrame frame) {
- if (this.debug) {
- System.out.println("S " + frame);
- }
- }
+ @Override
+ protected void onException(IOException exception) {
+ DebugWebSocketServer.LOG.log(Level.SEVERE, "exception occured", exception);
+ }
+
+ @Override
+ protected void debugFrameReceived(WebSocketFrame frame) {
+ if (server.debug) {
+ System.out.println("R " + frame);
+ }
+ }
+
+ @Override
+ protected void debugFrameSent(WebSocketFrame frame) {
+ if (server.debug) {
+ System.out.println("S " + frame);
+ }
+ }
+ }
}