<?phpif(rand(1,3) == 1){/* Fake an error */header("HTTP/1.0 404 Not Found");die();}
/* Send a string after a random number of seconds (2-10) */sleep(rand(2,10));echo("Hi! Have a random number: " . rand(1,10));?>
<html><head><title>BargePoller</title><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<style type="text/css" media="screen">body{ background:#000;color:#fff;font-size:.9em; }.msg{ background:#aaa;padding:.2em; border-bottom:1px #000 solid}.old{ background-color:#246499;}.new{ background-color:#3B9957;}.error{ background-color:#992E36;}</style>
<script type="text/javascript" charset="utf-8">function addmsg(type, msg){/* Simple helper to add a div.type is the name of a CSS class (old/new/error).msg is the contents of the div */$("#messages").append("<div class='msg "+ type +"'>"+ msg +"</div>");}
function waitForMsg(){/* This requests the url "msgsrv.php"When it complete (or errors)*/$.ajax({type: "GET",url: "msgsrv.php",
async: true, /* If set to non-async, browser shows page as "Loading.."*/cache: false,timeout:50000, /* Timeout in ms */
success: function(data){ /* called when request to barge.php completes */addmsg("new", data); /* Add response to a .msg div (with the "new" class)*/setTimeout(waitForMsg, /* Request next message */1000 /* ..after 1 seconds */);},error: function(XMLHttpRequest, textStatus, errorThrown){addmsg("error", textStatus + " (" + errorThrown + ")");setTimeout(waitForMsg, /* Try again after.. */15000); /* milliseconds (15seconds) */}});};
$(document).ready(function(){waitForMsg(); /* Start the inital request */});</script></head><body><div id="messages"><div class="msg old">BargePoll message requester!</div></div></body></html>
from random import randintfrom time import sleepfrom django.http import HttpResponse, HttpResponseNotFound
def retmsg(request):if randint(1,3) == 1:return HttpResponseNotFound('<h1>Page not found</h1>')else:sleep(randint(2,10))return HttpResponse('Hi! Have a random number: %s' % str(randint(1,10)))
<?
header('Content-type: multipart/x-mixed-replace; boundary=endofsection');
// Keep in mind that the empty line is important to separate the headers// from the content.echo 'Content-type: text/plain
After 5 seconds this will go away and a cat will appear...--endofsection';flush(); // Don't forget to flush the content to the browser.
sleep(5);
echo 'Content-type: image/jpg
';
$stream = fopen('cat.jpg', 'rb');fpassthru($stream);fclose($stream);
echo '--endofsection';
const http = require('http');
const server = http.createServer((req, res) => {SomeVeryLongAction(res);});
server.on('clientError', (err, socket) => {socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');});
server.listen(8000);
// the long running task - simplified to setTimeout here// but can be async, wait from websocket service - whatever reallyfunction SomeVeryLongAction(response) {setTimeout(response.end, 10000);}
const http = require('http');var responsesArray = [];
const server = http.createServer((req, res) => {// not dealing with connection// put it on stack (array in this case)responsesArray.push(res);// end this is where normal api flow ends});
server.on('clientError', (err, socket) => {socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');});
// and eventually when we are ready to resolve// that if is there just to ensure you actually// called endpoint before the timeout kicks infunction SomeVeryLongAction() {if ( responsesArray.length ) {let localResponse = responsesArray.shift();localResponse.end();}}
// simulate some action out of endpoint flowsetTimeout(SomeVeryLongAction, 10000);server.listen(8000);
如你所见,你真的可以响应所有连接,第一,做任何你想做的事情。每个请求都有id,所以你应该能够使用map和访问特定的out of api调用。