1

I have a minimal javafx webview and a nodeJs server code. But webview doesn't open its local page (http://localhost:8000/), after loading the page, the status is CANCELLED. Full sequence of statuses: READY > SCHEDULED > RUNNING > CANCELLED.

If you run the page 'http://192.168.54.1:8000' (localhost > ipv4) then it throws the error 'Connection refused by server'.

Please tell me what could be the problem?

WebView code:

import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;

public class Web {
    private JSBridge jsBridge;
    private WebEngine webEngine;
    private javafx.scene.web.WebView webView;
    private Worker<?> engineWorker;
    Web(){
        webView = new javafx.scene.web.WebView();
        webView.setPageFill(Color.TRANSPARENT);
        webEngine = webView.getEngine();

        engineWorker = webEngine.getLoadWorker();
        engineWorker.stateProperty().addListener(this::engineWorkerListener);
        webEngine.load("http://localhost:8000/");
    }

    private void engineWorkerListener(ObservableValue<? extends Worker.State> ov, Worker.State oldState, Worker.State newState){
        System.out.println("WebView status: " + ov.getValue());
        if (newState == Worker.State.FAILED) System.out.println("WebView exception: " + webEngine.getLoadWorker().getException().toString());
        if (newState == Worker.State.SUCCEEDED) System.out.println("WebView loading SUCCEEDED!");
    }
}

NodeJS server code:

const http = require("http");
const host = 'localhost';
const port = 8000;

const requestListener = function (req, res) {
    res.writeHead(200);
    res.end("My first server!");
};

const server = http.createServer(requestListener);
server.listen(port, host, () => {
    console.log(`Server is running on http://${host}:${port}`);
});
Dmitriy
  • 375
  • 1
  • 18
  • Can you open the url in a standard browser? Could be a firewall rule blocking a non-standard port, but I do not know the cause. – jewelsea Oct 26 '22 at 20:37
  • I am not sure why sometimes you reference localhost and other times 192.168.54.1. – jewelsea Oct 26 '22 at 20:40
  • @jewelsea Yes, I tried opening in Chrome, jxBrowser, JCEF - it works fine there. But such problems occur on WebView, I would like to set up the correct work on this particular browser, since I want to use javafx. I tried to replace 'localhost' with ipv4 (192.168.54.1), which means the same thing, because I read on the Internet that it helped someone in solving their problem in opening a local page, but my problem is precisely that the browser does not know how to work with socket.io (and possibly other frameworks due to serializing complex objects to JSON) – Dmitriy Oct 26 '22 at 21:19
  • @jewelsea I already solved this problem by changing the line res.writeHead(200) > res.writeHead('200', {'Content-Type': 'text/html'}); After that, the page opened, but unfortunately this does not solve my main problem, where the page does not start when 'socket.io' is connected (https://stackoverflow.com/questions/74210940/javafx-webview-not-load-page-with -socket-io) Please see the question and share your knowledge if you can. – Dmitriy Oct 26 '22 at 21:28
  • I suggest you [self-answer this question](https://stackoverflow.com/help/self-answer) with the solution you provided in the comment. I know you have another problem related to `socket.io` use, but that appears to be a different problem from specifying the correct return code and content type to the HTTP response. – jewelsea Oct 26 '22 at 21:45

1 Answers1

1

Changing this line of code solved the problem and the page opened successfully:

res.writeHead(200) -> res.writeHead('200', {'Content-Type': ''});
Dmitriy
  • 375
  • 1
  • 18