Untitled

 avatar
unknown
plain_text
5 months ago
3.5 kB
11
Indexable
const net = require('net');

// Define the port and host for the TCP server
const SERVER_HOST = '0.0.0.0'; // Replace with the actual host if needed
const SERVER_PORT = 8888;        // Replace with the actual port

const receiptLines = [
    "${POS 1}---New Transaction--",
    "${POS 1} 1 Stella Ros 12.99",
    "${POS 1} 1 Bag Fee     0.10",
    "${POS 1}Paid Cash: $20.00",
    "${POS 1}Change Due: $5.62.",
    "${POS 1}---New Transaction--",
    "${POS 1} 1 Aquafina:W  2.69",
    "${POS 1} 1 Sprite      2.49",
    "${POS 1}Change Due: $0.46.",
    "${POS 1}Paid Cash: $6.00",
    "${POS 1}---New Transaction--",
    "${POS 1} 1 Hawaii vol  1.49",
    "${POS 1}No Change Due.",
    "${POS 1}Paid Check: $1.54",
    "${POS 1}---New Transaction--",
    "${POS 1} 1 Path water  2.99",
    "${POS 1} 1 Oh Fresh M  2.99",
    "${POS 1} 1 Oh Fresh M  2.99",
    "${POS 1} 1 Shonen      2.99",
    "${POS 1} 1 Shonen      2.99",
    "${POS 1} 1 Pepsi Nitr  1.99",
    "${POS 1} 1 Mountain D  2.39",
    "${POS 1} 2 grabba lea  3.98",
    "${POS 1} 1 Bag Fee     0.10",
    "${POS 1}Paid Cash: $26.00",
    "${POS 1}Change Due: $0.49.",
    "${POS 1}---New Transaction--",
    "${POS 1} 1 GAME LEAF   5.59",
    "${POS 1}Change Due: $0.19.",
    "${POS 1}Paid Cash: $6.25",
    "${POS 1}---New Transaction--",
    "${POS 1} 1 Modelo Esp 26.99",
    "${POS 1} 1 Marlboro R 11.24",
    "${POS 1}Paid Cash: $100.00",
    "${POS 1}Change Due: $57.87.",
    "${POS 1}---New Transaction--",
    "${POS 1} 1 Cheetos     2.79",
    "${POS 1} 1 XX Large b  2.99",
    "${POS 1} 1 Coca-Cola:  2.49",
    "${POS 1} 1 Bag Fee     0.10",
    "${POS 1}Change Due: $11.37.",
    "${POS 1}Paid Cash: $20.00",
    "${POS 1}---New Transaction--",
    "${POS 1} 1 Mike&Mega   2.49",
    "${POS 1}Paid Cash: $5.00",
    "${POS 1}Change Due: $2.51.",
    "${POS 1}---New Transaction--",
    "${POS 1} 1 Miller Gen 14.99",
    "${POS 1} 1 Cazo De Or  5.49",
    "${POS 1}Change Due: $17.66.",
    "${POS 1}Paid Cash: $40.00",
    "${POS 1}---New Transaction--",
    "${POS 1} 2 Medicine $  2.98",
    "${POS 1} 1 Lifesavers  3.49",
    "${POS 1} 1 Sourly Rai  3.99",
    "${POS 1} 1 Sprite      3.99",
    "${POS 1} 1 Mountain D  2.69",
    "${POS 1} 1 Fanta:Pine  2.49",
    "${POS 1} 1 Coca-Cola   2.49",
    "${POS 1} 1 Bag Fee     0.10",
    "${POS 1} 1 Merchant F  0.60",
    "${POS 1}Paid   DEBIT: $24.30",
    "${POS 1}No Change Due.",
    "${POS 1}---New Transaction--",
    "${POS 1}"
]

// Create the TCP server
const server = net.createServer((socket) => {
    console.log('Client connected');


    // Function to send lines with a delay between each
    let lineIndex = 0;
    const sendLine = () => {
        if (lineIndex < receiptLines.length) {
            socket.write(receiptLines[lineIndex] + '\n');
            lineIndex++;
        } else {
            lineIndex = 0; // Reset the index to start over again
        }

        // Continue sending lines with a delay of 500ms between lines
        setTimeout(sendLine, 500);
    };

    // Start sending lines as soon as the client connects
    sendLine();

    // Handle client disconnection
    socket.on('end', () => {
        console.log('Client disconnected');
    });

    // Handle errors
    socket.on('error', (err) => {
        console.error(`Server error: ${err.message}`);
    });
});

// Start listening on the specified host and port

server.listen(SERVER_PORT, SERVER_HOST, () => {
    console.log(`Server listening on ${SERVER_HOST}:${SERVER_PORT}`);
});
Editor is loading...
Leave a Comment