python:
@http.route('/employeeTask', type='http', auth="public", website=True, csrf=False)
def employee_live_update_keeping_page(self, **kw):
if not self.check_logged_in_user():
return request.redirect('/web/login')
return http.request.render('iHotal.employee_task_live_update')
@http.route('/employee_task_updates', type='http', auth="public", website=True, csrf=False)
def employee_live_update_keeping(self, **kw):
if not self.check_logged_in_user():
return request.redirect('/web/login')
# record = request.env['helpdesk_tickets'].sudo().search([])
# [(rec.name, rec.id) for rec in record]
import werkzeug.wrappers
def events():
message = "data: The payload we are sending\n\n\n\n"
# if self.event is not None:
# message = f"{message}\nevent: {self.event}"
# if self.id is not None:
# message = f"{message}\nid: {self.id}"
# if self.retry is not None:
# message = f"{message}\nretry: {self.retry}"
# message = f"{message}\r\n\r\n"
return message.encode('utf-8')
die_rolls = {u"1D6": 5,
u"1D20": 9}
yield "data: " + json.dumps(die_rolls) + "\n\n"
response = werkzeug.wrappers.Response()
response.mimetype = 'text/event-stream'
response.headers['mimetype'] = 'text/event-stream'
response.headers['data'] = str({u"1D6": 5, u"1D20": 9}).encode('utf-8')
# response.data = events()
# response.headers['Cache-Control'] = 'public, max-age=604800'
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET, POST'
# # response.headers['Connection'] = 'close'
#
# # response.headers['Date'] = time.strftime("%a, %d-%b-%Y %T GMT", time.gmtime())
# # response.headers['Expires'] = time.strftime("%a, %d-%b-%Y %T GMT", time.gmtime(time.time() + 604800 * 60))
#
return response
return http.Response(events(), mimetype="text/event-stream")
javascript:$(document).ready(function(){
// if (!!window.EventSource) {
// var source = new EventSource("/employee_task_updates");
// } else {
// return
// }
// source.addEventListener('message', function(e) {
// console.log(e.data);
// }, false);
//
// source.addEventListener('open', function(e, d='4') {
// console.log('open');
// }, false);
//
// source.addEventListener('error', function(e) {
// if (e.readyState == EventSource.CLOSED) {
// console.log('error');
// }
// }, false);
var source = new EventSource("/employee_task_updates");
source.onerror = function(eventdata) {
console.log('error');
// relisten if fail to connect to eventsource
}
source.onmessage = function(eventdata) {
console.log(eventdata);
// var dice_roll = JSON.parse(eventdata["data"]);
// $.each(die_roll, function(index, dice_roll) {
// var new_li = jQuery('<li>');
// new_li.text(die_roll.1D6);
// $('#rolls').append(new_li);
// });
}
});
xml:<odoo>
<data>
<template id="employee_task_live_update" name="Employees Task">
<t t-call="website.layout">
<xpath expr="//*[id='wrap']" position="replace">
<script src="/iHotal/static/src/js/live-server-update.js"/>
<h1>Getting Task updates</h1>
<div id="result"></div>
</xpath>
</t>
</template>
</data>
</odoo>