Call API and Apply Socket In Apngular 4
import { Component, OnInit } from '@angular/core';
import { WebServiceService} from '../web-service.service';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
@Component({
selector: 'app-websocket',
templateUrl: './websocket.component.html',
styleUrls: ['./websocket.component.css']
})
export class WebsocketComponent implements OnInit {
constructor(private ws: WebServiceService) {
console.log('contractor');
this.getChartData('ADABTC');
this.GetFeeds();
}
ngOnInit() {
}
getChartData(s) {
console.log('getChartData');
try {
let params = {
Symbol: s,
PkgId: this.ws.Trading_PkgId,
PartnerId: 0//this.GBS.DealerId
};
this.ws.GetChartHistory(params).subscribe(data => {
console.log('Chart Data',data);
});
} catch (e) { }
}
GetFeeds() {
console.log("Broad Cast Start");
var ws = this.ws.Broadcast('100|1|10');
var g = this.ws;
var me = this;
ws.onmessage = function (evt) {
try {
console.log("Broad Cast Data",evt);
} catch (e) {
console.log('Broadcast Socket Error', e);
}
}
}
}
webService.service.ts
import { Injectable } from '@angular/core';
import { HttpParams, HttpClient } from '@angular/common/http';
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/map';
@Injectable()
export class WebServiceService {
url: string = 'https://apis.xyz.com/CryptExchAPI_Bity';
socketUrl: string = 'ws://x.xxx.xxx.xxx:19555/';
Trading_PkgId: number = 14;
constructor(public http: Http) { }
GetChartHistory(params: any) {
return this.get('GET_HISTORICAL_DATA_BYID', params);
}
get(endPointg: string, params?: any, optn?: any) {
if (!optn) {
optn = {
params: new HttpParams()
};
};
let p = [];
let newParam;
if (params) {
for (let k in params) {
let str = k + '=' + params[k];
p.push(str);
}
newParam = p.join('&');
} try {
return this.http.get(this.url + '/' + endPointg + '?' + newParam)
.map(response => response.json());
} catch (e) {
console.log(e);
}
}
//SOCKET
Broadcast(params) {
return this.webSocket('BB', params);
}
webSocket(domain: string, param?: any) {
var fws = new WebSocket(this.socketUrl + domain);
fws.onopen = function () {
fws.send(param); // Send the message 'Ping' to the server
};
return fws;
}
}
No comments:
Post a Comment