AJAX fire-and-forget, looking for the opposite of the event sent by the server

Are these APIs symmetric with respect to the Server-Sent event to generate fire and forget events from browser to server? I know how not to respond to a request on the server side, but how to tell the browser that it does not need to wait for a response?

The goal is to save resources on the client side, say that you want to send 10k events to the server as quickly as possible, without worrying about what the server will respond.

Change: . This is mostly irrelevant, here are some details about the project I'm working on that will use AJAX fire-and-forget. I want to create a JavaScript network library for Scala.js, which will have one of its applications as a transport layer between Akka actors on the JVM and in the browser (compiled with Scala.js). When WebSockets are unavailable, I want to have some kind of reserve, and having a pending connection over a long route in every JS-> JVM message is unacceptable.

+4
source share
6 answers

" , ?" , .

, Google , .

, javascript src, , .

var image = new Image();
image.src = "your-script.php?id=123&other_params=also";

PROs: /, ajax

: GET .

Edit

:

http://help.yahoo.com/l/us/yahoo/ywa/faqs/tracking/advtrack/3520294.html

https://support.google.com/dfp_premium/answer/1347585?hl=en

.

+3

, , XMLHttpRequest , , 100% , XMLHttpRequest, ?

, XMLHttpRequest , . , no-op. , - , ( ), . , , JavaScript - , .

XMLHttpRequest ( ) . , Microsoft , , , Web 2.0. , XMLHttpRequest, .

XMLHttpRequest, , TCP ( UDP), , , TCP ACK . , . TCP/IP.

, HTTP, HTTP-... ? , . , HTTP- - , , .

, abort() XMLHttpRequest, . , XMLHttpRequest, , . , ? . . .

, ( ): Microsoft XMLHttpRequest Microsoft Visual Interdev, Java , JavaScript .. .

, Java- Sun Microsoft. , - Microsoft , Microsoft, Java, . ? , , , XMLHttpRequest.

, , , ...: -)

, , XMLHttpRequest, .

, Java-, - , . , , , Java , Java. , . , . HTTP -, - - HTTP-, . , TCP , , .

+3

, - 2 . , :

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
  if (xhr.readyState == 2) {
    alert("Response header recived, it was not a fire-and-forget...");
  }
};
xhr.open("POST", "http://www.service.org/myService.svc/Method", true);
xhr.timeout = 2;
xhr.send(null);

, - / (, 1 ). - 50 , (6 ).

+1

XMLHttpRequest (.. , :

var req = new XMLHttpRequest();
req.open('GET', 'http://my.url.goes.here.com');
req.send();

Image, : btw:

new Image().src = 'http://my.url.goes.here.com';

Image , , , XHR. (BTW, , , 1x1 PNG GIF- , , ", , MIME/html".)

0

, . , , .

. . , , - .

0

XHR, WebSockets. , , HTTP-.

, WebSockets XHR, , . :

JS

function sendMessage(message) {
    WebSocketsAvailable ? sendWithWebSockets(message) : sendWithXHR(message);
}

var xhrQueue = [];

function sendWithXHR(message) {
    xhrQueue.push({
        timestamp: Date.now(),  // if this matters
        message: message
    });
}

function flushXhrQueue() {
    if (xhrQueue.length) {
        var req = new XMLHttpRequest();
        req.open('POST', 'http://...');
        req.onload = function() { setTimeout(flushXhrQueue, 5000); };
        // todo: needs to handle errors, too
        req.send(JSON.stringify(xhrQueue));
        xhrQueue = [];
    }
    else {
        setTimeout(flushXhrQueue, 5000);
    }
}

setTimeout(flushXhrQueue, 5000);

There can be two endpoints on a server: one for WebSockets and one for XHR. The XHR handler deserializes the JSON queue object and calls (once per message) the same handler that is used by the WebSockets handler.

Server Pseudo Code

function WSHandler(message) {
    handleMessage(message, Date.now());
}

function XHRHandler(jsonString) {
    var messages = JSON.parse(jsonString);

    for (var messageObj in messages) {
        handleMessage(messageObj.message, messageObj.timestamp);
    }
}

function handleMessage(message, timestamp) {
    ...
}
0
source

Source: https://habr.com/ru/post/1530248/


All Articles