TCP服务器在某些设备上无法正常工作

时间:2020-10-19 13:04:27

标签: node.js electron node-ipc

TCP Server在某些设备上无法正常工作。我正在使用tcp套接字在电子和.net应用程序之间进行通信。我正在电子应用程序上创建服务器。这是我的服务器:

const ipc = require('node-ipc');
const Store = require('electron-store');
const store = new Store();

const config = store.get('gastro-club');
const BASE_URL = "https://gastroclub.com.tr/hm/api/";
const CONTENT_TYPE = "application/json; charset=utf-8";
const userParameter = config.user_parameter;
const username = config.username;
const password = config.password;
const ip = config.ip;
const port = config.port;
const token = "Basic " + btoa(username + ":" + password);


ipc.config.id = 'world';
ipc.config.retry = 1500;
ipc.config.sync = true;
ipc.config.networkHost = (ip)?(ip):'127.0.0.1';
ipc.config.networkPort = (port)?(port):9700;
ipc.config.rawBuffer = true;
ipc.config.encoding = 'utf8';

ipc.serveNet();
ipc.server.start();


    console.log('TCPServer.js file');
    ipc.server.on(
        'connect',
        function (socket) {
            //console.log("connection", socket);
            ipc.log('got connection');

        }
    );
    ipc.server.on(
        'data',
        function (data, socket) {
            ipc.log('got a message', data,data.toString());
            if (data) {
                let jsRaw = data.toString();
                let json;
                try{
                    json=JSON.parse(jsRaw);
                }catch (e) {
                    console.log(e);
                }

                if (json && json.message === "gastro") {
                    let code= json.code;
                    let price= json.total;
                    //console.log('open gastro');
                    //ipcRenderer.send('key:gastro', json.data);
                    console.log(json);
                    getDiscount(code,price, socket);
                    //testDiscount(code,price, socket);
                }else {
                    ipc.server.emit(
                        socket,
                        '{"Result":"false","Errors":[{"Message":"Alınan mesaj geçerli formatta değil."}],"Message":"Alınan mesaj geçerli formatta değil.","DiscountRate":"null","PromotionCode":"null","FullName":"null","CampaignTitle":"null"}'
                    );
                }
            }
        }
    );
function TCPServerRun() {

}

function getDiscount(code, price, socket) {
    //console.log('func', userParameter, token);
    $.ajax({
        url: BASE_URL + "Check",
        contentType: CONTENT_TYPE,
        dataType: "json",
        type: "POST",
        async: false,
        headers: {
            'Authorization': token,
        },
        data: JSON.stringify({
            "Code": code,
            "UserParameter": userParameter,
            "Price": price
        }),
        success: function (data) {
                //doğrulama başarılı
                ipc.server.emit(
                    socket,
                    JSON.stringify(data)
                );
        },
        error: function (jqXHR, textStatus) {
            //error
            ipc.server.emit(
                socket,
                '{"Result":"false","Errors":[{"Message":"Gastro ile iletisim saglanamadi."}],"Message":"Gastro ile iletisim saglanamadi.","DiscountRate":"null","PromotionCode":"null","FullName":"null","CampaignTitle":"null"}'
            );
        }
    });
}
function testDiscount(code, price, socket){
    let data={
        "DiscountRate": "15",
        "Result": true,
        "Message": "Kod Doğrulama Başarılı",
        "PromotionCode": "GC93899",
        "CampaignTitle": "Restoran İndirimi"
    };
    ipc.server.emit(
        socket,
        JSON.stringify(data)
    );
}

module.exports.TCPServerRun = TCPServerRun;

使用此配置,它可以在大多数计算机上使用,但是在某些设备上,它正在连接到服务器但不接收数据。在关闭连接并重试之后,它会返回旧消息。

0 个答案:

没有答案