Home AMX User Forum Scripting Languages
Options

IP Communications & Muse JavaScript scripts

How an IP Connection is implemented in Muse JS is not immediately obvious; by definition raw sockets are not "typically" available in JavaScript and Node.JS is not a thing here.

For those who have tried, even "standard" http methods such as XMLHttpRequest will log a runtime reference error that "XMLHttpRequest" is not defined. But it is in that error log where the solution can be found

jdk.nashorn.internal.runtime.ECMAException: ReferenceError: "XMLHttpRequest" is not defined

As highlighted the Muse JavaScript engine is an implementation of Java Nashorn engine https://docs.oracle.com/en/java/javase/11/nashorn/introduction.html and much like Groovy, the underlying Java classes can be imported into the script.

Below is a Muse JS script that queries a Roku STB (RESTful API)

var t1 = context.services.get("timeline");
t1.start([10000], true);
t1.expired.listen(function(){
    context.log.info("polling");
    var url = "http://192.168.2.41:8060/query/apps";
    var response; 

    response = httpGet(url).data;
    context.log.info(response);
});

function httpGet(theUrl){
    var con = new java.net.URL(theUrl).openConnection();
    con.requestMethod = "GET";

    return asResponse(con);
}

function asResponse(con){
    var d = read(con.inputStream);

    return {data : d, statusCode : con.responseCode};
}

function read(inputStream){
    var inReader = new java.io.BufferedReader(new java.io.InputStreamReader(inputStream));
    var inputLine;
    var response = new java.lang.StringBuffer();

    while ((inputLine = inReader.readLine()) != null) {
           response.append(inputLine);
    }
    inReader.close();
    return response.toString();
}

Source:
https://gist.github.com/billybong/a462152889b6616deb02

And the run time printout:

07:36:13.762 INFO  | pool-23-thread-1 | Program-roku-js | 39 |  <?xml version="1.0" encoding="UTF-8" ?><apps>   <app id="31012" type="menu" version="2.0.62">Vudu Movie &amp; TV Store</app> <app id="2941" type="appl" version="1.14.103">SomaFM</app>      <app id="14362" type="appl" version="3.0.167">Amazon Music</app>     <app id="837" type="appl" version="2.20.110005158">YouTube</app>        <app id="12" type="appl" version="4.2.100018003">Netflix</app>  <app id="13" type="appl" version="14.1.2022120820">Prime Video</app> <app id="151908" type="appl" version="9.3.10">The Roku Channel</app>    <app id="2251" type="appl" version="2.0.0">CNET</app>        <app id="9477" type="appl" version="1.0.259">Vanguard Cinema</app>      <app id="9581" type="appl" version="4.0.2">NBC News</app>    <app id="1453" type="appl" version="2.5.3">TuneIn</app> <app id="1688" type="appl" version="1.6.2">Roku Newscaster</app>        <app id="2213" type="appl" version="5.5.13">Roku Media Player</app>  <app id="65978" type="appl" version="3.1.3217">CNN</app>        <app id="53725" type="appl" version="9.3.1">NBC Sports</app> <app id="51320" type="appl" version="2.2.824">Red Bull TV</app> <app id="17112" type="appl" version="9.3.6">CBS Sports Stream &amp; Watch Live</app> <app id="34376" type="appl" version="4.8.2023051800">ESPN</app> <app id="13535" type="appl" version="7.9.3">Plex - Free Movies &amp; TV</app>   <app id="27181" type="appl" version="6.0.5">Sky News</app>   <app id="27536" type="appl" version="5.27.6">CBS News</app>     <app id="95307" type="appl" version="4.0.1225">FOX Sports</app>      <app id="74519" type="appl" version="5.29.3">Pluto TV - It&apos;s Free TV</app> <app id="60716" type="appl" version="4.4.6212023">KING 5 News for Seattle-Tacoma</app>       <app id="38345" type="appl" version="4.4.101">SiriusXM</app>    <app id="43465" type="appl" version="4.8.2600">Fubo: Watch Live TV &amp; Sports</app>        <app id="73376" type="appl" version="5.18.1">ABC: Watch TV Shows &amp; News</app>       <app id="629613" type="appl" version="1.1.4">The Rugby Network</app> <app id="172665" type="appl" version="2.7.6">Haystack Local &amp; World News</app>      <app id="291097" type="appl" version="1.37.2023101000">Disney Plus</app>     <app id="551012" type="appl" version="14.0.43">Apple TV</app>   <app id="637193" type="appl" version="14.0.43">Apple Music</app>     <app id="46041" type="appl" version="8.72.8274">Sling TV - Live Sports, News, Shows + Freestream</app>  <app id="683311" type="appl" version="8.4.31">LiveTV</app></apps>

Comments

  • Options
    ychych Posts: 30

    Can the Muse controller support pouring third-party JAR packets? The NX series has limitations, isn't the Muse series no longer subject to any constraints?

  • Options

    @ych said:
    Can the Muse controller support pouring third-party JAR packets? The NX series has limitations, isn't the Muse series no longer subject to any constraints?

    Yes - in Extension/Driver/Service development there are not any limitations on external dependencies other than Java8 compliance.

    The above snippet is about the extent of scripting programming I have been doing so am unable to comment on external jars in a script.

Sign In or Register to comment.