Commit 4061936b authored by Thomas Mignot's avatar Thomas Mignot

Websocket reconnection, auto page reload

parent bb99f895
......@@ -12,8 +12,10 @@ then juste run ./manage.py runserver from the root directory of this project
What to do next?
Documentation for sure.
Plus a tutorial
OK/ Documentation for sure.
OK/ Automatic websocket reconnection and page reloading on success
A tutorial
Authentication:
- Without authentication and user specific filtering, it will remain a toy, a nice toy, but just a toy
......@@ -24,8 +26,6 @@ Server Side Rendering:
- the first HTTP request should return a full HTML page
- all links should be 'a' tag that have no effect on current location
Automatic websocket reconnection and page reloading on success
Change the way methods works, maybe by importing a dict and assigning funcptr to method name
Errors handling: It won't ever be stable enough without good errors
......
......@@ -8,6 +8,10 @@
<body>
</body>
<script>
$ = function(q) {
return document.querySelector(q);
};
ID = function(){
function chr4(){
return Math.random().toString(16).slice(-4);
......@@ -73,10 +77,6 @@
return elem;
};
var ws = new WebSocket(
'ws://' + window.location.host + '/ws/ddp/'
);
handleDDP = function(data) {
switch (data.type) {
case 'insert': constructDOM(data.params); break;
......@@ -114,34 +114,6 @@
parent.insertBefore(cur_node, prev_node)
};
ws.onmessage = function(e) {
var data = JSON.parse(e.data);
var result, error;
switch (data.type) {
case 'Connected': init(); break;
case 'DDP': handleDDP(data.params); break;
default:
if (data.type == 'Error')
error = data;
else if (data.type == 'Success')
result = data;
ws.callbacks[data._id](result, error);
delete ws.callbacks[data._id]
};
};
ws.onclose = function(e) {
};
ws.callbacks = [];
ws_send = function(data, cb) {
var _id = ID();
data._id = _id;
ws.callbacks[_id] = cb;
ws.send(JSON.stringify(data));
};
var initialized = false;
var current_url = '{{ url }}';
var query_string = '{{ query_string }}';
......@@ -182,13 +154,6 @@
}
};
onwsready = function(cb) {
if (typeof(window.onwsready_cb) == 'undefined') {
window.onwsready_cb = []
}
window.onwsready_cb.push(cb);
};
init = function() {
setup();
window.onwsready_cb.forEach(function(cb) {
......@@ -197,10 +162,6 @@
initialized = true;
};
onwsready(function() {
route(current_url, query_string);
});
call = function(name, params) {
ws_send({
type: 'method',
......@@ -214,9 +175,64 @@
});
};
$ = function(q) {
return document.querySelector(q);
var ws;
ws_connect = function(reconnecting) {
ws = new WebSocket(
'ws://' + window.location.host + '/ws/ddp/'
);
if (reconnecting) {
ws.onopen = function() {
window.location.reload(true);
};
};
ws.onmessage = function(e) {
var data = JSON.parse(e.data);
var result, error;
switch (data.type) {
case 'Connected': init(); break;
case 'DDP': handleDDP(data.params); break;
default:
if (data.type == 'Error')
error = data;
else if (data.type == 'Success')
result = data;
ws.callbacks[data._id](result, error);
delete ws.callbacks[data._id]
};
};
ws.onclose = function(e) {
setTimeout(function() {
ws_connect(true);
}, 1000);
};
ws.callbacks = [];
};
ws_connect();
ws_send = function(data, cb) {
var _id = ID();
data._id = _id;
ws.callbacks[_id] = cb;
ws.send(JSON.stringify(data));
};
onwsready = function(cb) {
if (typeof(window.onwsready_cb) == 'undefined') {
window.onwsready_cb = []
}
window.onwsready_cb.push(cb);
};
onwsready(function() {
route(current_url, query_string);
});
</script>
</html>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment