我正在设计棘轮websockets中的聊天室,以尽可能快地响应。 它知道用户什么时候离开页面,以及类似的东西。 但是,如果一个用户/客户端失去了与服务器的连接,问题是客户端不能让服务器知道它已经断开连接,因为它已经断开连接并且不能向服务器发送消息。那么,如何跟踪聊天客户端何时失去互联网连接并且不再上网?棘轮Websockets - 检测断开连接的客户端
两个可能的解决方案,我能想到的:
服务器轮询客户端每15分钟到半小时检查,看看谁在线上。没有回应的客户会断开连接。这可能不会中断一切在PHP中发生?如果是的话如何?我在哪里放置代码?我从
LoopInterface
看到addPeriodicTimer()
的一些信息,但我不确定这是否能完成这项工作,或者函数适合我的代码。 它也叫它sleep()
函数,因为那样做不好。我还是想其他任务在后台发生的事情,而这个功能是一个计时器(如果可能的话在PHP)onClose()
方法在PHP 这可检测出用户在任何环境真的断开连接?如果是这样,当这个事件发生时,我怎么知道哪个用户被断开了?它只传递一个ConnectionInterface并且没有消息。
对不起,即时通讯仍然是新的棘轮库,仍然试图找出如何实现这一任务。
我的server.php代码:为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
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();
?>
代码
<?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( )方法在互联网连接终止时不会触发。
有没有办法可以解决第一个问题?
听起来不错,但我目前不想为API支付费用,也不希望每天仅限于100个连接。 – emjay
aww cmon。这里没有一个人使用Ratchet API来告诉我如何定期向所有客户端发送消息? – emjay