Websockets - Receive data (survey?)

This might be a dumb question for those who have already wrapped around him, and maybe I just need more .

Question: Using or it looks like polling is still happening. Is it correct?

Example (not a real project): I want to monitor a text file. If I am missing something (more coffee?), I still have to either a) Ask the server if there is an update, or b) Report the page I have an update; Through sleeping PHP code for a given time or using setTimeout a client-side loop.

Things I understand: I definitely see an advantage already in conversations between the server and the page. I see that I am not sending HTTP requests. Therefore, I see the benefits.

Details: I always used xmlhttprequest , so I decided to check all these things on the network, as from what I thought I understood was that the data is sent to the client in real time, but, as mentioned above, if I don’t see something or some kind of logic here, it seems to me that I still need to either tell or to check the intervals for the data, otherwise the data is sent in an infinite loop (imagine that you are making a mysql call).

Maybe my logic in my code is all bad. You can view it. Of all the examples I found, each seems to just start an infinite loop in PHP

PHP (minus all connection jargon)

while(true) {
    // update once a second
    $this->send($client, file_get_contents('/my/file/test.txt'));
    sleep(1);
}

Javascript

var websocket = new WebSocket( "ws://mysite.com:12345" );

websocket.onmessage = function( str ) {
    console.log( str.data );
};

I just don’t understand the logic of this in how I can do this in real time without any polling. Perhaps this is how it should work.

I understand that if I delete sleep from , more and more it turns out in real time, too much, but it looks like it will endlessly try the file in the above example, and this seems wrong.

Edit: To clarify, I'm not specifically looking for a specific solution for viewing a text file. Perhaps you thought about this if you had a question.

: , : , , , .

+4
3

- , ( Sub/Pub ).

, , - "" "" .

, .

PHP ( , ), Ruby Plezi Real-Time Framework.

touch . , , API - .

, .

, plezi gem [sudo] gem install plezi ( sudo ) IRB irb . :

require 'plezi'

class RootController
    def index
        %{<html><head>
<script>
    var websocket = NaN;
    function connect() { websocket = new WebSocket( (window.location.protocol.match(/https/) ? 'wws' : 'ws') + '://' + window.location.hostname + (window.location.port == '' ? '' : (':' + window.location.port) ) + "/" ); }
    function init()
    {
        connect()
        websocket.onopen = function(evt) { WriteMessage("(Connected and waiting for messages)", "connection") };
        websocket.onclose = function(evt) { WriteMessage("(Disconnected. messages will be lost)", "connection");connect();  };
        websocket.onmessage = function(evt) {
            WriteMessage(evt.data, "");
        };
        websocket.onerror = function(evt) { WriteMessage(evt.data, 'error'); };
    }
    function WriteMessage( message, message_type )
    {
        if (!message_type) message_type = 'received'
        var msg = document.createElement("p");
        msg.className = message_type;
        msg.innerHTML = message;
        document.getElementById("output").appendChild(msg);
    }
    function Send(message)
    {
        WriteMessage(message, 'sent'); 
        websocket.send(message);
    }
    window.addEventListener("load", init, false);
  </script></head>
<body>
<p>Messages should show up here:</p>
<div id=output></div>
</body>
</html>
        }
    end
    def touch
        FileController.touch
        "You Touched the file, a message should be sent to the web browser windows."
    end
    def on_message data

    end
    def self.push_update_event
        RootController.broadcast :notify_client, "The file was updated."
    end
    protected
    def notify_client data
        response << data        
    end
end

class FileController
    def self.touch
        GR.info "A file should be touched"
        puts "you can do whatever you feel like here..."
        RootController.push_update_event
    end
end

class APIController
    def touched
        RootController.push_update_event
    end
end

listen

route '/', RootController
route '/api', APIController

exit # the server will start once you exit the irb terminal

:

" " () script, http://localhost:3000/api/touched, ( , ).

0

- .

$sFile = "/my/file/test.txt";
$timeMod = filemtime($sFile);
while(true) {
    if (filemtime("SomeFileHere.txt")!==$timeMod ) {
        $timeMod = filemtime($sFile);
        // File has changed, update variable with new timestamp
        $this->send($client, file_get_contents($sFile));
    } else {
        // No change, do nothing here.
    }
    sleep(1);
}

. , .

- - , PHP , (, dameon). , .. , , PHP script/thread .

, . . (, /).. , .

, , , , , . , 10 , , .

/ ?

python 20 000 ... (GPS-). , python . 130

PHP, , , , .

0

. , . PHP , .

nodejs/socket.io, php- , - . nodejs .

-4

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


All Articles