2

I am using the AndroidAsync library from GitHub that is provided by koush. I need this library to create a WebSocket server and I was able to create it.

private static List<WebSocket> webSockets = new ArrayList<WebSocket>();
private static AsyncHttpServer httpServer = new AsyncHttpServer();

Here's the implementation:

public static void createWebSocket() {
    httpServer.websocket("/", new AsyncHttpServer.WebSocketRequestCallback() {
        @Override
        public void onConnected(final WebSocket webSocket, AsyncHttpServerRequest request) {
            webSockets.add(webSocket);

            webSocket.setClosedCallback(new CompletedCallback() {
                @Override
                public void onCompleted(Exception ex) {
                    Log.d(TAG, "onCompleted");
                }
            });

            webSocket.setStringCallback(new WebSocket.StringCallback() {
                @Override
                public void onStringAvailable(String s) {
                    Log.d(TAG, "onStringAvailable");
                }
            });
        }
    });

    httpServer.listen(8080);
}

This implementation works completely fine. But I want to use the wss protocol where I can use a JKS (Java KeyStore) certificate for the websocket.

Is there any way to do this? If not with this library, is there any other library I can use? Any example would be really appreciated.

Thank you!!

ᴛʜᴇᴘᴀᴛᴇʟ
  • 4,466
  • 5
  • 39
  • 73

3 Answers3

0

try this one. I haven't used it. However, it says it supports Java servers and Android clients with support for wss. Good luck!

Ruchira Randana
  • 4,021
  • 1
  • 27
  • 24
0

Honestly, I don't know that I am right. I just happened into this.

Do you think you could do it with NanoHTTPD for java?

I imagine the basic structure is:

MyHTTPDTask

import java.io.IOException;
import java.util.Map;

import fi.iki.elonen.NanoHTTPD;

class MyHTTPDTask extends AsyncTask {
    private MyServer mHTTPD;

    @Override
    protected Object doInBackground(Object... params) {
        mHTTPD = new MyServer();
        mHTTPD.makeSecure(NanoHTTPD.makeSSLSocketFactory(R.string.keystore.jks, "password".toCharArray()), null);
    }
}

MyServer

import java.io.IOException;

import fi.iki.elonen.NanoHTTPD;

public class MyServer extends NanoHTTPD {
    private final static int PORT = 8080;

    public MyServer() throws IOException {
        super(PORT);
        start();
        System.out.println( "\nRunning! Point your browers to http://localhost:8080/ \n" );
    }

    @Override
    public Response serve(IHTTPSession session) {
        String msg = "<html><body><h1>Hello server</h1>\n";
        msg += "<p>We serve " + session.getUri() + " !</p>";
        return newFixedLengthResponse( msg + "</body></html>\n" );
    }
}
roberto tomás
  • 4,435
  • 5
  • 42
  • 71
0

For the server side, why don't you use the standard API, javax.websocket? It's a part of Java EE.

For the Android side, see "Which WebSocket library to use in Android app?".

Community
  • 1
  • 1
Takahiko Kawasaki
  • 18,118
  • 9
  • 62
  • 105