简体   繁体   English

如何连接我的移动应用程序(用 lua 编写)和我的服务器(用 node.js 编写)?

[英]How to connect my mobile app (written in lua) and my server (written in node.js)?

I am new to lua and node js and I am trying to connect the mobile app that I'm working on to the server.我是 lua 和 node js 的新手,我正在尝试将我正在使用的移动应用程序连接到服务器。 The problem is it connects to the server but the data that I am trying to pass gets lost or it doesn't reach the server.问题是它连接到服务器,但我尝试传递的数据丢失或无法到达服务器。 Any ideas on what's wrong with what I'm doing?关于我正在做的事情有什么问题的任何想法?

this is my lua code to connect to the server这是我连接到服务器的 lua 代码

local function store ()

    local headers = {}

    headers["Content-Type"] = "application/x-www-form-urlencoded"
    headers["Accept-Language"] = "en-US"

    local body = "fname="..fname
    local params = {}

    params.headers = headers
    params.body = body

    print(body)
    print(headers)
    print(params.body)
    print(params.headers)

    network.request( "http://192.168.56.2:8888", "POST", networkListener, params )

end



local function networkListener( event )
        if ( event.isError ) then
        print( "Network error!")
    else
        print ( "RESPONSE: " .. event.response )
        local serializedString = json.decode( event.response )

                            print(serializedString)

        --data = json.decode(serializedString)
        --print(serializedString.student[1])

    end
end

` `

This is the code for the simple server I am trying to send a request to这是我尝试向其发送请求的简单服务器的代码

var express = require('express');
var app = express();

var morgan = require('morgan');
var consolidate = require('consolidate');
var bodyparser = require('body-parser');
var parser = require('luaparse');

////////////////////////////////////////////////////////////////////////////////

app.listen(8888,function() {console.log('Server Running!');});

////////////////////////////////////////////////////////////////////////////////

app.set('views', __dirname + '/views');
app.engine('html', consolidate.nunjucks);
app.use(morgan('dev'));
app.use(bodyparser.urlencoded({ extended: true }));
app.use('/static', express.static(__dirname + '/static'));

////////////////////////////////////////////////////////////////////////////////

app.get('/', function(req, res) {
  res.render('index.html');
});

app.post('/', function(req, res) {
    var fname = req.fname;
    var lname = req.body.lastname;

    console.log("it went in");
    console.log(req.body.fname);
    console.log(req.body);
    console.log(req.header);
    console.log("nahuman");


  res.render('index.html');
 });

////////////////////////////////////////////////////////////////////////////////

Your code is Ok, except that it seems your network listener networkListener() is declared after your store() function.您的代码没问题,只是您的网络侦听器networkListener()似乎是您的store()函数之后声明的。 Lua cannot access stuff declared after what you are executing, unless it is forward declared. Lua 不能访问你执行之后声明的东西,除非它是前向声明的。 So, lua does not find the listener and it does not get called, even if there was an error.因此,lua 找不到侦听器,也不会调用它,即使出现错误。 This function should be declared before your store() function, so that it can access it, like this:这个函数应该在你的store()函数之前声明,以便它可以访问它,像这样:

local function networkListener(event)
    ...
end

local function store()
    ...
end

That, or you could forward declare it, sort of like this:那个,或者你可以转发声明它,有点像这样:

local networkListener = nil -- This forward declaration

local function store()
   ...
end

networkListener = function()
    ...
end

Here is more info about lua forward declaration .这是有关 lua 前向声明的更多信息 I know this is the case since you provided us with a screenshot of your actual code order.我知道是这种情况,因为您向我们提供了实际代码顺序的屏幕截图。 You can always use a debugger to see if everything is working OK after you try the solution.在尝试解决方案后,您始终可以使用调试器查看是否一切正常。 I recommend the IDE zerobrane studio .我推荐 IDE zerobrane studio

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM