Skip to content

Commit

Permalink
Support utf8 on socket
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 committed Nov 13, 2024
1 parent dfe8839 commit 8552b0e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
7 changes: 4 additions & 3 deletions ZplEscPrinter/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ async function zpl(data){
}
async function escpos(data,b64){
let dataAux = data;
try{ dataAux = atob(data.trim()); }catch(e){}
try{ dataAux = base64DecodeUnicode(data.trim()); b64=true; }catch(e){}

if (!dataAux || !dataAux.trim().length) {
console.warn(`esc/pos = '${data}', seems invalid`);
Expand Down Expand Up @@ -243,17 +243,18 @@ function startTcpServer() {

server.on('connection', function (sock) {
console.log('CONNECTED: ' + sock.remoteAddress + ':' + sock.remotePort);
sock.setEncoding('utf8');
clientSocketInfo = {
peerAddress: sock.remoteAddress,
peerPort: sock.remotePort
};
sock.write(JSON.stringify({success: true}, 'text/html'));
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 = String.fromCharCode.apply(null, new Uint8Array(data)).replace(regex,'');
const code = (data || '').replace(regex,'');
if (code.includes('Host:') && code.includes('Connection: keep-alive') && code.includes('HTTP')) {
console.log('It\'s an ajax call');
return;
Expand Down
7 changes: 4 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//const { updateElectronApp, UpdateSourceType } = require('update-electron-app')
//updateElectronApp()

const { app, BrowserWindow, dialog, ipcMain } = require('electron')
const path = require("path")

if (require('electron-squirrel-startup')) return app.quit();

const { updateElectronApp, UpdateSourceType } = require('update-electron-app')
updateElectronApp()

let win
const createWindow = () => {
win = new BrowserWindow({
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"bootstrap": "^5.3.2",
"electron-squirrel-startup": "^1.0.0",
"glyphicons-only-bootstrap": "^1.0.1",
"jquery": "^3.7.1"
"jquery": "^3.7.1",
"update-electron-app": "^3.0.0"
}
}

0 comments on commit 8552b0e

Please sign in to comment.