Hi, I have this situation.
I'm writing a PHP site with several pages. Almost all of them need to connect to a server to get some data from it and show it to the client. This connections are made with sockets. Sockets are encapsulated in a class Socket, so, when a page loads, a new Socket object is created. It connects to the server, gets the data and then, when the script finish executing, I understand PHP cleans the object and close the connection.
The problem is that I don't think it's a good idea to be opening and closing connections every time a page is loaded. That is why I was wondering how to keep just one socket connection opened and use it from all pages. I know this can't be done with a session variable because resources can't be stored. Any other options? A global varialbe maybe?
Thanks in advance,
Steven
Keep socket connection alive between scripts
Moderator: General Moderators
-
estero2002
- Forum Newbie
- Posts: 4
- Joined: Mon Oct 01, 2007 10:15 pm
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
Everything is essentially a file in UNIX. Maybe you could open the socket on the command line and name it (or symlink to it) with the session ID, then you could test to see if the file exists and create it if it's missing.
You'd need to write your own garbage collector to clean up old sockets, and I have no idea what the memory footprint would be.
Just how many socket operations do you have per session? It might be best to simply incur the overhead of creating the socket every time. Would certainly be less prone to error.
You'd need to write your own garbage collector to clean up old sockets, and I have no idea what the memory footprint would be.
Just how many socket operations do you have per session? It might be best to simply incur the overhead of creating the socket every time. Would certainly be less prone to error.