feat(live): work on websocket data source, #3455

This commit is contained in:
Torkel Ödegaard
2016-03-14 13:20:55 +01:00
parent 8d7b7009c3
commit 2adc4d12be
9 changed files with 96 additions and 29 deletions

View File

@@ -5,24 +5,51 @@ import coreModule from 'app/core/core_module';
export class LiveSrv {
conn: any;
initPromise: any;
getWebSocketUrl() {
var l = window.location;
return ((l.protocol === "https:") ? "wss://" : "ws://") + l.host + config.appSubUrl + '/ws';
}
init() {
this.conn = new WebSocket("ws://localhost:3000/ws");
this.conn.onclose = function(evt) {
console.log("WebSocket closed");
};
this.conn.onmessage = function(evt) {
console.log("WebSocket message", evt.data);
};
this.conn.onopen = function(evt) {
console.log("Connection opened");
};
if (this.initPromise) {
return this.initPromise;
}
if (this.conn && this.conn.readyState === 1) {
return Promise.resolve();
}
this.initPromise = new Promise((resolve, reject) => {
console.log('Live: connecting...');
this.conn = new WebSocket(this.getWebSocketUrl());
this.conn.onclose = function(evt) {
reject({message: 'Connection closed'});
};
this.conn.onmessage = function(evt) {
console.log("Live: message received:", evt.data);
};
this.conn.onopen = function(evt) {
console.log('Live: connection open');
resolve();
};
});
return this.initPromise;
}
send(data) {
this.conn.send(JSON.stringify(data));
}
subscribe(name) {
if (!this.conn) {
this.init();
}
return this.init().then(() => {
this.send({action: 'subscribe', stream: name});
});
}
}