Timon de Groot
Timon de Groot

Reputation: 8153

PHP Socket identification through multiple requests

I'm making a PHP api which creates a client socket connection with my Java SocketServer. This is going to be used for a chat application for learning purpose.

So I'm setting up the whole api at the moment and I'm kinda wondering how I should identify my socket. To store the socket I'm planning to store it in $_SESSION. I have read that the socket id is not a good way to identify it.

What's the best way to store a socket in a session and can I have some code examples?

Thanks in advance!

Upvotes: 0

Views: 332

Answers (2)

Santa's helper
Santa's helper

Reputation: 996

you can try this way: 1) make a stand alone php which is creates a socket client and then starts an infitive loop: 2) in every loop it checks the chat_table with a token_id indicates that the chat session 3) If there is a new message from users your php post it them via socket connection 4) usleep(200000); //

So if its acceptable your main php will initiate these stand alone php via exec() like:

exec('php -f client.php '.escapeshellarg($sessionId).' > /dev/null &');

with these argumants "&" this process will be handled in background your main php will NOT wait that row. At your client.php you can the $sessionId with :

$sessionId = $_SERVER['argv'][1];

After an pre-defined expire time your infitive looped client.php can stop itself. there is no activity in the table for that session it breaks.

Upvotes: 1

Narf
Narf

Reputation: 14752

You can't "store" a socket in between requests, it just doesn't work that way.

What you can do, is to send the session ID (or some other similar token) via that socket, so that the receiving end (your Java app) can identify different clients via session IDs.

Upvotes: 0

Related Questions