Skip to content
Snippets Groups Projects
Commit e8b80f40 authored by Eugen Ciur's avatar Eugen Ciur
Browse files

basic handling of websockets

parent 0e494433
No related branches found
No related tags found
No related merge requests found
......@@ -6,6 +6,17 @@ import { inject as service } from '@ember/service';
export default class ViewComponent extends Component {
@service requests;
@service websockets;
constructor(owner, args) {
super(owner, args);
this.websockets.addHandler(this.messageHandler, this);
}
messageHandler(message) {
console.log(message);
}
@action
onRunOCR() {
......
import ENV from 'papermerge/config/environment';
import Service from '@ember/service';
export default class Websockets extends Service {
constructor(owner, args) {
super(owner, args);
let that = this;
this._socket = new WebSocket(this.base_url);
this._handlers = [];
this._socket.onmessage = function(event) {
that._handlers.forEach((item) => {
let json_data;
try {
json_data = JSON.parse(event.data);
item.handler.apply(item.context, [json_data, event]);
} catch (err) {
console.log(`Error while parsing incoming data: ${event.data}`);
}
});
}
}
get base_url() {
return `${ENV.APP.WS_HOST}/${ENV.APP.WS_NAMESPACE}`;
}
addHandler(handler, context) {
this._handlers.push({handler, context});
}
}
\ No newline at end of file
......@@ -18,7 +18,8 @@ module.exports = function (environment) {
},
APP: {
NAMESPACE: 'api'
NAMESPACE: 'api',
WS_NAMESPACE: 'ws/document' // websockets namespace
// Here you can pass flags/options to your application instance
// when it is created
},
......@@ -26,6 +27,7 @@ module.exports = function (environment) {
if (environment === 'development') {
ENV.APP.HOST = 'http://127.0.0.1:8000'
ENV.APP.WS_HOST = 'ws://127.0.0.1:8000' //websockets
// ENV.APP.LOG_RESOLVER = true;
// ENV.APP.LOG_ACTIVE_GENERATION = true;
// ENV.APP.LOG_TRANSITIONS = true;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment