我是PHP Websockets的初學者,我試圖創建與數據庫存儲的實時聊天。我做得很好,但現在我站在一個問題上。有問題,當用戶1發送消息給用戶2和用戶2首先到達站點(首先在localhoste上重新加載)時,它不會是「實時」的。PHP與Ratchet Websockets實時聊天
讓我進一步解釋一下。
這是我的server.php。這是practicly一樣棘輪教程:
$loop = React\EventLoop\Factory::create();
$pusher = new \Pusher();
$context = new React\ZMQ\Context($loop);
$pull = $context->getSocket(ZMQ::SOCKET_PULL);
$pull->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
$pull->on('message', array($pusher, 'onBlogEntry'));
$webSock = new React\Socket\Server($loop);
$webSock->listen(8080, '0.0.0.0'); // Binding to 0.0.0.0 means remotes can connect
$webServer = new Ratchet\Server\IoServer(
new Ratchet\Http\HttpServer(
new Ratchet\WebSocket\WsServer(
new Ratchet\Wamp\WampServer($pusher))), $webSock);
$loop->run();
在pusher.php這些方法是(我等非重要的東西被省略)最重要的:
protected $subscribedTopics = array();
protected $myID = array();
public function onSubscribe(ConnectionInterface $conn, $data) {
$this->subscribedTopics[json_decode($data)->teamID] = $data;
$this->myID[json_decode($data)->userID] = $data;
}
public function onBlogEntry($entry) {
$entryData = json_decode($entry, true);
if ((!array_key_exists($entryData['team_id'], $this->subscribedTopics)) ||
(!array_key_exists($entryData['to_user_id'], $this->myID))
) {
return;
}
$teamID = $this->subscribedTopics[$entryData['team_id']];
$teamID->broadcast($entryData);
}
在我主持人類我有簡單的形式。當用戶提交這種形式,這個代碼如下:
$this->chatPartner = $values['to_user_id']; //this I get from the form
$this->redrawControl('msg'); //here I redraw my layout
$this->messages_model->addMessage($values); //here I send data to database
$context = new \ZMQContext();
$socket = $context->getSocket(\ZMQ::SOCKET_PUSH, 'my pusher');
$socket->connect("tcp://localhost:5555");
$socket->send(json_encode($values));
然後,在視圖我有這樣的JavaScript代碼:
var myJSON = '{'
+ '"teamID" : {$teamId},' //this I get from the presenter
+ '"userID" : {$userId}' //this I get from the presenter
+ '}';
var conn = new ab.Session('ws://localhost:8080',
function() {
conn.subscribe(myJSON, function(topic, data) {
if (data.from_user_id == mypartnerIdA) {
//here I edit the DOM
}
});
},
function() {
console.warn('WebSocket connection closed');
},
{'skipSubprotocolCheck': true}
);
所以,回到我的問題。我模擬2個用戶。 User1重新加載此頁面,其中JavaScript連接優先。用戶2在他之後重新加載此頁面。當用戶1向用戶2發送消息時,消息立即(實時)出現。但是,當user2向user1發送消息時,此消息不會立即出現 - 只有在下次重新加載頁面後纔會顯示。
而我的問題是 - 如何解決這個問題?如何使用戶2的信息也是實時的?我怎樣才能解決這個我的代碼?
男人,非常感謝你,我不相信,有人幫我在這裏:)) – mrow
沒問題,如果你需要更多的信息,請讓我知道 – mitchken