Reputation:
im creating a website which uses php to connect to an XMPP server , and send a message However, im having a problem with sending/receiving messages .. Question is do i have to reconnect and send username/password everytime i make a request ( send a message ) ? how to avoid reconnecting ?
This is how i connect :
$this->_socket = fsockopen("sever.tld", 5222, $errno, $errstr, 30);
i send messages using fwrite Like this :
fwrite($Socket, $data);
i read messages using fread Like this :
$response = @fread($this->_socket, 1024);
Upvotes: 0
Views: 550
Reputation: 174947
Use the following loop to prevent the connection from closing:
while (!feof($this->_socket)) {
}
And place all of your logic inside. It will run endlessly in a loop while the connection is still active (which it would be until you kill it).
Upvotes: 1
Reputation: 98459
The approach you're using is not going to work in the long run.
Because a PHP instance effectively ceases to exist when it's finished sending a page back to the browser client, the connection you make to the XMPP server is closed. This means that all state (TLS session, authentication, &c) is lost.
So yes, if you do it this way, you'd have to reconnect and re-authenticate on every page load.
Please don't do it this way. You may use an XMPP-server-side adapter such as XMPP over BOSH, designed for this purpose, or an HTTP-server-side persistent connection via some daemon or longer-lived process which your PHP instances share.
Upvotes: 3