Browse Source

Huge cleanup and refactor of the MovimWebsocket class, behaviour and disconnection handling

pull/238/head
Timothée Jaussoin 9 years ago
parent
commit
b88d233100
  1. 32
      app/assets/js/movim_websocket.js
  2. 3
      app/controllers/AccountController.php
  3. 2
      app/controllers/LoginController.php
  4. 2
      app/models/sessionx/SessionxDAO.php
  5. 1
      app/views/login.tpl
  6. 11
      app/views/page.tpl
  7. 5
      app/widgets/AccountNext/AccountNext.php
  8. 3
      app/widgets/AccountNext/_accountnext_registered.tpl
  9. 6
      app/widgets/Login/_login_error.tpl
  10. 3
      app/widgets/Login/locales.ini
  11. 3
      app/widgets/Login/login.js
  12. 8
      app/widgets/Login/login.tpl
  13. 1
      locales/locales.ini
  14. 9
      themes/material/css/style.css

32
app/assets/js/movim_websocket.js

@ -25,10 +25,12 @@ var MovimWebsocket = {
connection: null, connection: null,
attached: new Array(), attached: new Array(),
registered: new Array(), registered: new Array(),
unregistered: false,
attempts: 1, attempts: 1,
launchAttached : function() { launchAttached : function() {
// We hide the Websocket error
MovimUtils.hideElement(document.getElementById('error_websocket'));
for(var i = 0; i < MovimWebsocket.attached.length; i++) { for(var i = 0; i < MovimWebsocket.attached.length; i++) {
MovimWebsocket.attached[i](); MovimWebsocket.attached[i]();
} }
@ -80,20 +82,16 @@ var MovimWebsocket = {
if(e.code == 1006) { if(e.code == 1006) {
MovimWebsocket.reconnect(); MovimWebsocket.reconnect();
} else if(e.code == 1000) { } else if(e.code == 1000) {
if(MovimWebsocket.unregistered == false) {
MovimUtils.disconnect();
} else {
MovimWebsocket.unregistered = false;
MovimWebsocket.init();
}
MovimUtils.disconnect();
} }
}; };
this.connection.onerror = function(e) { this.connection.onerror = function(e) {
console.log(e.code);
console.log(e);
console.log("Connection error!"); console.log("Connection error!");
// We hide the Websocket error
MovimUtils.showElement(document.getElementById('error_websocket'));
MovimWebsocket.reconnect(); MovimWebsocket.reconnect();
// We prevent the onclose launch // We prevent the onclose launch
@ -155,13 +153,12 @@ var MovimWebsocket = {
}, },
unregister : function(reload) { unregister : function(reload) {
if(reload == false) this.unregistered = true;
this.connection.unregister(); this.connection.unregister();
}, },
reconnect : function() { reconnect : function() {
var interval = MovimWebsocket.generateInterval(); var interval = MovimWebsocket.generateInterval();
console.log("Try to reconnect in " + interval);
console.log("Try to reconnect");
setTimeout(function () { setTimeout(function () {
// We've tried to reconnect so increment the attempts by 1 // We've tried to reconnect so increment the attempts by 1
MovimWebsocket.attempts++; MovimWebsocket.attempts++;
@ -183,16 +180,7 @@ var MovimWebsocket = {
} }
} }
function remoteUnregister()
{
MovimWebsocket.unregister(false);
}
function remoteUnregisterReload()
{
MovimWebsocket.unregister(true);
}
/*
document.addEventListener("visibilitychange", function () { document.addEventListener("visibilitychange", function () {
if(!document.hidden) { if(!document.hidden) {
if(MovimWebsocket.connection.readyState == 3) { if(MovimWebsocket.connection.readyState == 3) {
@ -200,7 +188,7 @@ document.addEventListener("visibilitychange", function () {
} }
} }
}); });
*/
window.onbeforeunload = function() { window.onbeforeunload = function() {
MovimWebsocket.connection.onclose = function () {}; // disable onclose handler first MovimWebsocket.connection.onclose = function () {}; // disable onclose handler first
MovimWebsocket.connection.close() MovimWebsocket.connection.close()

3
app/controllers/AccountController.php

@ -7,6 +7,9 @@ class AccountController extends Base {
} }
function dispatch() { function dispatch() {
$session = \Sessionx::start();
requestURL('http://localhost:1560/disconnect/', 2, ['sid' => $session->sessionid]);
$this->page->setTitle(__('page.account_creation')); $this->page->setTitle(__('page.account_creation'));
} }
} }

2
app/controllers/LoginController.php

@ -8,7 +8,7 @@ class LoginController extends Base {
function dispatch() { function dispatch() {
$session = \Sessionx::start(); $session = \Sessionx::start();
requestURL('http://localhost:1560/unregister/', 2, ['sid' => $session->sessionid]);
//requestURL('http://localhost:1560/unregister/', 2, ['sid' => $session->sessionid]);
$this->page->setTitle(__('page.login')); $this->page->setTitle(__('page.login'));

2
app/models/sessionx/SessionxDAO.php

@ -3,7 +3,7 @@
namespace modl; namespace modl;
class SessionxDAO extends SQL { class SessionxDAO extends SQL {
function init(Sessionx $s) {\movim_log($s->start);
function init(Sessionx $s) {
$this->_sql = ' $this->_sql = '
update sessionx update sessionx
set username = :username, set username = :username,

1
app/views/login.tpl

@ -1,6 +1,5 @@
<?php $this->widget('Presence'); ?> <?php $this->widget('Presence'); ?>
<main> <main>
<?php //$this->widget('Header');?>
<section> <section>
<div> <div>
<?php $this->widget('Login'); ?> <?php $this->widget('Login'); ?>

11
app/views/page.tpl

@ -53,6 +53,17 @@
</noscript> </noscript>
<div id="hiddendiv"></div> <div id="hiddendiv"></div>
<div id="snackbar" class="snackbar"></div> <div id="snackbar" class="snackbar"></div>
<div id="error_websocket" class="snackbar">
<ul class="list">
<li>
<span class="control icon gray">
<i class="zmdi zmdi-code-setting"></i>
</span>
<p class="normal">
<?php echo __('error.websocket'); ?>
</p>
</li>
</div>
<?php $this->widget('Dialog');?> <?php $this->widget('Dialog');?>
<?php $this->widget('Drawer');?> <?php $this->widget('Drawer');?>
<?php $this->widget('Notification');?> <?php $this->widget('Notification');?>

5
app/widgets/AccountNext/AccountNext.php

@ -93,7 +93,10 @@ class AccountNext extends \Movim\Widget\Base {
function onServiceUnavailable() function onServiceUnavailable()
{ {
Notification::append(null, $this->__('error.service_unavailable')); Notification::append(null, $this->__('error.service_unavailable'));
RPC::call('remoteUnregister');
$session = \Sessionx::start();
requestURL('http://localhost:1560/disconnect/', 2, ['sid' => $session->sessionid]);
RPC::call('MovimUtils.redirect', $this->route('account')); RPC::call('MovimUtils.redirect', $this->route('account'));
} }

3
app/widgets/AccountNext/_accountnext_registered.tpl

@ -3,7 +3,8 @@
<h2 id="username">username@server.com</h2> <h2 id="username">username@server.com</h2>
<a class="button color" onclick="remoteUnregister(); MovimWebsocket.attach(function() {MovimUtils.redirect('{$c->route('disconnect')}')});" href="#">
<a class="button color"
href="{$c->route('disconnect')}">
{$c->__('page.login')} {$c->__('page.login')}
</a> </a>
</div> </div>

6
app/widgets/Login/_login_error.tpl

@ -4,5 +4,9 @@
<h4 class="gray">{$error}</h4> <h4 class="gray">{$error}</h4>
</section> </section>
<div> <div>
<span class="button flat oppose" onclick="remoteUnregister(); MovimWebsocket.attach(function() {MovimUtils.redirect('{$c->route('login')}')});">{$c->__('button.return')}</span>
<span
class="button flat oppose"
onclick="MovimUtils.redirect('{$c->route('disconnect')}');">
{$c->__('button.return')}
</span>
</div> </div>

3
app/widgets/Login/locales.ini

@ -10,7 +10,6 @@ session = Session error
account_created = Account successfully created account_created = Account successfully created
xmpp_unauthorized = Your XMPP server is unauthorized xmpp_unauthorized = Your XMPP server is unauthorized
mec_error = The server takes too much time to respond mec_error = The server takes too much time to respond
websocket = Movim cannot talk with the server, please try again later (Websocket connection error)
impossible = Impossible login impossible = Impossible login
title = Oops! title = Oops!
@ -30,6 +29,6 @@ create_one = Create one !
another_account = Another account another_account = Another account
no_account = No account yet ? no_account = No account yet ?
whitelist.info = You can login with accounts from theses servers
whitelist_info = You can login with accounts from theses servers
connected = Connected connected = Connected
population = Population population = Population

3
app/widgets/Login/login.js

@ -147,9 +147,6 @@ MovimWebsocket.attach(function()
Login.init(); Login.init();
// We hide the Websocket error
MovimUtils.hideElement(document.getElementById('error_websocket'));
// We enable the form // We enable the form
var inputs = document.querySelectorAll('#login_widget div input[disabled]'); var inputs = document.querySelectorAll('#login_widget div input[disabled]');
for (var i = 0; i < inputs.length; i++) for (var i = 0; i < inputs.length; i++)

8
app/widgets/Login/login.tpl

@ -62,9 +62,10 @@
{/if} {/if}
{if="isset($whitelist) && $whitelist != ''"} {if="isset($whitelist) && $whitelist != ''"}
<ul class="list thin card">
<ul class="list thin">
<li class="info"> <li class="info">
<p class="normal">{$c->__('whitelist.info')} : {$whitelist}</p>
<p></p>
<p class="center normal">{$c->__('form.whitelist_info')} : {$whitelist}</p>
</li> </li>
</ul> </ul>
{/if} {/if}
@ -87,6 +88,3 @@
</div> </div>
</div> </div>
<div id="error_websocket" class="snackbar">
{$c->__('error.websocket')}
</div>

1
locales/locales.ini

@ -35,6 +35,7 @@ cannot_load_file = "Cannot load file '%s'"
widget_load_error = "Requested widget '%s' doesn't exist." widget_load_error = "Requested widget '%s' doesn't exist."
widget_call_error = "Requested event '%s' not registered." widget_call_error = "Requested event '%s' not registered."
whoops = "Whoops!" whoops = "Whoops!"
websocket = Movim cannot talk with the server, please try again later (Websocket connection error)
[button] [button]
validate = Validate validate = Validate

9
themes/material/css/style.css

@ -439,6 +439,15 @@ main section > div:first-child:nth-last-child(2) ~ div .actions.fixed > div:last
opacity: 0; opacity: 0;
} }
.snackbar#error_websocket {
max-height: initial;
}
.snackbar#error_websocket p {
line-height: 2.5rem;
white-space: initial;
}
@media screen and (max-width: 22rem) { @media screen and (max-width: 22rem) {
.snackbar { .snackbar {
width: 100%; width: 100%;

Loading…
Cancel
Save