节点在https中使用套接字io

时间:2017-12-01 14:52:47

标签: node.js https socket.io

我有一个带https的套接字节点服务器来打印票证。 当我在服务器上吃午饭时没关系,但是当我发送一个套接字进行打印时,服务器崩溃并显示此错误:

node: ../src/util-inl.h:196: TypeName* node::Unwrap(v8::Local<v8::Object>) [with TypeName = node::TLSWrap]: Assertion `(object->InternalFieldCount()) > (0)' failed.
Abandon

我在网上寻找并且人们说要更改版本节点,所以我将版本节点更改为3.8,4.4,8.4和现在9.2并且错误再次弹出...

我给你看了代码:

/**
 * Created by thibault on 13/09/17.
 */

'use strict';

let serverSettings    = require('./config').serverSettings;
let printers          = require('./config').printers;
let authkeyUdooServer = require('./config').authkeyUdoo;
let Log               = require('./utils/log');
let port              = serverSettings.port;
let bodyParser        = require('body-parser');
let socket            = require('socket.io');
let pdf               = require('html-pdf');
let express           = require('express');
let printer           = require('printer');
let moment            = require('moment');
var server            = (serverSettings.useSSL) ? require('https') : require('http');
let fs                = require('fs');
let he                = require('he');
let app               = express();
let logInstantce = new Log();



/*
 |------------------------------------------------------------------------------
 | Body parser
 |------------------------------------------------------------------------------
 */
app.use(bodyParser.json());       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({   // to support URL-encoded bodies
    extended: true,
    type: 'application/x-www-form-urlencoded'
}));

if (serverSettings.useSSL)
{
    var credentials = {
        key:  fs.readFileSync(serverSettings.SSL_KEY),
        cert: fs.readFileSync(serverSettings.SSL_CERT)
    };
    server          = server.Server(credentials, app);
}
else
{
    server = server.Server(app);
}
server.listen(port, function()
{
    console.log('\n');
    console.log(' |------------------------------------------------------------');
    console.log(' |');
    console.log(' | Server NodeSync started');
    console.log(' |');
    console.log(' |------------------------------------------------------------');
    console.log(' |');
    console.log(' |  Port     : ' + serverSettings.port);
    console.log(' |  SSL ?    : ' + ((serverSettings.useSSL) ? 'Yes' : 'No'));
    let url = 'http://' + serverSettings.url;
    if (serverSettings.useSSL)
    {
        console.log(' |  SSL key  : ' + serverSettings.SSL_KEY);
        console.log(' |  SSL cert : ' + serverSettings.SSL_CERT);
        url = 'https://' + serverSettings.url;
    }
    console.log(' |  URL      : ' + url);
    console.log(' |');
    console.log(' |------------------------------------------------------------\n\n');
});

let io = socket(server);

io.on('connection', (socket) => {
    logInstantce.success('New socket connection');
    console.log('socket started! At ' + serverSettings.url);

    socket.on('printTicket', (data) =>
    {
console.log('je passe');
        let htmlString          = he.decode(data.html);
        let width               = data.width;
        let height              = data.height;
        let nbPrint             = data.nbPrint;
        let typePrinter         = data.typePrinter;
        let authkeyUdooAccount  = data.authkeyUdooAccount;
        if (authkeyUdooAccount === authkeyUdooServer)
        {
            let options = {
                "height": height + 'px',        // allowed units: mm, cm, in, px
                "width":  width + 'px',
            };

            let nameFile = 'ticket_' +  moment().format('YYYYMMDDHHmmss');

            fs.writeFile('html/' + nameFile + '.html', htmlString, (err) =>
            {
                if (err)
                {
                    logInstantce.error('create html err : ' + err);
                    socket.emit('printError', 'create html err : ' + err);
                }
                else
                {
                    let html = fs.readFileSync('html/' + nameFile + '.html', 'utf8');

                    pdf.create(html, options).toFile('pdf/' + nameFile + '.pdf', (err, res) =>
                    {
                        if (err)
                        {
                            logInstantce.error('create pdf err : ' + err);
                            socket.emit('printError', 'create pdf err : ' + err);
                        }
                        else
                        {
                            for (let i = 0; i < nbPrint; i++)
                            {
                                printer.printDirect({
                                    printer: printers[typePrinter],
                                    data: fs.readFileSync('pdf/' + nameFile + '.pdf'),
                                    type: 'PDF',
                                    success: (id) =>
                                             {
                                                 logInstantce.success('printed with id ' + id);
                                                 if (i === (nbPrint - 1))
                                                 {
                                                     socket.emit('printSuccess');
                                                 }
                                             },
                                    error: (err) =>
                                             {
                                                 logInstantce.error('error on printing: ' + err);
                                                 if (i === (nbPrint - 1))
                                                 {
                                                     socket.emit('printError', 'error on printing: ' + err);
                                                 }
                                             }
                                })
                            }
                        }

                        //suppression des fichiers
                        fs.unlink('./html/' + nameFile + '.html', (err) =>
                        {
                            if(err) logInstantce.error('create html err : ' + err);

                            fs.unlink('./pdf/' + nameFile + '.pdf', (err) =>
                            {
                                if(err) logInstantce.error('create pdf err : ' + err);

                                logInstantce.success('file pdf and html deleted successfully : ' + nameFile);
                            });
                        });
                    });
                }
            });
        }
        else
        {
            logInstantce.error('wrong autkey udoo');
            socket.emit('printError', 'wrong autkey udoo');
        }
    });
});

之前我在http中使用它并且工作正常。 证书是好的因为如果我想我可以去服务器的网址,它用于其他项目。

所以,如果你有一个想法,我接受它:) 提前谢谢

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。 我设置节点的版本6.0.0并下载我的节点包,这很好:))