Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding utility classes to allow direct web socket/socketio/http connections to perspective in jupyterlab #59

Merged
merged 3 commits into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
adding more configuration
  • Loading branch information
timkpaine committed Mar 2, 2018
commit cf49a6c7d6434f68861370d5539ecaf5a600671f
7 changes: 1 addition & 6 deletions packages/perspective-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,8 @@
"watch": "tsc -w"
},
"dependencies": {
"@jpmorganchase/perspective-viewer": "^0.1.1",
"@jpmorganchase/perspective-viewer-highcharts": "^0.1.1",
"@jpmorganchase/perspective-viewer-hypergrid": "^0.1.1",
"@phosphor/messaging": "^1.2.2",
"@types/socket.io-client": "^1.4.0",
"socket.io": "*",
"@phosphor/widgets": "^1.5.0"
"socket.io": "*"
},
"devDependencies": {
"rimraf": "~2.6.2",
Expand Down
63 changes: 48 additions & 15 deletions packages/perspective-utils/src/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,57 @@ interface PSPHelper {

export
class PSPWebsocketHelper implements PSPHelper {
constructor(url:string, send:string) {
constructor(url: string, send: string, records: boolean) {
this.url = url;
this.send = JSON.parse(send);
this.send = send;
}

start(psp: any): void {
let socket = new WebSocket(this.url);
let to_send = this.send;
let as_record = this.records;
socket.onopen = function (event: any) {

}.bind(this);
if (to_send){
socket.send(to_send);
}
};

socket.onmessage = function (event: any) {
psp.update(event.msg);
}.bind(this);
if (as_record){
psp.update([event.data]);
} else {
psp.update(event.data);
}
};

if (this.send){
socket.send(this.send);
}
}

getUrl(): string {
return this.url;
}

private url:string;
private send:JSON;
private send:string;
private records:boolean;
}

export
class PSPSocketIOHelper implements PSPHelper {
constructor(url: string, channel: string) {
constructor(url: string, channel: string, records: boolean) {
this.url = url;
this.channel = channel;
this.records = records;
}

start(psp: any): void {
let socket = io.connect(this.url);
let as_record = this.records;
socket.on(this.channel, function (msg: any) {
psp.update([msg.msg]);
if (as_record){
psp.update([msg.msg]);
} else {
psp.update(msg.msg);
}
})
}

Expand All @@ -66,33 +78,54 @@ class PSPSocketIOHelper implements PSPHelper {

private url:string;
private channel:string;
private records:boolean;
}

export
class PSPHttpHelper implements PSPHelper {
constructor(url:string, field: string) {
constructor(url:string, field: string, records: boolean, repeat: number) {
this.url = url;
this.field = field;
this.records = records;
this.repeat = repeat;
}

start(psp: any): void {

sendAndLoad(psp: any){
let xhr = new XMLHttpRequest();
let field = this.field;
let as_record = this.records;

xhr.open('GET', this.url, true);
xhr.onload = function () {
let data = JSON.parse(xhr.response);
if (field !== ''){
data = data[field];
}
psp.update(data);

if (as_record){
psp.update([data]);
} else {
psp.update(data);
}
}
xhr.send(null);
}

start(psp: any): void {
if(this.repeat){
setInterval( () => this.sendAndLoad(psp), this.repeat);
} else {
this.sendAndLoad(psp);
}
}

getUrl(): string {
return this.url;
}

private url:string;
private field:string;
private records:boolean;
private repeat:number;
}