XMPP和PHP流


                    

im创建一个使用php连接到XMPP服务器并发送消息的网站
但是,我在发送/接收消息时有问题..
问题是我每次发出请求(发送消息)时都必须重新连接并发送用户名/密码吗?
如何避免重新连接?</ p>

这就是我的联系方式:</ p>

  $ this-> _ socket = fsockopen(“ sever.tld”,5222,$ errno,$ errstr,30);
</ code> </ pre>

我使用fwrite发送消息,像这样:</ p>

  fwrite($ Socket,$ data);
</ code> </ pre>

我使用fread读取消息,像这样:</ p>

  $ response = @fread($ this-> _ socket,1024);
</ code> </ pre>
     </ div>

展开原文

原文

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);

2个回答


您使用的方法从长远来看是行不通的。</ p>

由于PHP实例在将页面发送回浏览器客户端后实际上不再存在,因此您与XMPP服务器的连接已关闭。 这意味着所有状态(TLS会话,身份验证和&c)都将丢失。</ p>

是的,如果您采用这种方式,则必须在每次页面加载时重新连接并重新进行身份验证。</ p>

请不要这样操作。 您可以使用为此设计的XMPP服务器端适配器,例如 XMPP over BOSH 。 目的,或通过PHP实例共享的某些守护程序或寿命更长的进程进行HTTP服务器端持久连接。</ p>
     </ div>

展开原文

原文

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.


使用以下循环来防止连接关闭:</ p>

  while(!feof($ this-> _ socket)){

}
</ code> </ pre>

并将所有逻辑放入其中。 当连接仍处于活动状态时,它将无限循环运行(直到您终止该连接)。</ p>
     </ div>

展开原文

原文

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).

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问