简体   繁体   English

多个发布请求和小型Web服务器

[英]Multiple post requests and small web server

I have a problem with a small web server with limited resources: 我在资源有限的小型Web服务器上遇到问题:

  • The device is an embedded controller with network interface and uses web pages for configuration. 该设备是具有网络接口的嵌入式控制器,并使用网页进行配置。

  • The data are exchanged with json format and the post method. 数据以json格式和post方法交换。

The problem is this: my device can serve only one post request at a time with a small buffer size. 问题是这样的:我的设备一次只能使用一个较小的缓冲区来处理一个发布请求。

To test it, I created a page with multiple posts for sending data to my server. 为了对其进行测试,我创建了一个包含多个帖子的页面,用于将数据发送到服务器。 I opened the page with the browser Firefox 4.0 and the browser tried to open multiple socket to serve all requests in parallel. 我使用Firefox 4.0浏览器打开了该页面,浏览器尝试打开多个套接字以并行处理所有请求。

How do I create a sequential flow message? 如何创建顺序流消息? (I'm not worried about the speed) (我不担心速度)

Here is a small example of how I intend to proceed, but this solution opens two sockets for sending two post requests to my server and aborts one of them. 这是我打算如何进行的一个小示例,但是此解决方案打开了两个套接字,用于向服务器发送两个发布请求,并中止其中的一个。

for (var j=0; j<2; j++) {
    // read page data and create objdata  
    jdata = JSON.stringify(objdata);

    // alert("I am about to POST this:\n\n" + jdata);

   $.post(
     'prgtimetbl.json',
     jdata,
     function(data) {
     //           alert("Response: " + data);
     },
     "json"
   );
}

With jQuery use $.ajax and async: false : 与jQuery一起使用$ .ajaxasync: false

$.ajax({
    type: "POST",
    url: "some.php",
    async: false,
    data: "name=John&location=Boston",
    success: function(msg){
        alert( "Data Saved: " + msg );
    }
});

Cixate was faster than me, but I had a slightly different approach using async queued requests. Cixate比我快,但是我使用异步排队请求的方法略有不同。

Sample: http://jsfiddle.net/HTGPM/ 范例: http//jsfiddle.net/HTGPM/

Code: 码:

$(document).ready(function(){
    var stuffToPost = [{name:'obj1',id:1},{name:'obj2',id:2},{name:'obj3',id:3}];
    var postIndex = 0;

    var postNext = function() {
        if (postIndex < stuffToPost.length)
        {
            $.ajax({
                type: 'post',
                url: '?',
                data: JSON.stringify(stuffToPost[postIndex]),
                success: function(data) {
                    alert('Data '+stuffToPost[postIndex].id+' was sent successfully');
                    postIndex++;
                    postNext();
                },
                error: function() {
                    alert('Something bad happened. Stopping');
                }
            });
        }
        else if (postIndex == stuffToPost.length)
        {
            alert('All data is sent !');
        }
    };

    postNext();
});

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

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