Azure Functions和本地TCP服务器

时间:2019-05-08 12:27:40

标签: azure azure-functions

我有一个HTTP触发的Azure函数,该函数从数据库获取数据并将其通过TCP / IP发送到用Python编写的本地服务器。本地化一切正常。但是我将功能部署到Azure门户的那些我无法连接到本地服务器以在那里发送数据。我该如何解决?

下面是我的代码:

module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
const net = require('net');

var Connection = require('tedious').Connection;
var Request = require('tedious').Request;  


// connect to DB
var config = {
    authentication: {
        options: {
            userName: '',
            password: '' 
        },
        type: 'default'
    },
    server: '', 
    options:
    {
        database: '', 
        encrypt: true,
    }
};  

if (req.body) {

    var connection1 = new Connection(config);
        connection1.on('connect', function(err){ 
        console.log("Connected to DataBase");

        var request1 = new Request("SELECT 
KeyFigure.id,KeyFigure.unit_id,KeyFigure.week,KeyFigure.daily_gain,
FROM KeyFigure INNER JOIN KeyFigureDelivery ON 
KeyFigure.id=KeyFigureDelivery.key_figure_id WHERE 
KeyFigureDelivery.delivered=0", function(err, rowCount, rows){  
            console.log(rowCount + ' row(s) returned');
            if (err) {
                var client_1 = new net.Socket();
                client_1.connect(9000, '127.0.0.1', function() {
                    console.log('Connected to Server');
                    client_1.write(JSON.stringify("No data to send...from 
KeyFigure"));//'Data was written to a Data Base.');
                    client_1.destroy(); //close()
                });
                console.log("Error in Database KeyFigure");
                connection1.close();
                console.log("Disconnected_1 because of Error");
            }
            if (rowCount == 0) {
                var client_2 = new net.Socket();
                client_2.connect(9000, '127.0.0.1', function() {
                    console.log('Connected to Server');
                    client_2.write(JSON.stringify("No data to send...from 
KeyFigure"));//'Data was written to a Data Base.');
                    client_2.destroy(); //close()
                });
                connection1.close();
                console.log("Disconnected_1 because of 0 Rows")
            }
        });  

        request1.on('row', function(columns) {
            var DataToSend = [];
            var rowdata = new Object()
            columns.forEach(function(column) {
                //console.log("%s\t%s", column.metadata.colName, 
column.value);
                //DataToSend.push([String(column.metadata.colName) + ":" 
+ String(column.value)])
                    rowdata[column.metadata.colName] = column.value;
                });
            DataToSend = rowdata
            var client1 = new net.Socket();
            client1.connect(9000, '127.0.0.1', function() {
                console.log('Connected to Server');
                client1.write(JSON.stringify(DataToSend));//'Data was 
written to a Data Base.');
                //client.destroy(); //close()
                client1.on('data', function(data) {
                    console.log('Received: ' + JSON.parse(data).id);
                    //client.destroy();
                    var dataIn1 = JSON.parse(data)

                    if (dataIn1.status == 200){
                        var connection_in1 = new Connection(config);
                        connection_in1.on('connect', function(err){ 
                            console.log("Connected");
                            var request_in1 = new Request("UPDATE 
KeyFigureDelivery SET delivered = 1 WHERE key_figure_id= 
"+dataIn1.id+";", function(err, rowCount, rows){  
                            //console.log(rowCount + ' row(s) returned'); 
UPDATE KeyFigureDelivery SET delivered = 1 WHERE key_figure_id= 
"+data+";"
                            if (err) {
                                console.log("Error in Database 
KeyFigure");
                            }
                            });  
                                request_in1.on('row', function(columns) {
                                    columns.forEach(function(column) {                    
                                    });
                                    context.res = {
                                        status: 201, 
                                        body: "Input Data is Valid and 
Accepted.\n"
                                    };
                                    connection_in1.close();      
                                });
                                connection_in1.execSql(request_in1);  
                        }); 
                    }
                    else {
                        console.log("Data was not delivered!!..from 
Server...KeyFigure")
                    } 
                });
                client1.on('error', function(err){
                    console.log("Error: "+err.message);
                });
                //client1.destroy();
                connection1.close();
            });
        });
        request1.on('doneInProc', function (rowCount, more, rows) { 
            connection1.close();
            console.log("Connection 1 closed...")
        });
        connection1.execSql(request1); 
    });
}
else{
context.res = {
        status: 400,
        body: "Please pass a name on the query string or in the request 
body."
    };

};

我需要的是:(azure函数)从数据库获取数据,将其发送到服务器,如果数据有效-服务器向客户端发送一条消息(azure函数),更新数据库中的表。所有这些都通过一个HTTP触发了Azure函数。怎么可能呢?

谢谢您的帮助

1 个答案:

答案 0 :(得分:1)

(当前)Azure Functions不支持Web套接字。有关详细信息,请参见Github线程:https://github.com/Azure/Azure-Functions/issues/738

  

总而言之,在设计,结算和   技术实施。我认为有合理的解决方案   这些问题,而一旦这   该功能在我们的优先级列表中足够高。

如果您有真实案例,则应对该Github线程进行评论。