N
N
nepster-web2014-01-09 20:10:41
PHP
nepster-web, 2014-01-09 20:10:41

A few questions about working with the EventSource library?

I use this thing https://github.com/Yaffle/EventSource
More or less figured out what's what. But still there are a few questions to understand.

Question number 1, what is the loop for?

header('Content-Type: text/event-stream');
        header('Cache-Control: no-cache');
        header("Access-Control-Allow-Origin: *");
        
        $start = time();
        
        $lastEventId = floatval(isset($_SERVER["HTTP_LAST_EVENT_ID"]) ? $_SERVER["HTTP_LAST_EVENT_ID"] : 0);
        if ($lastEventId == 0) 
        {
            $lastEventId = (isset($_GET["key"])) ? (string)$_GET["key"] : 0;
        }
        
        $id = 0;
        
        while(true) 
        {
            if ((time() - $start) > 10) 
            {
                break;
            }
        
            $updateProposals = array($id);
        
            //echo ":" . str_repeat(" ", 2048) . "\n"; // 2 kB padding for IE
            echo "retry: 2000\n";
            echo "id: {$lastEventId}" . PHP_EOL;
            echo "data: " . ProcessingData::load()->getJson($updateProposals) . PHP_EOL;
            echo PHP_EOL;
            
            ob_flush();
            flush();
            
            
            $id += 1;
            sleep(4);
        }

There is such a code. It works and the connection keeps 10 seconds.
If you remove this cycle, then after each return of data there will be a reconnect. Even if you leave
ob_flush();
flush();

Can you please tell me why to use a loop here?

question 2, disconnection
After 10 seconds expires, a reconnect is in progress.
Then the action error is thrown in js
var es = new EventSource("/games/game/updateProposals/?id=<?=$game->game_id?>");
  
                    var open = function (event) 
                    {
                        console.log('Открытие');
                    }; 
                    
                    var listener = function (event) 
                    {
                        console.log('OK');
                        $('.test').html(Math.random());
                    };  
                        
                    var error = function (event) 
                    {
                        //if (event.readyState == EventSource.CLOSED) 
                        if (EventSource.CLOSED == 2) 
                        {
                            console.log('Соединение закрыто');
                        }            
                        else
                        {
                            console.log('Другая ошибка');
                        }    
                    }; 
                
                    
                    es.addEventListener("open", open);
                    es.addEventListener("message", listener);
                    es.addEventListener("error", error);

And here is such a moment that the documentation says event.readyState , although in fact event.target.event.readyState comes with a value of 2 and reconnects again for 10 seconds.
Then if you remove the code:
if ((time() - $start) > 10) 
            {
                break;
            }

Then the reconnect will be in about 30 seconds.
Please tell me why the reconnect occurs anyway, even if we do not exit the loop
And question number 3. What is the best way to update in real time using this library, how many seconds will it be optimal to keep the connection, what should be the cleanliness of the update (that is, how many seconds to send a request), how often do you need to reconnect?
Please do not offer node.js, since I use the node after everything.
For reference. When the user is on the site (on the application page of the game or waiting for an opponent), we update the data so that he can see who has joined the application, and so on. After the application has already been formed by the play button, the user flies into the game, which already uses the node.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
avalak, 2014-01-09
@avalak

0. The library you mention every time is polyfill. It is needed to implement SSE in old and crooked browsers. It makes no sense to write about it every time (especially since you are trying to make a server).
1. This is a demo from the library. Just to see if the library works.
I don't even know what to answer to that. A cycle is needed to perform actions cyclically. Your CO.
What happens in the loop:
- if more than 10 seconds have passed, exit the work
- write data
- flush the buffer
- sleep for 4 seconds
What were you waiting for? The script did its job and died.

Even if you leave
ob_flush();
flush();

This is to force data to be sent to the user.
PHP has a limited lifetime. He was made to die. Dies after 30 seconds (probably exactly 30 seconds is specified in the settings).
Again. This is a browser library. In the case of SSE, the connection must not be terminated. In your case it is breaking due to using php.
In order not to break (optimal application) you need to use node, tornado or something similar

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question