Browse Source

Add a little timer and an event to put the user Away when the last tab is closed

pull/183/head
Timothée Jaussoin 10 years ago
parent
commit
4705f874db
  1. 13
      app/widgets/Presence/Presence.php
  2. 3
      composer.json
  3. 54
      composer.lock
  4. 16
      linker.php
  5. 10
      src/Movim/Daemon/Core.php
  6. 44
      src/Movim/Daemon/Session.php

13
app/widgets/Presence/Presence.php

@ -35,6 +35,19 @@ class Presence extends WidgetBase
$this->addcss('presence.css');
$this->addjs('presence.js');
$this->registerEvent('mypresence', 'onMyPresence');
$this->registerEvent('session_up', 'onSessionUp');
$this->registerEvent('session_down', 'onSessionDown');
}
function onSessionUp()
{
$this->ajaxSet();
}
function onSessionDown()
{
$p = new Away;
$p->request();
}
function onMyPresence($packet)

3
composer.json

@ -25,6 +25,7 @@
"forxer/Gravatar": "~1.2",
"respect/validation": "1.0.*",
"ezyang/htmlpurifier": "^4.7",
"ramsey/uuid": "^3.2"
"ramsey/uuid": "^3.2",
"react/promise-timer": "^1.1"
}
}

54
composer.lock

@ -4,8 +4,8 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "d6dafb959ed67d8cdfd33a6bcaf6af22",
"content-hash": "3c6685a763a45be2403852a3c067cbac",
"hash": "dc85c5d3228c63268ce4e4ebcab697e0",
"content-hash": "ee7a70320957c4309628805376724c1f",
"packages": [
{
"name": "cboden/ratchet",
@ -1188,6 +1188,56 @@
"description": "A lightweight implementation of CommonJS Promises/A for PHP",
"time": "2016-02-26 19:09:02"
},
{
"name": "react/promise-timer",
"version": "v1.1.0",
"source": {
"type": "git",
"url": "https://github.com/reactphp/promise-timer.git",
"reference": "6a46050d801688e08d1a550b97b1f3735505c62a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/reactphp/promise-timer/zipball/6a46050d801688e08d1a550b97b1f3735505c62a",
"reference": "6a46050d801688e08d1a550b97b1f3735505c62a",
"shasum": ""
},
"require": {
"php": ">=5.3",
"react/event-loop": "~0.4.0|~0.3.0",
"react/promise": "~2.1|~1.2"
},
"type": "library",
"autoload": {
"psr-4": {
"React\\Promise\\Timer\\": "src/"
},
"files": [
"src/functions.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Christian Lück",
"email": "christian@lueck.tv"
}
],
"description": "Trivial timeout implementation for Promises",
"homepage": "https://github.com/react/promise-timer",
"keywords": [
"async",
"event-loop",
"promise",
"reactphp",
"timeout",
"timer"
],
"time": "2016-02-29 19:03:45"
},
{
"name": "react/socket",
"version": "v0.4.3",

16
linker.php

@ -37,11 +37,11 @@ function handleSSLErrors($errno, $errstr) {
}
// Temporary linker killer
$loop->addPeriodicTimer(5, function() use(&$conn, &$timestamp) {
/*$loop->addPeriodicTimer(5, function() use(&$conn, &$timestamp) {
if($timestamp < time() - 3600*2) {
$conn->close();
}
});
});*/
$stdin_behaviour = function ($data) use (&$conn, $loop, &$buffer, &$connector, &$xmpp_behaviour, &$parser, &$timestamp) {
if(substr($data, -1) == "") {
@ -56,6 +56,12 @@ $stdin_behaviour = function ($data) use (&$conn, $loop, &$buffer, &$connector, &
if(isset($msg)) {
if($msg->func == 'message' && $msg->body != '') {
$msg = $msg->body;
} elseif($msg->func == 'down') {
$evt = new Event;
$evt->runEvent('session_down');
} elseif($msg->func == 'up') {
$evt = new Event;
$evt->runEvent('session_up');
} elseif($msg->func == 'unregister') {
\Moxl\Stanza\Stream::end();
} elseif($msg->func == 'register') {
@ -113,11 +119,13 @@ $xmpp_behaviour = function (React\Stream\Stream $stream) use (&$conn, $loop, &$s
$stdin->on('data', $stdin_behaviour);
// We define a huge buffer to prevent issues with SSL streams, see https://bugs.php.net/bug.php?id=65137
$conn->bufferSize = 1024*32;
//$conn->bufferSize = 1024*32;
$conn->on('data', function($message) use (&$conn, $loop, $parser, &$timestamp) {
if(!empty($message)) {
$restart = false;
fwrite(STDERR, colorize($message, 'yellow')." : ".colorize('received', 'green')."\n");
if($message == '</stream:stream>') {
$conn->close();
$loop->stop();
@ -134,7 +142,6 @@ $xmpp_behaviour = function (React\Stream\Stream $stream) use (&$conn, $loop, &$s
set_error_handler('handleSSLErrors');
$out = stream_socket_enable_crypto($conn->stream, 1, STREAM_CRYPTO_METHOD_TLS_CLIENT);
restore_error_handler();
if($out !== true) {
$loop->stop();
return;
@ -145,7 +152,6 @@ $xmpp_behaviour = function (React\Stream\Stream $stream) use (&$conn, $loop, &$s
$restart = true;
}
#fwrite(STDERR, colorize($message, 'yellow')." : ".colorize('received', 'green')."\n");
#fwrite(STDERR, colorize(getenv('sid'), 'yellow')." widgets : ".\sizeToCleanSize(memory_get_usage())."\n");
\Moxl\API::clear();

10
src/Movim/Daemon/Core.php

@ -63,7 +63,7 @@ class Core implements MessageComponentInterface {
";
$path = $explode['host'].$explode['path'];
if($explode['scheme'] == 'https') {
$ws = 'wss://'.$path.'ws/';
$secured = 'true';
@ -78,7 +78,7 @@ class Core implements MessageComponentInterface {
return $ws;
}
public function onOpen(ConnectionInterface $conn)
{
$sid = $this->getSid($conn);
@ -87,7 +87,7 @@ class Core implements MessageComponentInterface {
$this->sessions[$sid] = new Session($this->loop, $sid, $this->baseuri);
}
$this->sessions[$sid]->attach($conn);
$this->sessions[$sid]->attach($this->loop, $conn);
}
}
@ -103,7 +103,7 @@ class Core implements MessageComponentInterface {
{
$sid = $this->getSid($conn);
if($sid != null && isset($this->sessions[$sid])) {
$this->sessions[$sid]->detach($conn);
$this->sessions[$sid]->detach($this->loop, $conn);
if($this->sessions[$sid]->process == null) {
unset($this->sessions[$sid]);
@ -134,7 +134,7 @@ class Core implements MessageComponentInterface {
$sd = new \Modl\SessionxDAO();
$sd->deleteEmpty();
}
public function onError(ConnectionInterface $conn, \Exception $e)
{
echo "An error has occurred: {$e->getMessage()}\n";

44
src/Movim/Daemon/Session.php

@ -2,6 +2,7 @@
namespace Movim\Daemon;
use Ratchet\ConnectionInterface;
use React\EventLoop\Timer\Timer;
class Session {
protected $clients;
@ -13,27 +14,43 @@ class Session {
public $registered;
protected $buffer;
private $state;
public function __construct($loop, $sid, $baseuri)
{
$this->sid = $sid;
$this->baseuri = $baseuri;
$this->clients = new \SplObjectStorage;
$this->register($loop, $this);
$this->timestamp = time();
}
public function attach(ConnectionInterface $conn)
public function attach($loop, ConnectionInterface $conn)
{
$this->clients->attach($conn);
if($this->countClients() > 0) {
$this->stateOut('up');
}
echo colorize($this->sid, 'yellow'). " : ".colorize($conn->resourceId." connected\n", 'green');
}
public function detach(ConnectionInterface $conn)
public function detach($loop, ConnectionInterface $conn)
{
$this->clients->detach($conn);
if($this->countClients() == 0) {
$loop->addPeriodicTimer(5, function($timer) {
if($this->countClients() == 0) {
$this->stateOut('down');
}
$timer->cancel();
});
}
echo colorize($this->sid, 'yellow'). " : ".colorize($conn->resourceId." deconnected\n", 'red');
}
@ -41,7 +58,7 @@ class Session {
{
return $this->clients->count();
}
private function register($loop, $me)
{
$buffer = '';
@ -58,7 +75,7 @@ class Session {
$this->process->start($loop);
// Buffering the incoming data and fire it once its complete
// Buffering the incoming data and fire it once its complete
$this->process->stdout->on('data', function($output) use ($me, &$buffer) {
if(substr($output, -1) == "") {
$out = $buffer . substr($output, 0, -1);
@ -105,7 +122,20 @@ class Session {
$client->close();
}
}
public function stateOut($state)
{
if($this->state == $state) return;
if(isset($this->process)) {
$this->state = $state;
$msg = new \stdClass;
$msg->func = $this->state;
$msg = json_encode($msg);
$this->process->stdin->write($msg."");
}
}
public function messageIn(ConnectionInterface $from, $msg)
{
$this->timestamp = time();

Loading…
Cancel
Save