XMPP 和 PHP 流式传输
我正在创建一个使用 php 连接到 XMPP 服务器并发送消息的网站 但是,我在发送/接收消息时遇到问题.. 问题是每次发出请求(发送消息)时我是否必须重新连接并发送用户名/密码? 如何避免重新连接?
这就是我的连接方式:
$this->_socket = fsockopen("sever.tld", 5222, $errno, $errstr, 30);
我使用 fwrite 发送消息,如下所示:
fwrite($Socket, $data);
我使用 fread 读取消息,如下所示:
$response = @fread($this->_socket, 1024);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从长远来看,您使用的方法不会起作用。
由于 PHP 实例在完成将页面发送回浏览器客户端后实际上不再存在,因此您与 XMPP 服务器的连接将关闭。这意味着所有状态(TLS 会话、身份验证等)都会丢失。
所以是的,如果您这样做,则必须在每次页面加载时重新连接并重新进行身份验证。
请不要这样做。您可以使用 XMPP 服务器端适配器,例如为此设计的 XMPP over BOSH目的,或者通过 PHP 实例共享的某些守护进程或长期进程的 HTTP 服务器端持久连接。
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.
使用以下循环来防止连接关闭:
并将所有逻辑放入其中。当连接仍然处于活动状态时,它将无限循环地运行(直到您终止它为止)。
Use the following loop to prevent the connection from closing:
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).