Search code examples
javawebsocketjava-8nashorn

use WebSocket in Nashorn


I'm using Intellij IDE and I want to implement websocket in a javascript that's evaluated by Nashorn Java8. I have the following line at the top of my javascript file:

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

Yet it is giving me this error:

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$8.run(LauncherImpl.java:837)
    at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:335)
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:301)
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:298)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$6.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.access$300(WinApplication.java:39)
    at com.sun.glass.ui.win.WinApplication$4$1.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 is defined in the HTML5 plugin, so I am not really sure why Nashorn can't find the websocket in the library.


Solution

  • Nashorn is a ECMAScript5 compatible JavaScript engine. WebSocket API is not a part of ECMAScript 5 but browser API. You can implement WebSocket API using any available Java WebSocket client libraries.

    For example, 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

    In case of vertx framework, it has API to connect to WebSocket endpoint https://github.com/vert-x/vertx-examples/blob/master/src/raw/javascript/websockets/ws_client.js