I'm currently setting up a messaging system with PHP. To realize the "instant" messaging system, I use a unidirectional connection with the server, the SSE. But for my SSE to work, I send data via the url with a http_build_query
: [code JS]
const eventSource = new EventSource("http://localhost/sse_test.php?<?= http_build_query($url) ?>");
Except that I quickly realized that it will not work indefinitely because in my system, the amount of data sent is proportional to the number of conversations and groups: [code PHP]
$url = [];
for ($i = 0; $i < $num; $i++) {
$tableau_url = Array(
"conv" => $conv_table_sse[$i],
"to" => $to_table_sse[$i]
);
array_push($url, $tableau_url);
}
Except that the maximum length of a url is 2048 characters in general. So I did some research and I saw that you can add headers for an SSE connection. So here is what I did: [code JS]
var headers = JSON.parse('<?= json_encode($url) ?>');
const eventSource = new EventSource("http://localhost/sse_test.php",{
withCredentials: true, // allows to send the cookies with the request
headers: headers // add headers to the request
});
Finally I will have to recover my data with this code: [code PHP]
(I didn't put all the code because it doesn't influence this one because before it worked very well with $_GET
)
if (isset($_SERVER)) {
if (!empty($_SERVER)) {
$parametres = $_SERVER;
Except that I have a problem when retrieving the data. It is as if the data I was passing through the headers
did not exist, here is a screen of the var_dump($_SERVER)
:
And this is what should be there:
After explaining my situation to you, I hope that you will be able to enlighten me and explain the errors that I may have inadvertently made.
source https://stackoverflow.com/questions/75784462/headers-in-sse-request
No comments:
Post a Comment