在 Nashorn 中使用 WebSocket

use WebSocket in Nashorn

我正在使用 Intellij IDE,我想在 Nashorn Java8 评估的 javascript 中实现 websocket。我的 javascript 文件顶部有以下行:

var socket = new WebSocket("http://localhost:12345/echo");

但它给我这个错误:

Caused by: javax.script.ScriptException: ReferenceError: "WebSocket" is not defined in src/sample/hello.js at line number 8
    at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:564)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:548)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:528)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:524)
    at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:194)
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
    at sample.Main.start(Main.java:28)
    at com.sun.javafx.application.LauncherImpl.run(LauncherImpl.java:837)
    at com.sun.javafx.application.PlatformImpl.run(PlatformImpl.java:335)
    at com.sun.javafx.application.PlatformImpl.run(PlatformImpl.java:301)
    at com.sun.javafx.application.PlatformImpl.run(PlatformImpl.java:298)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.run(PlatformImpl.java:298)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access0(WinApplication.java:39)
    at com.sun.glass.ui.win.WinApplication.run(WinApplication.java:112)
    ... 1 more
Caused by: src/sample/hello.js:8 ReferenceError: "WebSocket" is not defined
    at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:56)
    at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:318)
    at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:290)
    at jdk.nashorn.api.scripting.NashornScriptEngine.__noSuchProperty__(NashornScriptEngine.java:274)
    at jdk.nashorn.internal.scripts.Script$engine._L35(nashorn:engine/resources/engine.js:37)
    at jdk.nashorn.internal.scripts.Script$hello.runScript(src/sample/hello.js:8)
    at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:498)
    at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:206)
    at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:378)
    at jdk.nashorn.internal.runtime.Context.evaluateSource(Context.java:885)
    at jdk.nashorn.internal.runtime.Context.load(Context.java:563)
    at jdk.nashorn.internal.objects.Global.load(Global.java:857)
    at jdk.nashorn.internal.scripts.Script$\^eval\_.runScript(<eval>:1)
    at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:498)
    at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:206)
    at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:378)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:546)
    ... 16 more

WebSocket是在HTML5插件中定义的,所以我不太清楚为什么Nashorn在库中找不到websocket。

Nashorn 是一个兼容 ECMAScript5 的Java脚本引擎。 WebSocket API 不是 ECMAScript 5 的一部分,而是浏览器 API 的一部分。 您可以使用任何可用的 Java WebSocket 客户端库来实现 WebSocket API。

例如,http://jfarcand.wordpress.com/2011/12/21/writing-websocket-clients-using-asynchttpclient/ or https://tyrus.java.net/documentation/1.7/user-guide.html#websocket-client-endpoint

对于 vertx 框架,它有 API 连接到 WebSocket 端点 https://github.com/vert-x/vertx-examples/blob/master/src/raw/javascript/websockets/ws_client.js

为了在 Nashorn 中启用 WebSocket API 支持,Java 9 的 HttpClient 模块 jdk.incubator.httpclient 可以是另一种选择。 与其他 Java WebSocket 客户端库相比,它 API 稍微简单一些。 我整理了一个基本示例,说明如何使用它在 Nashorn 中启用 WebSocket API: https://github.com/kjussakov/sochorn