使用PrintNode JS API进行静默打印

时间:2018-01-07 12:28:08

标签: javascript printing silent

我正在尝试使用PrintNode-JS使用javascript和PrintNode远程打印服务在浏览器中静默打印。但我无法正确地做到这一点。所以我需要一些正确的javascript代码。我正在尝试以下代码: https://www.printnode.com/

<script type='text/javascript'>

    var API_KEY = '<your_api_key_here>';

    // got websocket
    if (!PrintNode.WebSocket.isSupported()) {

        // once a websocket is authenticated you can register
        function authenticated (authData) {
            if (authData.error) {
                // most likely not authenticated, see authData.error for more detail
                return;
            }
            // authData will contain information about accountId, permissions, and maxSubscriptions

            // ok, now make some requests to the server to get data you're interested in
            // pass in optional second argument to have this called when server publishes a event
            // but you can also use the event emitter
            this.getScales({}, function (measurement) {
                // this is only meaningful if the websocket is running on the same machine
                // as the running PrintNode client
                console.log("scales data by subscription callback", measurement);
                console.log("scales latency %dms", measurement.getLatency());
            });

        }

        // error callback fired if anything goes wrong including
        //  - exceptions thrown by subscription callbacks
        //  - network issues which cause socket to fail or any timeouts
        //  - errors in printnode server or client libs
        function errorCallback (err, data) {
            console.log("Error!", err, data);
        }

        // instantiate a new
        var ws = new PrintNode.WebSocket(
            {apiKey: API_KEY},
            authenticated,
            errorCallback
        );

        // subscribe to all computer events
        ws.subscribe("computer", function computerEvent (something, info) {
            // lots of things could come in on this event
            // for now we're only interested in scalesMeasurements
            if (something instanceof PrintNode.ScalesMeasurement) {
                console.log("scales data by computer subscription", something);
            }
        });

        // subscribe to all scales events
        ws.subscribe("scales", function computerEvent (measurement) {
            console.log("scales data by scales subscription", measurement);
        });

        // debugging
        ws.subscribe('system.state', function stateChange (newState) {
            console.log("newState", newState);
        });

    // TODO fallback to polling
    } else {

        var httpOptions = {
            success: function (responseBody, repsonse) {
                console.log("success", response);
            },
            error: function (responseBody, repsonse) {
                console.log(response);
            },
            complete: function (response) {
                console.log(
                    "%d %s %s returned %db in %dms",
                    response.xhr.status,
                    response.reqMethod,
                    response.reqUrl,
                    response.xhr.responseText.length,
                    response.getDuration()
                );
            },
            timeout: function (url, duration) {
                console.log("%s timeout %d", url, duration);
            }
        };

        var http = new PrintNode.HTTP(
            new PrintNode.HTTP.ApiKey(API_KEY),
            httpOptions
        );

        http.scales(
            {success: function (response) {
                console.log("success from callback", response);
            }},
            {computerId: 0}
        ).then(
            function (response, info ) {
                console.log("success from promise", response);
            },
            function (err) {
                throw "simulated exception in a promise callback";
            }
        // If your promise callbacks start throw[ing] you can 'catch' this in the returned
        // promise from .then() but this is getting pretty meta right here.
        // Callbacks really shouldn't throw exceptions in async code like this.
        ).then(null, console.log.bind(console, "promise callbacks threw error;"));

    }

    </script> 

1 个答案:

答案 0 :(得分:0)

问题是示例代码中存在拼写错误。

查找并替换&#34; repsonse&#34;通过&#34;响应&#34;在websocket示例中

此外,在HTTP客户端示例中,要打印的功能应为

api.createPrintjob(options, printJobPayload)

而不是

api.printjob ...