I was able to print raw ZPL commands from PHP directly to the printer, except that I can't print more than 1 label at once after windows update to windows-10 on the TLP 2844-Z printer and my first time when installing WebClientPrint Processor (WCPP) in windows-10. When I was trying to emulate ZPL printer in the ZPL Printer app it also happened. The only exception was when I try this on the mac Safari browser, it's doing fine.
Working request script (still working in Safari, and previously in all other browser):
for(var i=0; i<rows.length; i++){
javascript:jsWebClientPrint.print('useDefaultPrinter=' + $('#useDefaultPrinter').attr('checked') + '&printerName=' + $('#installedPrinterName').val() + '¶m=' + rows[i].value);
}
What's preventing me was the permission asking:
on Chrome weren't generated as many time as the request were (which aren't the problem on Safari).
Example when request were 2:
it only ask for permission once, resulting (only 1 label printed):
when it supposed to be (2 labels printed):
I was able to reproduce the above by using the following script:
for (var i = 0; i < rows.length; i++) {
var url = ('useDefaultPrinter=' + $('#useDefaultPrinter').attr('checked') + '&printerName=' + $('#installedPrinterName').val() + '¶m=' + rows[i].value);
window.open('webclientprint:' + domain + url);
}
Which aren't ideal since many tabs would be generated only to print, where previously you don't need any new tab to do the same.
Any idea how to solve this? So that it would print as many as the request ask?
What I did to solve this was to make each request on a separate tab and closed the tab once it's executed. To make it simple I make it into a separate function.
Request script changed into:
for (var i = 0; i < rows.length; i++) {
if (i > 0)
delayPrint(rows[i], i); // separate function
else
javascript: jsWebClientPrint.print('useDefaultPrinter=' + $('#useDefaultPrinter').attr('checked') + '&printerName=' + $('#installedPrinterName').val() + '¶m=' + rows[i].value);
}
separate function used for delaying request and to make each request on a separate tab and closed the tab once it's executed:
function delayPrint(data, interval) {
setTimeout(function() {
var wnd = window.open('webclientprint:' + domain + ('useDefaultPrinter=' + $('#useDefaultPrinter').attr('checked') + '&printerName=' + $('#installedPrinterName').val() + '¶m=' + rows[i].value));
setTimeout(function() {
wnd.close(); // close once it's done
}, 1000);
}, interval * 3000);
}
Related
tried the code below as provided in the official site - https://www.npmjs.com/package/wait.for. But not working as expected.
Output:
*before calling test
after calling test
reverse for 216.58.196.142: ["syd15s04-in-f14.1e100.net"]*
Expected output:
*before calling test
reverse for 216.58.196.142: ["syd15s04-in-f14.1e100.net"]
after calling test*
What is that I can do to make it work?
var dns = require("dns"), wait=require('wait.for');
function test(){
var addresses = wait.for(dns.resolve4,"google.com");
for (var i = 0; i < addresses.length; i++) {
var a = addresses[i];
console.log("reverse for " + a + ": " + JSON.stringify(wait.for(dns.reverse,a)));
}
}
console.log("before calling test");
wait.launchFiber(test);
console.log("after calling test");
wait.launchFiber(test);
Means launch and forget. launchFiber starts a concurrent execution fiber. Inside the fiber you can use wait for, but the fiber is concurrent with the main execution thread.
So this is my task:
You must collect the complete content provided to you by each of the
URLs an d print it to the console (stdout). You don't need to print
out the length, just the data as a String; one line per URL. The catch
is that you must prin t them out in the same order as the URLs are
provided to you as command-line arguments.
And I'm doing it like this:
var http = require('http');
var dataStream = [];
var dataArr = [];
var count = 0;
/*
Function to print results
#dataArr - array
*/
function printResults(dataArr) {
for (var i = 0; i < process.argv.length - 2; i++)
console.log(dataArr[i]);
}
/*
Function to get data from http
#i - int
Getting command line arguments as parametrs.
*/
function httpGet(i) {
http.get(process.argv[2 + i], function(res) {
res.setEncoding('utf8');
res.on('data', function(data) {
dataStream.push(data);
});
res.on('end', function() {
dataArr[i] = (dataStream.join(""));
dataStream = [];
count++;
if (count == process.argv.length - 2) {
printResults(dataArr);
}
});
res.on('error', function(e) {
console.log("Got error: " + e.message);
});
});
}
for (var i = 0; i < process.argv.length - 2; i++) {
httpGet(i);
}
And for some reason sometimes it stores data in array as it supposed, but sometimes it breaks and outputs complete nonsense.
Some results examples:
When working:
$ learnyounode verify program.js
Your submission results compared to the expected:
────────────────────────────────────────────────────────────────────────────
────
1. ACTUAL: "Shazza got us some trackies when as stands out like dog's ba
lls. Grab us a show pony heaps he hasn't got a lurk. She'll be right rubbish
mate it'll be budgie smugglers. You little ripper bloke heaps we're going t
op end. He's got a massive bog standard also built like a freckle. "
1. EXPECTED: "Shazza got us some trackies when as stands out like dog's ba
lls. Grab us a show pony heaps he hasn't got a lurk. She'll be right rubbish
mate it'll be budgie smugglers. You little ripper bloke heaps we're going t
op end. He's got a massive bog standard also built like a freckle. "
2. ACTUAL: "As dry as a sook and as dry as a cleanskin. As cunning as a
metho where get a dog up ya parma. "
2. EXPECTED: "As dry as a sook and as dry as a cleanskin. As cunning as a
metho where get a dog up ya parma. "
3. ACTUAL: "Gutful of gyno how come a mokkies. It'll be clacker and buil
t like a holy dooley!. Get a dog up ya boozer heaps come a captain cook. "
3. EXPECTED: "Gutful of gyno how come a mokkies. It'll be clacker and buil
t like a holy dooley!. Get a dog up ya boozer heaps come a captain cook. "
4. ACTUAL: ""
4. EXPECTED: ""
And an example when it's not working:
$ learnyounode verify program.js
Your submission results compared to the expected:
────────────────────────────────────────────────────────────────────────────
────
1. ACTUAL: "of bogan with it'll be rort. He hasn't got a give it a burl
flamin you little ripper dinky-di. Watch out for the mate's rate to shazza g
ot us some swag. "
1. EXPECTED: "He's got a massive op shop to you little ripper corker. Gutf
ul of bogan with it'll be rort. He hasn't got a give it a burl flamin you li
ttle ripper dinky-di. Watch out for the mate's rate to shazza got us some sw
ag. "
2. ACTUAL: "You little ripper thongs when as stands out like ropeable. T
rent from punchy boardies bloody as cunning as a brisvegas. "
2. EXPECTED: "You little ripper thongs when as stands out like ropeable. T
rent from punchy boardies bloody as cunning as a brisvegas. "
3. ACTUAL: "As dry as a uluru when come a scratchy. Flat out like a ute
with get a dog up ya chrissie. As busy as a fair go no worries it'll be fair
dinkum. She'll be right freo when it'll be cracker. He's Watch got out a fo
r massive the op crook shop my to as you busy little as ripper a corker. bru
mby. Gutful "
3. EXPECTED: "As dry as a uluru when come a scratchy. Flat out like a ute
with get a dog up ya chrissie. As busy as a fair go no worries it'll be fair
dinkum. She'll be right freo when it'll be cracker. Watch out for the crook
my as busy as a brumby. "
4. ACTUAL: ""
4. EXPECTED: ""
Don't treat the data you receive on res.on("data") as an array. Instead, treat it as a string and define it as a variable within the http function (not as a global variable) and do str += data.
Alternatively, you could look at using a library like Async to manage the correct ordering of the async functions you need to execute, as you need each Async to be executed and returned in sequential order.
So the problem was:
Your code makes the assumption that the 3 http responses will not
overlap - that a data event for one response will never occur before
the end event of the previous response, this is not always the case. I
recommend you move the definition of your dataStream variable inside
your httpGet function, this way each request/response will have its
own variable and they cannot interfere with each other, regardless of
timing.
So I refactored my solution to look like this, and now it works 100% of the time:
var http = require('http');
var dataArr = [];
var count = 0;
/*
Function to print results
#dataArr - array
*/
function printResults(dataArr) {
for (var i = 0; i < process.argv.length - 2; i++) {
console.log(dataArr[i].replace('undefined', ''));
}
}
/*
Function to get data from http
#i - int
Getting command line arguments as parametrs.
*/
function httpGet(i) {
http.get(process.argv[2 + i], function(res) {
res.setEncoding('utf8');
res.on('data', function(data) {
dataArr[i] += data;
});
res.on('end', function() {
count++;
if (count == process.argv.length - 2) {
printResults(dataArr);
}
});
res.on('error', function(e) {
console.log("Got error: " + e.message);
});
});
}
for (var i = 0; i < process.argv.length - 2; i++) {
httpGet(i);
}
More info: https://github.com/nodeschool/discussions/issues/1270
I've tried nearly every example for scripts I can find. Every sample opens the terminal for a split second. Even this closes as soon as input is entered. Is this normal?
var rl = require('readline');
var prompts = rl.createInterface(process.stdin, process.stdout);
prompts.question("How many servings of fruits and vegetables do you eat each day? ", function (servings) {
var message = '';
if (servings < 5) {
message = "Since you're only eating " + servings +
" right now, you might want to start eating " + (5 - servings) + " more.";
} else {
message = "Excellent, your diet is on the right track!";
}
console.log(message);
process.exit();
});
There are 2 options that control this in Tools/Options/Node.js Tools/General:
Wait for input when process exists abnormally
Wait for input when process exists normally
Taken from https://nodejstools.codeplex.com/discussions/565665
I have a problem with node.js. The commands of the program doesn't load cronologically and i don't know how to do it.
I'm trying to download some images and text from database and send it with packs of 8. But node.js runs for loop and command after loop at the same time.
Here's my code:
socket.on('background_dinamically', function(data){
connection.query("SELECT * FROM products WHERE id='"+data.cathegory+"'" , function(err, rows, fields){
var count = 0;
var array_elements = [];
if(err){
socket.emit('errorserver');
}else{
for (var i = rows.length - 1, count; i >= 0; i-- & count ++) {
array_elements.push(rows[i]);
if (count == 8) {
socket.emit('image_loading_background', [array_elements, data]);
count = 0;
array_elements = [];
}
};
if(count > 0 && count < 8 && count != 0) {
socket.emit('image_loading_background', [array_elements, data]);
}
}
});
});
Marc, first I would check if synchronisation can be done on the client side. If you force your nodejs app to synchronize before sending data to the client, scalability suffers.
If you cannot do without synchronizing on the server side, you can choose between spaghetti code or a sync lib.
Welcome to the world of asynchronous (not chronological) programming. By default, node will work on I/O operations in parallel as you are seeing. To get other behaviors including chronological (in serial), parallel batches, as well as error handling helpers, have a look at one of the many flow control libraries available. Specifically, I recommend caolan/async.
The only solution I can think of is to do it with JS, but I can't pass any variables from the phantom script to the JS I'm trying to execute.
You should take a look at CasperJS. It's a very nice PhantomJS script that allows you to easily perform this kind of web behavior.
As far as communicating with your PhantomJS script, as stands today, you have a few reliable options:
Pass your data in via command line args.
Exchange data via reading/writing of files.
Have your PhantomJS script call your Node.js script via GETS/POSTS.
Yes, there are issue in the QtWebKit bridge between C++ and JS to pass stuff up and down.
It works, but better from JS to C++ than the opposite.
We have a number of issues to address, but this is one of the highest in number one in terms of demand.
In the meanwhile, I usually "decorate" the page object like this:
var page = require("webpage").create();
page.evaluateWithParams = function(func) {
var args = [].slice.call(arguments, 1),
str = 'function() { return (' + func.toString() + ')(',
i, ilen, arg;
for (i = 0, ilen = args.length; i < ilen; ++i) {
arg = args[i];
if (/object|string/.test(typeof arg)) {
str += 'JSON.parse(' + JSON.stringify(JSON.stringify(arg)) + '),';
} else {
str += arg + ',';
}
}
str = str.replace(/,$/, '); }');
return this.evaluate(str);
}
And then you can call a function within the scope of the page like this:
var a = 1, b = 2;
page.evaluateWithParams(function(arg1, args) {
// your code that uses arg1 and arg2
}, a, b);
Hope this helps.
Ivan