mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(live): work on websocket data source, #3455
This commit is contained in:
@@ -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});
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user