Maybe I do not fully understand what is happening inside the XMLHttpRequest, but I have to somehow make a lot of requests without creating a new connection every time. I use a standard set of calls:

function GetFrame(){ var DataRequest = new XMLHttpRequest(); DataRequest.open("POST", "/index.php", true); DataRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); DataRequest.send("Data="+Data); DataRequest.onreadystatechange = function(){ if(DataRequest.readyState === 4){ if(DataRequest.status === 200){ if(DataRequest.responseText.length > 0){ Stream.src = DataRequest.responseText; } } } }; } 

Everything works, the pictures change (video goes), but if you look at the connections in the firewall, the outgoing ones grow like a snowball . The clients that send these pictures are written on the pluses - hooked by the socket to the server and send the stream without increasing connections, and the web interface where the view is performed accesses the server with AJAX. Tried to render to global var DataRequest = new XMLHttpRequest(); - did not help.

Is it possible to somehow keep the connection open and send as many requests on it using XMLHttpRequest? Or is it necessary to rewrite code on web sockets?

PS: In apache config - KeepAliveTimeout 86400 (day), MaxKeepAliveRequests 0 (unlimited)

  • Maybe try adding a DataRequest.setRequestHeader('Connection', 'close') ? - Yaant
  • , does not help: (( - Iceman

2 answers 2

Connections to the server are controlled by the browser. You cannot control them from the script. But you can be sure that if there is a “waiting” connection at the time the request starts, the browser will guess to use it again. Well, if not zaglyuchit. But if this is a browser bug, then you will not even fix it from javascript.

Compounds can accumulate for various reasons. The most likely is that you make many simultaneous requests. You GetFrame() does not report the end of the request - and therefore, they can become too much simply because of the slow connection. See in the browser console how many requests are active at a time.

If you need to find out how to wait until the end of the request before doing the following, you will be asked this question: "How can I return a value from an event or from a callback function?"

If all else fails, try using nginx instead of apache (or put nginx before apache). Nginx works better with multiple connections and is not afraid of their number :)

    XMLHttpRequest - does not know how to wait and is sent every time you call send .
    If you are trying to make some kind of online interface or chat, I advise you to look towards the ws:// protocol

    RFC 6455
    You can push off for example from this chat.

    • Unfortunately, there is a clear installation - no php-demons. Or maybe this request can somehow be forcedly broken after the server’s response, without changing the apache configs? And then the server periodically crashes with the "server reached maxclients setting" log - Iceman
    • XMLHttpRequest - it doesn’t know how, I don’t understand if you have already hooked up a socket to the server, then why do you even use HTTP? When you already have a place where the answer lies - at the expense of the server reached maxclients setting error server reached maxclients setting - apparently, something with the settings of the apache web server itself. - And
    • The server script simply handles POST requests and works with shared memory. A client in C ++ sends requests with data and all the necessary headers, but it "hangs" on one socket. And the web interface, where content sent by C ++ clients is viewed, spawns a bunch of connections with AJAX, and, since KeepAlive is awesome on the server (so that clients do not fall off), these AJAX requests are dead on the server after working off. Maybe you can somehow tell the server that after answering AJAX you need to nail the connection? Or can I send POST through sockets to JS without rewriting the server script? - Iceman
    • In fact, if you use readystatechange in a readystatechange then the request itself is killed, and if you do not use it, you can simply do xhr.abort() - to kill the request. But I don’t understand, if you already have a client that hangs on the server’s socket, then what are you using HTTP for when you can get a response from the socket? - And
    • Customers send frames from the camera. Each client has their own identifier, which is displayed in the list of clients in the web interface. Under each client, the server allocates a block of allocated memory, where frames are rewritten each time data is received. When selecting a client from the list in the web interface, ajax accesses the client id, which serves as the key to shmop_open, and receives a stream of frames from this memory region (in the above code, Data is the client id). There are 25 such requests from the web interface per second, and each creates a new connection (there is no such connection on cpp clients - there is 1 socket). - Iceman