• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package fi.iki.elonen.samples.echo;
2 
3 /*
4  * #%L
5  * NanoHttpd-Websocket
6  * %%
7  * Copyright (C) 2012 - 2015 nanohttpd
8  * %%
9  * Redistribution and use in source and binary forms, with or without modification,
10  * are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice, this
13  *    list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  *    this list of conditions and the following disclaimer in the documentation
17  *    and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the nanohttpd nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software without
21  *    specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
32  * OF THE POSSIBILITY OF SUCH DAMAGE.
33  * #L%
34  */
35 
36 import java.io.IOException;
37 import java.util.logging.Level;
38 import java.util.logging.Logger;
39 
40 import fi.iki.elonen.NanoWSD;
41 import fi.iki.elonen.NanoWSD.WebSocketFrame.CloseCode;
42 
43 /**
44  * @author Paul S. Hawke (paul.hawke@gmail.com) On: 4/23/14 at 10:31 PM
45  */
46 public class DebugWebSocketServer extends NanoWSD {
47 
48     /**
49      * logger to log to.
50      */
51     private static final Logger LOG = Logger.getLogger(DebugWebSocketServer.class.getName());
52 
53     private final boolean debug;
54 
DebugWebSocketServer(int port, boolean debug)55     public DebugWebSocketServer(int port, boolean debug) {
56         super(port);
57         this.debug = debug;
58     }
59 
60     @Override
openWebSocket(IHTTPSession handshake)61     protected WebSocket openWebSocket(IHTTPSession handshake) {
62         return new DebugWebSocket(this, handshake);
63     }
64 
65     private static class DebugWebSocket extends WebSocket {
66 
67         private final DebugWebSocketServer server;
68 
DebugWebSocket(DebugWebSocketServer server, IHTTPSession handshakeRequest)69         public DebugWebSocket(DebugWebSocketServer server, IHTTPSession handshakeRequest) {
70             super(handshakeRequest);
71             this.server = server;
72         }
73 
74         @Override
onOpen()75         protected void onOpen() {
76         }
77 
78         @Override
onClose(CloseCode code, String reason, boolean initiatedByRemote)79         protected void onClose(CloseCode code, String reason, boolean initiatedByRemote) {
80             if (server.debug) {
81                 System.out.println("C [" + (initiatedByRemote ? "Remote" : "Self") + "] " + (code != null ? code : "UnknownCloseCode[" + code + "]")
82                         + (reason != null && !reason.isEmpty() ? ": " + reason : ""));
83             }
84         }
85 
86         @Override
onMessage(WebSocketFrame message)87         protected void onMessage(WebSocketFrame message) {
88             try {
89                 message.setUnmasked();
90                 sendFrame(message);
91             } catch (IOException e) {
92                 throw new RuntimeException(e);
93             }
94         }
95 
96         @Override
onPong(WebSocketFrame pong)97         protected void onPong(WebSocketFrame pong) {
98             if (server.debug) {
99                 System.out.println("P " + pong);
100             }
101         }
102 
103         @Override
onException(IOException exception)104         protected void onException(IOException exception) {
105             DebugWebSocketServer.LOG.log(Level.SEVERE, "exception occured", exception);
106         }
107 
108         @Override
debugFrameReceived(WebSocketFrame frame)109         protected void debugFrameReceived(WebSocketFrame frame) {
110             if (server.debug) {
111                 System.out.println("R " + frame);
112             }
113         }
114 
115         @Override
debugFrameSent(WebSocketFrame frame)116         protected void debugFrameSent(WebSocketFrame frame) {
117             if (server.debug) {
118                 System.out.println("S " + frame);
119             }
120         }
121     }
122 }
123