Search code examples
javascriptjavawebsocketjxbrowser

JxBrowser loading HTML from resource URL, cannot open websocket


I am using JxBrowser to load a html file that resides in a jar. This html file has a websocket that is failing to connect (I receive code 106 in the onclose event)

I am getting these errors from JxBrowser

09:20:18.381 INFO: 09:20:18 SEVERE: [1228/092018.371:ERROR:validation_errors.cc(87)] Invalid message: VALIDATION_ERROR_DESERIALIZATION_FAILED

09:20:18.382 INFO: 09:20:18 SEVERE: [1228/092018.371:ERROR:render_process_host_impl.cc(4145)] Terminating render process for bad Mojo message: Received bad user message: Validation failed for WebSocket::AddChannelRequest deserializer [VALIDATION_ERROR_DESERIALIZATION_FAILED]

09:20:18.382 INFO: 09:20:18 SEVERE: [1228/092018.371:ERROR:bad_message.cc(23)] Terminating renderer for bad IPC message, reason 123

Here is my code for creating the browser

Browser browser = new Browser();
BrowserView browserView = new BrowserView(browser);
browser.addConsoleListener(new ConsoleListener() {
            @Override
            public void onMessage(ConsoleEvent ce) {
                System.out.println("Console log from " + ce.getSource() + " message " + ce.getMessage());
            }
        });
        
        BrowserContext browserContext = browser.getContext();
        ProtocolService protocolService = browserContext.getProtocolService();
        protocolService.setProtocolHandler("jar", new ProtocolHandler() {
            @Override
            public URLResponse onRequest(URLRequest request) {
                try {
                    URLResponse response = new URLResponse();
                    URL path = new URL(request.getURL());
                    InputStream inputStream = path.openStream();
                    DataInputStream stream = new DataInputStream(inputStream);
                    byte[] data = new byte[stream.available()];
                    stream.readFully(data);
                    response.setData(data);
                    String mimeType = getMimeType(path.toString());
                    response.getHeaders().setHeader("Content-Type", mimeType);
                    return response;
                } catch (Exception ignored) {
                }
                return null;
            }
        });
        URL url = getClass().getClassLoader().getResource("resources/example.html");
        browser.loadURL(url.toExternalForm());

Here is a simplified version of my WebSocket code

this.ws = new WebSocket('ws://127.0.0.0:1337');
        this.ws.onopen = () => {
            console.log('OnOpen');
        };
        this.ws.onmessage = (evt) => {
            console.log('OnMessage ' + evt.data);
        };
        this.ws.onerror = (event) => {
            console.log('OnError');
        };
        this.ws.onclose = (event) => {
            console.log('OnClose ' + event.code + ' ' + event.reason);
        };

I am having a hard time finding information on the error messages received from Chromium. Could there be problems because the URL is using the jar: protocol? Or is this a problem because chrome does not allow unsecure websocket connections?

(Edit) The socket connects when I use the JxBrowser's loadHTML method and supply the string text of the HTML file. So I believe this has something to do with the fact that I am loading from a resource URL, and not to do with chrome not allowing unsecure websocket connections. I really dont want to have to stream the file contents so that I can use loadHTML.

(Edit2) Loading the HTML from a file URL also works. I guess I can create a local copy of the HTML file in the jar and load that instead. I am disappointed that I cannot do this using the jar URL, I guess it might be a limitation of JxBrowser.


Solution

  • I have decided to copy the HTML files from the jar into a local folder, and using a File URL to load them.