austgl / phpws

Automatically exported from code.google.com/p/phpws
0 stars 0 forks source link

Getting IP('s) of connected clients #16

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago

First, great job on this piece of work.
I adjusted some of the code so it does exactly what I want :)

However, I want to be able to get the IP's of the connected users.
I saw the getIp() function in websocket.users.php but even an echo in the 
__construct() never seems to be shown.

I tried to call the socket_getpeername($socket, $address) here-and-there on 
different classes, but it alway's tells me "supplied resource is not a valid 
Socket resource".

I'm not an advanced PHP programmer, so I'm a bit stuck.
Any way to easily get a $user->getIp() for example?

Original issue reported on code.google.com by ad...@indiciumsolutions.nl on 23 Jan 2012 at 11:23

GoogleCodeExporter commented 9 years ago
First, thanks for the compliment :)

As for the IP, PHPWS doesnt use the socket_ API but the streaming API. 
Unfortunately the PHP manual seems down for me now so I can't look up the 
specific function. Will have another look when its back online :)

Original comment by ch...@devristo.com on 23 Jan 2012 at 5:03

GoogleCodeExporter commented 9 years ago
As for the $connection->getIp(), this should be relatively easy to add. Will 
add this as well.

Original comment by ch...@devristo.com on 23 Jan 2012 at 5:09

GoogleCodeExporter commented 9 years ago
Awesome!
Yeah, I saw the streaming part, but I figured socket_getpeername() would still 
work.
Any reason why you changed from socket to streaming? (I used your old code a 
while ago)
Easier to grab changes instead of making a while loop that checks changes 
perhaps?

I had to adjust that part in the run() function to 
stream_select($changed,$write,$except,0,300000), because I use System_Daemon( 
http://kevin.vanzonneveld.net/techblog/article/create_daemons_in_php/ )
so i can just restart, log, etc that Daemon quite easily.

Some more adjustments that might be interesting (for you or others):
Changed some of the UriHandlers, that the "channel" is automatically made when 
it doesn't exist.
And made it so any message from the server (as client) it would broadcast to 
that channel to every user in that channel.
And, because it's a Daemon i could set it at 4am to destroy the class and 
create it again so any garbage (created channels, connections) is destroyed. I 
would rather do it nicer, like unset some when there are no users there, but i 
can't figure out a way to do that.
Speaking about garbage-collection: your script does seem to use a lot of memory 
at some point when it's running quite a long time. 
Thats because PHP doesn't clean up nicely. 
I suggest the following:

            clearstatcache();
            // Garbage Collection (PHP >= 5.3)
            if (function_exists('gc_collect_cycles')) {
                gc_collect_cycles();
            }
try to set up a demo.php, connect/disconnect a lot of users and check 
memory_get_usage().
And then with these functions in run() for example. You'd see that the memory 
never gets higher.

Also, i'm not sure if anyone had experienced this but idle connections of 
+-20min are disconnected without the browser + server knowing.
I know this is a websocket issue, because most browsers don't have the 
Ping/Pong that has been implemented quite recently (i think)
As a test, i made 2 channels, lets say /echo/ and /test/
On both of them i connect two users. If there's anything going on in /echo/: 
none of these users get disconnected.
As for /test/, 2 users are idle, nothing is send or received for 15 or 20 min. 
Then the browser fails to notice ws.onclose but so does the server.
Once you do something for that channel /test/ you see "123 has disconnected, 
124 has disconnected".
But the browser still is clueless.
I thought that was really annoying, so i made a small array with the id's of 
the users + time() of connect.
Once that time + 5min hits, it would send ::PingFrame that you implemented 
already ;) and reset time().
(But Flash would send a Pong back, so i had to adjust that as well, since 
onMessage() was called but $msg is empty)

Sorry for the long post but this read might be interesting (or not!)

Original comment by ad...@indiciumsolutions.nl on 23 Jan 2012 at 6:18

GoogleCodeExporter commented 9 years ago
Primarily I changed to streaming because it allows to support SSL easily. 

Your adjustments sound very useful in chat like application :) However PHPWS is 
bit more generic. For a sample  it would be great to add, will put that on my 
todo list ;)

I will also add that garbage collection trigger. However I dont see your issue, 
guess that it heavily depends on platform and PHP version. The instance we use 
on u2start.com is now up for 14 days and memory usage is still low. But I know 
that on my local pc there were some performance related issues.

The daemon stuff is very interesting, also a bit scary since I am primarily a 
Windows user. Will have a look in a virtual machine and play around with the 
System_Daemon.

And by the way, I think 
http://php.net/manual/en/function.stream-socket-get-name.php will give you the 
IP address of the client!

Original comment by ch...@devristo.com on 26 Jan 2012 at 10:11

GoogleCodeExporter commented 9 years ago
And also, thanks a lot for all your suggestions! Very cool to see a hobby 
project being used by others :)

Original comment by ch...@devristo.com on 26 Jan 2012 at 10:11

GoogleCodeExporter commented 9 years ago
Great! That was it!
I didn't know that function.
For those who want it:

Add the following in server.protocol.php in the abstract class 
WebSocketConnection:

        public function getIp() {
                return stream_socket_get_name($this->_socket->getResource(),TRUE);
        }

Now you can use $user->getIp() in your  onConnect() 

Thanks!!

Original comment by ad...@indiciumsolutions.nl on 27 Jan 2012 at 10:16

GoogleCodeExporter commented 9 years ago
I have pushed a change containing the getIp() and the garbage collection. Didnt 
have time to implement the keepalive ping. Will do that later :)

Thank you!

Original comment by ch...@devristo.com on 28 Jan 2012 at 10:18

GoogleCodeExporter commented 9 years ago

Original comment by ch...@devristo.com on 15 Dec 2012 at 11:04