I have a simple chat program than runs on a php web-socket. There is simple php script that runs on server computer which processes incoming data and forwards it to other sockets.
The questions is how can I check if a a websocket is closed from clients end.
I know that when socket.close(); is called on client side, php script receives 2 bytes of data but when I unmask it, it seems to be empty.
Please can anyone shed little light on this. Thank You
UPADTE:
This is weird.
There is certain data when I unmask I get ♥8. why is this ?
UPDATE 2:
Okay,
Further investigation reveals the following
if socket.close(); is called from your browser. Your socket_recv() function will recieve 6 bytes of data and when unmasked it will have null.
If you browser windows is closed which has websocket active your socket_recv() function will get 8 bytes of data and when unmasked will reveal ASCII characters 3 and 233.
I dont exactly know whats hap
Related
I'm attempting to retrieve a very big list of Strings back from the server (~4500 String, each String is ~50 chars). my Application is running a Java back-end with REST API, an Apache connecting the client to the server from security manners with PHP code and client side call coming from a jQuery call.
If I put the URL in the browser is the the full output of the JSON, but when I call it from the script it only receive an array of 42 items.
I Check JSON is formatted well when return, and couldnt find anything that limit the respons size.
What can be the problem?
Thank you
This question already has answers here:
How do I close a connection early?
(20 answers)
Continue PHP execution after sending HTTP response
(13 answers)
Closed 9 years ago.
So I have a PHP script that handles post requests. It works quite good, but (always a but) the data I am receiving is encoded. I need to run an external program to decode it. That also works.
My problem is, that my client connects to the server, does a POST and waits for the OK reply from the server, to verify that the post request has been handled correctly. Now when I add the part of the Decoder into my POST request handling script, it takes some time for my client to receive the OK from the server. It is only sending it back when the entire script has been finished, and not only the uploading of the files part, understandably.
What I was wondering now, can I do this in another way? When the uploading of the files is done, the client should now that the upload is finished. He should not have to worry about the decoding part. I also need it to decode immediately after the POST request has been handled, so I'm a bit puzzled at how I need to do this...
Don't really think I need to give out my code but if the need should arise i'll be glad to oblige.
This question already has answers here:
Why doesn't exec("top"); work on Linux?
(5 answers)
Closed 9 years ago.
Is it possible to display the commands like 'top' on webpage using php?
<?php
echo shell_exec('top');
?>
maybe this:
<?php
$output = null;
exec('top -n 1', $output);
var_dump($output);
?>
If I understand your question correctly, you're trying to get an interactive program showing on the client with live updates. This is not possible as you've demonstrated.
It seems you may not understand what's going on with PHP. PHP runs on the server before the page is downloaded by the client. The client then gets a 'snapshot' of the page as the server rendered it. Once the page is loaded on the user's machine, the server cannot touch the page.
To get interactive content, you have a few options (from least desirable and easiest to most desirable and most involved):
refresh the page
make periodic requests for updates (AJAX)
have the server push down changes (COMET, WebSockets)
Another problem is that interactive commands like top use a bunch of terminal-specific (refresh the terminal, rewrite bits of text, etc.) that will mess up the output in the browser. You'll need to do something like #David said and get a snapshot of the output and get that to the user periodically (choose one from above).
There are lots of libraries and tutorials for PHP available for whichever route you choose.
To preface, I know this isn't a great question and it will be hard to explain.
I have a PHP script that takes 5-10 minutes to run. I don't want the user to have to wait for it. If I "trigger" the script using jquery ajax, and then the user navigates away from that page or closes the browser (and doesn't wait for the response (if any) which will come much later), will the script still execute fully (assuming there are no errors etc)
Thanks!
Once the server receives the AJAX Request along with the data, it would process it as usual, even if you close the page or the window. If you close the browser window before the server receives the AJAX Request, the processing it not going to happen.
Furthermore, if the AJAX Request is returning any kind of data or displaying messages, it is advised that you leave the window open, so that there is some "Listening" page to the Server's Request Response.
In your PHP script you could call the ignore_user_abort(true) function which would cause the script to run regardless of the user closing the page or not.
You could use the command line if you have access to one:
php /loc/to/file.php
This does not have a timeout and might be faster than port:80 (a browser calling an phpfile)
Or call the main phpfile in another file via a php's exec():
<?php
exec("php /loc/to/file.php > /loc/to/result.txt");
?>
You might want to use shell_exec()
The dir of results.txt and the file itself need to be writable.
The 'greater than' sign writes the output of the phpfile to result.txt. If the php would echo 123, that would be the contents of result.txt
5 to 10 minutes is a very long time, you might want to check your code for improvements. If you are using a database, add indice (an index) on columns u use allot, that ccan save huge amounts of tim
if i have something like this in php
$foo=0;
while($foo<20){
echo "hello";
usleep(1000000);
$foo=$foo+1;
}
and i make an ajax request to that php file, can i do anything with the data while the request is in progress?
i mean, the script echos hello every second and i saw that the request only shows what data it has when the whole loop is finished, so isnt there a way i can access each hello when its echoed out?
Look for firebug extension of Firefox.
There are a few reasons why you can'y see it.
The content coming from the AJAX request is processed by the server like any other http/php request.
What is happening is the data is being cached by the php buffer, then when its done its flushing it to the output. Which apache then delivers to you.
There is so little data that there is no need to flush this buffer before the processes is done. So you are only seeing the final result.
If you had some much data outputted that it cause the output to be flushed before hand then you may get it.
The other problem is going to be you ajax request handler. I'm pretty sure the onComplete (or similar) method that you (and everyone else) is using will only be called when the output from the server request is finishing and your browser has the full data.
It may be possible to use a different event or perhaps write the ajax code your self (with out using stuff like jQuery) but i'm not even sure if that would solve your problem; as this might also be something to do the with x http request implementation.
May i ask what your are trying to do this for? there may be an easier solution for your actually problem *i'm assuming this isn't the code your actually are using on your site).
Dan
If you execute the flush(); command in PHP you will send content. If you're compressing at the server level you may need to pad output to fill up a packet to make it send.
flush();
Here's an example: http://example.preinheimer.com/flush.php
The correct answer is you CAN see the content while it's being returned.
The other answers were partially correct in mentioning that the PHP output buffer will keep the output "bottled up"... but the output buffer can be disabled.
Once you disable the output buffer you need to show the JQuery response before the request completes - you do this by updating the browser periodically while the connection to the server is still active. This concept is called "Comet" or "Long Polling".
See these questions:
Comet and jQuery
How do I implement basic "Long Polling"?
Comet In PHP