脚本完成执行后如何使套接字保持活动状态

I have written a php script, which connects to facebook chat via tcp socket. My questions are:

  1. How to keep the socket alive even when the php script is ended? I want other php file to communicate with facebook XMPP server via this socket? I have tried to save the socket as a session variable, but it did not work.

  2. I tried to use pfsockopen instead of socket_create, according to some forum discussion. But I always receive failure when I call pfsockopen function.

Any one has idea how to solve this?

Thanks!

You need a standalone daemon for this.

You cannot share the socket between different PHP scripts. What @seriyPS suggests makes sense - create a PHP script, that:

  • never ends
  • opens socket to FB chat
  • Listen on another socket

Then your PHP scripts can open socket to that 'daemon' PHP script, send a message, the daemon sends it to FB and returns the FB response.