Skip to content

Commit

Permalink
Fix socket response
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 committed Nov 13, 2024
1 parent 8552b0e commit 5a91776
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
19 changes: 14 additions & 5 deletions ZplEscPrinter/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ const defaults = {
};

$(function () {
$(window).bind('focus blur', function () {
$('#panel-head').toggleClass('panel-heading-blur');
$(window).bind('focus', function () {
$('#panel-head').removeClass('panel-heading-blur');
});
$(window).bind('blur', function () {
if (document.activeElement && document.activeElement.tagName === 'IFRAME') return;
$('#panel-head').addClass('panel-heading-blur');
});

// todo only on first run
if (!global.localStorage.getItem('isOn')) {
Object.entries(defaults).forEach(function ([k, v]) {
Expand Down Expand Up @@ -248,13 +251,19 @@ function startTcpServer() {
peerAddress: sock.remoteAddress,
peerPort: sock.remotePort
};
sock.write(JSON.stringify({success: true}));

sock.on('data', async function (data) {
notify('{0} bytes received from Client: <b>{1}</b> Port: <b>{2}</b>'.format(data.length, clientSocketInfo.peerAddress, clientSocketInfo.peerPort), 'print', 'info', 1000);
//console.log(String.fromCharCode.apply(null, new Uint8Array(data)));
const regex = /POST.*\r\n\r\n/gs;
const code = (data || '').replace(regex,'');
if (regex.test(data)) {
const response = JSON.stringify({success: true});
sock.write('HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: ' + Buffer.byteLength(response) + '\r\n\r\n' + response);
data = data.replace(regex,'');
}
sock.end();

const code = data + '';
if (code.includes('Host:') && code.includes('Connection: keep-alive') && code.includes('HTTP')) {
console.log('It\'s an ajax call');
return;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zpl-escpos-printer",
"productName": "Zpl/EscPos Printer",
"version": "3.0.4",
"version": "3.0.5",
"description": "Printer emulator for Zpl/EscPos rendering engine.",
"main": "main.js",
"scripts": {
Expand Down

0 comments on commit 5a91776

Please sign in to comment.