我在 Ratchet 网络套接字中设计一个聊天室,以尽可能地响应。 它知道用户何时离开页面,以及类似的一切。 但是,如果用户/客户端与服务器失去连接,问题是客户端无法让服务器知道它已断开连接,因为它已经断开连接并且无法向服务器发送消息。那么,我该如何跟踪聊天客户端何时失去互联网连接并且不再在线?
我能想到的两种可能的解决方案:
服务器每 15 分钟到半小时轮询一次客户端,以查看谁在线。不响应的客户端将断开连接。这是否可能在不中断 php 中发生的所有其他事情的情况下完成?如果是这样怎么办?我应该把代码放在哪里?我从
LoopInterface
中看到了一些关于addPeriodicTimer()
的信息,但我不确定这是否能完成这项工作,或者该函数适合我的代码。 它还会调用sleep()
函数,因为那样不好。当这个函数在定时器上时,我仍然希望在后台发生其他任务(如果可能的话在 php 中)
php 中的 onClose()
方法 这能否检测到用户在任何情况下何时真正断开连接?如果是这样,当此事件触发时,我如何找出哪个用户已断开连接?它只传递一个 ConnectionInterface 而没有消息。
抱歉,我对 ratchet 库还是个新手,仍在努力研究如何完成这项任务。
我的 server.php 代码:
<?php
require($_SERVER['DOCUMENT_ROOT'].'/var/www/html/vendor/autoload.php');
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
$server = IoServer::factory(new HttpServer(new WsServer(new Chat)), 8080);
$server->run();
?>
app.js 代码
// JavaScript Document
var chat = document.getElementById("chatwindow");
var msg = document.getElementById("messagebox");
var refInterval = 0;
var timeup = false;
var awaytimer;
var socket = new WebSocket("ws://52.39.48.172:8080");
var openn = false;
function addMessage(msg){
"use strict";
chat.innerHTML += "<p>" + msg + "</p>";
}
msg.addEventListener('keypress', function(evt){
"use strict";
if(evt.charCode != 13)
return;
evt.preventDefault();
if(msg.value == "" || !openn)
return;
socket.send(JSON.stringify({
msg: msg.value,
uname: nme,
uid: id,
tag: "[msgsend]"
}));
msg.value = "";
});
socket.onopen = function(evt) {
openn = true;
socket.send(JSON.stringify({
uname: nme,
uid: id,
tag: "[connected]"
}));
};
socket.onmessage = function(evt) {
var data = JSON.parse(evt.data);
if(data.tag == "[connected]")
{
addMessage(data.uname + " has connected...");
}
else if(data.tag == "[bye]")
{
addMessage(data.uname + " has left the room...");
if(data.uname == nme)
socket.close();
}
else if(data.tag == "[msgsend]")
{
addMessage(data.uname + ": " + data.msg);
}
};
window.onfocus = refPage;
function refPage()
{
if(timeup == true)
{
if(refInterval == 1)
{
refInterval = 0;
location.reload();
}
}
else
{
clearTimeout(awaytimer);
}
timeup = false;
}
window.onblur = timelyExit;
function timelyExit()
{
refInterval = 1;
// change this to trigger some kind of inactivity timer
awaytimer = setTimeout(function(){socket.send(JSON.stringify({
uname: nme,
uid: id,
tag: "[bye]"
})); timeup=true; }, 900000);
}
window.onoffline = window.onunload = window.onbeforeunload = confirmExit;
function confirmExit()
{
socket.send(JSON.stringify({
uname: nme,
uid: id,
tag: "[bye]"
}));
socket.close();
}
socket.onclose = function() {
openn = false;
//cant send server this message because already closed.
/*
socket.send(JSON.stringify({
uname: nme,
uid: id,
tag: "[bye]"
}));
*/
socket.close();
};
chat.php 代码
<?php
error_reporting(E_ALL ^ E_NOTICE);
session_id($_GET['sessid']);
if(!session_id)
session_start();
$userid = $_SESSION["userid"];
$username = $_SESSION["username"];
$isadmin = $_SESSION["isadmin"];
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface
{
protected $clients;
public function __construct()
{
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn)
{
$this->clients->attach($conn);
}
public function onClose(ConnectionInterface $conn)
{
$this->clients->detach($conn);
}
public function onMessage(ConnectionInterface $conn, $msg)
{
$msgjson = json_decode($msg);
$tag = $msgjson->tag;
if($tag == "[msgsend]")
{
foreach($this->clients as $client)
{
$client->send($msg);
}
}
else if($tag == "[bye]")
{
foreach($this->clients as $client)
{
$client->send($msg);
}
onClose($conn);
}
else if($tag == "[connected]")
{
//store client information
//send out messages
foreach($this->clients as $client)
{
$client->send($msg);
}
}
}
public function onError(ConnectionInterface $conn, Exception $e)
{
echo "Error: " . $e->getMessage();
$conn -> close();
}
}
?>
编辑:刚刚测试并确认 onClose() 方法在互联网连接终止时不会触发。
有没有办法我仍然可以使用第一个解决方案?
最佳答案
检测断开连接的客户端的最佳解决方案是基于事件并且根本不轮询客户端。这种方法将是您的第二个解决方案,并且还可以很好地围绕 WebSocket 消息传递的异步特性对其进行建模。
然而,正如您所说,在某些情况下,某些顽皮的客户端可能不会通知套接字服务器他们的断开连接并使其“挂起”,可以这么说。在这种情况下,我建议不要尝试在套接字服务器本身内实现轮询触发器,而是通过触发的单独的服务器端客户端启动轮询通过 cron 或其他任务调度程序并指示套接字服务器发起请求以轮询所有连接的客户端。
有关构建服务器端客户端的更多信息,请参阅 this question of mine为此我也找到了一些解决方案。
为了确定谁发送了断开连接消息,我建议不要只在Ratchet\MessageComponentInterface
实现中使用SplObjectStorage
你有一个简单的数组,而不是在另一个类中包装一个简单的数组,所以像这样:
class MyClientList
{
protected $clients = [];
public function addClient(Connection $conn)
{
$this->clients[$conn->resourceId] = [
'connection' => $conn,
];
return $this;
}
public function removeClient(Connection $conn)
{
if(isset($this->clients[$conn->resourceId])) {
unset($this->clients[$conn->resourceId]);
}
return $this;
}
public function registerClient(ConnectionInterface $conn, array $userData)
{
if(isset($this->clients[$conn->resourceId])) {
$this->clients[$conn->resourceId] = array_merge(
$this->clients[$conn->resourceId],
$userData
);
}
return $this;
}
public function getClientData(ConnectionInterface $conn)
{
return isset($this->clients[$conn->resourceId]) ?
$this->clients[$conn->resourceId] :
null
;
}
}
在用户首次连接后不久,您的客户端应该向服务器发送套接字消息并指示服务器现在使用附加信息注册客户端的身份(在您的情况下,您正试图识别一个 uname
和 uid
属性)。通过对连接 ID 进行索引,您应该能够使用此实现来判断客户端发送初始注册消息后发出的所有消息的身份。
关于javascript - Ratchet Websockets - 检测断开连接的客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38551995/