0

I have a 'nodeJs' server using 'expressJs' and 'socket.io'. The page opens fine unless I use 'socket.io. If you use 'socket.io', then it throws an 'Unknown error' error:

const app = require('express');
const server = require('http').Server(app);
const io = require('socket.io')(server, {}); // this line causes 'Unknown error' on page load

It doesn't matter what parameters to use in {}. If you just create a variable const io = require('socket.io'), then the page will open successfully.

Does anyone know what could be causing this and how to resolve it? Maybe because the browser does not understand some types and therefore gives an error? I read online that WebView used to use the Nashorn API, which allowed plugin libraries to convert their data to a Java-friendly type (serializing complex objects to JSON) and apparently avoided errors, but it has been removed.

Update: I found https://github.com/socketio/socket.io-client-java, maybe this is the right direction in solving the question, but I don't understand how to use it with webview...

jewelsea
  • 150,031
  • 14
  • 366
  • 406
Dmitriy
  • 375
  • 1
  • 18
  • *"I read online that WebView used to use the Nashorn API"* -> I don't think so, [I think WebView has always used JavaScriptCore](https://stackoverflow.com/questions/30104124/what-javascript-engine-used-inside-javafx). – jewelsea Oct 26 '22 at 21:49
  • @jewelsea It was answered here that it was removed https://bugs.openjdk.org/browse/JDK-8091505 – Dmitriy Oct 26 '22 at 21:52
  • *"which allowed plugin libraries to convert their data to a Java-friendly type (serializing complex objects to JSON) and apparently avoided errors"* -> The JavaScript implementation in WebView should have nothing to do with the communications protocol (e.g. socket.io/http/websockets), so may have nothing to do with your problem. There are numerous issues that could cause your page not to load, I don't know what the actual cause is. – jewelsea Oct 26 '22 at 21:54
  • No, that JDK-8091505 was a request to add Nashorn as an alternate JavaScript implementation in WebView, which was denied. Nashorn was never provided as an alternate JavaScript implementation in WebView, so Nashorn's [later removal from the JDK (not JavaFX or WebView)](https://openjdk.org/jeps/372) was irrelevant to both JavaFX and WebView. – jewelsea Oct 26 '22 at 21:56
  • @jewelsea The page stops opening just after connecting socket.io. I found https://github.com/socketio/socket.io-client-java, maybe using it explicitly can help if socket.io is the problem? I don't see what the problem could be either. Other browsers (Chrome, jxBrowser, JCEF) don't have the issues that happen in WebView – Dmitriy Oct 26 '22 at 21:58
  • "*I found https://github.com/socketio/socket.io-client-java*" -> That library is also irrelevant if you are using WebView. That is a Java library, and WebView runs using its own processing engine and JavaScript implementation. You can use the Java socket-io client library if you call it from Java directly (not via WebView), but that isn't what you indicate that you want to achieve. – jewelsea Oct 26 '22 at 21:58
  • @jewelsea Perhaps the mechanism for processing and implementing JavaScript in WebView does not cope with these frameworks? Can it be expanded or updated in some way? – Dmitriy Oct 26 '22 at 22:01
  • [socket-io](https://socket.io) provides a web socket implementation with a fallback to HTTP long polling. WebView handles [websockets](https://stackoverflow.com/questions/46708924/websocket-support-for-javafx-webview) and can also handle HTTP, so I don't know what the issue is. – jewelsea Oct 26 '22 at 22:03
  • *Perhaps the mechanism for processing and implementing JavaScript in WebView does not cope with these frameworks?* -> That is unlikely, JavaScriptCore is the same JavaScript implementation found in other major browsers and is compliant with recent JavaScript standards. Likely it is some other issue. And no, the JavaScript implementation in WebView is not expandable or updateable. – jewelsea Oct 26 '22 at 22:06
  • I advise trying to track down the actual cause of your error (I don't have advice on how to do that, and it might be very difficult to do so). Then you could try to look for potential solutions. Otherwise, you are unlikely to solve this issue, unless you adopt an entirely different approach (e.g. use jxBrowser/JCEF, etc instead). – jewelsea Oct 26 '22 at 22:08
  • @jewelsea I just remembered that I manage to launch the page if I add the following code before webEngine.loadUrl(url): URI uri = URI.create(url); System.setProperty('http.proxyHost', uri.getHost()); System.setProperty('http.proxyPort', String.valueOf(uri.getPort())); After that, the page opens, but web sockets do not work. There are also issues with page redirects. Maybe an error occurs because it does not pass the security check? Certificates or something like that... – Dmitriy Oct 26 '22 at 22:11
  • Proxy-based issues [can occur with WebSockets](https://stackoverflow.com/questions/38974731/websockets-not-connected-behind-proxy). The socket.io library says that it can gracefully fallback to long-poll HTTP if the websocket connection fails, but I guess there is some issue there (I don't know what). – jewelsea Oct 26 '22 at 22:15
  • @jewelsea I would use JCEF, but unfortunately I have problems with it too. Since I have a javafx application, I understand that JCEF works together with Swing, it will be problematic to rewrite the application, but I'm ready for this ... The main problem for me is that I can't make JCEF transparent and place it in container JLayeredPane so I can add JCEF over VLCJ – Dmitriy Oct 26 '22 at 22:16
  • *"Maybe an error occurs because it does not pass the security check? Certificates or something like that"* -> In your previous question you mentioned that you are using HTTP, not HTTPS or WSS, so TLS and certificates are not involved (unless you added them). – jewelsea Oct 26 '22 at 22:16
  • *"There are also issues with page redirects."* -> this can be caused by a low-quality proxy implementation, but I don't the actual cause in your case. – jewelsea Oct 26 '22 at 22:17
  • @jewelsea Here https://stackoverflow.com/questions/38974731/websockets-not-connected-behind-proxy , as I understand it, it works fine if the client does not use a proxy. My page won't even open without a proxy. If I use a proxy, then yes, the same problem as in this question and apparently switching to https can help in this case... – Dmitriy Oct 26 '22 at 22:22
  • @jewelsea So far, I've managed to find out that before version 14-ea+2, my site opens fine. The site is the simplest with nodeJs and socket.io. If the version is higher, then throws an unknown error and other problems occur. It would be possible to use version 14-ea+2, but I need transparency, which was added in version 18... :( – Dmitriy Oct 28 '22 at 03:12
  • @jewelsea I found an example that is easy to try on your own to see the problem. [socketio-chat](https://socketio-chat-h9jt.herokuapp.com/) - if you open this chat in javafx.WebView, then everything works and there is no unknown error. But if you install chat ([github.com/socketio](https://github.com/socketio/socket.io/tree/master/examples/chat)) locally on PC and open localhost:3000 then it throws an unknown error... – Dmitriy Oct 28 '22 at 11:26

0 Answers0