Created
May 26, 2021 07:56
-
-
Save nahakiole/a3e21290483a4faba26ff12309eb83c5 to your computer and use it in GitHub Desktop.
NanoHTTP Webserver that serves Websocket and HTTP.
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 android.os.Build; | |
import android.util.Log; | |
import java.io.IOException; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import fi.iki.elonen.NanoHTTPD; | |
import fi.iki.elonen.NanoWSD; | |
public class HTTPControl extends NanoWSD { | |
private static HTTPControl instance; | |
private static final Logger LOG = Logger.getLogger(HTTPControl.class.getName()); | |
private final boolean debug; | |
public static final int PORT = 8080; | |
public static HTTPControl instance() throws IOException { | |
if (instance != null) { | |
return instance; | |
} | |
instance = new HTTPControl(); | |
instance.start(); | |
return instance; | |
} | |
private HTTPControl() { | |
super(PORT); | |
this.debug = true; | |
} | |
@Override | |
protected WebSocket openWebSocket(IHTTPSession handshake) { | |
return new DebugWebSocket(this, handshake); | |
} | |
@Override | |
public NanoHTTPD.Response serveHttp(NanoHTTPD.IHTTPSession session) { | |
return newFixedLengthResponse("test"); | |
} | |
private static class DebugWebSocket extends WebSocket { | |
private final HTTPControl server; | |
public DebugWebSocket(HTTPControl server, IHTTPSession handshakeRequest) { | |
super(handshakeRequest); | |
this.server = server; | |
} | |
@Override | |
protected void onOpen() { | |
} | |
@Override | |
protected void onClose(WebSocketFrame.CloseCode code, String reason, boolean initiatedByRemote) { | |
if (server.debug) { | |
Log.d("DebugWebSocket","C [" + (initiatedByRemote ? "Remote" : "Self") + "] " + (code != null ? code : "UnknownCloseCode[" + code + "]") | |
+ (reason != null && !reason.isEmpty() ? ": " + reason : "")); | |
} | |
} | |
@Override | |
protected void onMessage(WebSocketFrame message) { | |
try { | |
message.setUnmasked(); | |
sendFrame(message); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
@Override | |
protected void onPong(WebSocketFrame pong) { | |
if (server.debug) { | |
Log.d("DebugWebSocket","P " + pong); | |
} | |
} | |
@Override | |
protected void onException(IOException exception) { | |
HTTPControl.LOG.log(Level.SEVERE, "exception occured", exception); | |
Log.d("DebugWebSocket","exception occured", exception); | |
} | |
@Override | |
protected void debugFrameReceived(WebSocketFrame frame) { | |
if (server.debug) { | |
Log.d("DebugWebSocket","R " + frame); | |
} | |
} | |
@Override | |
protected void debugFrameSent(WebSocketFrame frame) { | |
if (server.debug) { | |
Log.d("DebugWebSocket","S " + frame); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment