Skip to content

Commit c7f5602

Browse files
author
Wasin Waeosri
committed
Merge branch 'b3_handleIDs'
1. Support subscription IDs handler 2. Fix UI confusion
2 parents 890a963 + 8968981 commit c7f5602

File tree

6 files changed

+102
-43
lines changed

6 files changed

+102
-43
lines changed

README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,21 @@ The example supports Chrome, Firefox and IE11 (based on the WebSocket and Web Wo
1414

1515
## Prerequisite
1616
This example requires the following dependencies softwares.
17-
1. [Node.js](https://nodejs.org/en/) - version 6.10 or higher.
17+
1. [Node.js](https://nodejs.org/en/) runtime - version 8.9.3 or higher.
1818
2. [npm](https://www.npmjs.com/) package manager (included in Node.js)
19-
3. [TypeScript](https://www.typescriptlang.org) compiler
20-
4. [Express.js](https://expressjs.com/) web framework
19+
3. [TypeScript](https://www.typescriptlang.org) compiler (will be installed via ```npm install``` command)
20+
4. [Express.js](https://expressjs.com/) web framework (will be installed via ```npm install``` command)
2121

2222
This example also uses the following 3rd party libraries for UI presentation.
23-
1. [jQuery 3.2.1](https://jquery.com/)
24-
2. [Bootstrap 3.3.7](https://getbootstrap.com/docs/3.3/)
23+
1. [jQuery 3.2.1](https://jquery.com/) JavaScript library
24+
2. [Bootstrap 3.3.7](https://getbootstrap.com/docs/3.3/) CSS library
2525

2626
jQuery,Bootstrap and Express.js are distributed under the [MIT license](https://opensource.org/licenses/MIT). Please see more detail in the LICENSE.md file.
2727

28+
*Note: This example uses jQuery and Bootstrap for Web UI theme and color only, not for HTML DOM interaction.
29+
2830
## Package
29-
The project includes complete TypeScript source codes, a simple Express.js web server applciation file, CSS files and all required static dependencies. The dynamic dependencies for compiling and building JavaScript source file are defined in *package.json* file which can be installed via ```npm install``` command.
31+
The project includes complete TypeScript source codes, a simple Express.js web server applciation file, CSS files and all required static dependencies. All dynamic dependencies for compiling and building JavaScript source file are defined in *package.json* file which can be installed via ```npm install``` command.
3032

3133
The project includes the following files and folder
3234
- *src/* folder: The folder that contains all TypeScript source files
@@ -63,7 +65,12 @@ The project includes the following files and folder
6365
## References
6466
For further details, please check out the following resources:
6567
* [Thomson Reuters Elektron WebSocket API page](https://developers.thomsonreuters.com/websocket-api) on the [Thomson Reuters Developer Community](https://developers.thomsonreuters.com/) web site.
68+
* [Developer Webinar Recording: Introduction to Electron Websocket API](https://www.youtube.com/watch?v=CDKWMsIQfaw)
6669
* [TypeScript programming language: Documentation](https://www.typescriptlang.org/docs/home.html).
6770
* [Mozilla Developer Network: Web Workers API page](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API)
6871

6972
For any question related to this article or Elektron WebSocket API page, please use the Developer Community [Q&A Forum](https://community.developers.thomsonreuters.com/).
73+
74+
### Change logs
75+
- 21 December 2017: Fixed UI, change service name to be optional parameter, Add README.md content
76+
- 11 January 2018: Now the application can handle subscription IDs.

images/application_screen.png

51.2 KB
Loading

src/json_msg_classes.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,14 @@ class ItemRequestMsg implements JSONItemRequestMsg {
6161

6262
class ItemRequestMsgKey implements JSONItemRequestKey {
6363
Name: string;
64-
Service: string;
64+
Service?: string;
6565

66-
constructor(Name: string, Service: string) {
66+
constructor(Name: string, Service?: string) {
6767
this.Name = Name;
68-
this.Service = Service;
68+
if (Service !==''){
69+
this.Service = Service;
70+
}
71+
6972
}
7073
}
7174

src/json_msg_interface.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ export interface JSONItemRequestMsg {
2323
Key: JSONItemRequestKey;
2424
}
2525

26-
//Interface for Market Price domain item request's key attribute JSON message
26+
//Interface for Market Price domain item request's key attribute JSON message, service name is an optional
2727
export interface JSONItemRequestKey {
2828
Name: string;
29-
Service: string;
29+
Service?: string;
3030
}
3131

3232
//Interface for close JSON message

src/web_app.ts

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const protocol: string = "tr_json2";
88
const loginID: number = 1;
99
const loginDomain: string = "Login";
1010
let itemID: number = 0;
11+
let itemname:string = "";
1112

1213
let btnConnect: any;
1314
let inMessagePre: any;
@@ -39,13 +40,19 @@ function onOpen(event: any): void {
3940

4041
//An event listener to be called when a message is received from the server
4142
function onMessage(event: any): void {
42-
console.log(JSON.stringify(event.data));
43+
//console.log(JSON.stringify(event.data));
4344

4445
let incomingdata = JSON.parse(event.data.toString());
4546

4647
//Iterate each JSON message and send it to market_price_app.js
48+
let data:any = null;
4749
for (let index = 0; index < incomingdata.length; index++) {
48-
50+
data = incomingdata[index];
51+
//console.log(`incoming msgtype is ${data.Type} domain is ${data.Domain} itemname = ${itemname}`);
52+
if(data.Type === 'Refresh' && data.Key.Name === itemname){
53+
//Push subscription item name and ID to selection box
54+
pushIDstoDropDownMenu(data.Key.Name, data.ID);
55+
}
4956
display(inMessagePre,JSON.stringify(incomingdata[index], undefined, 2));
5057
//If incoming message is PING (server ping)
5158
if (incomingdata[index].Type === "Ping") {
@@ -98,11 +105,24 @@ window.onload = () => {
98105
btnSubscribe.addEventListener("click", function() {
99106
let txtServiceName:any = document.getElementById("txtServiceName");
100107
let txtItemName:any = document.getElementById("txtItemName");
101-
sendItemrequest(txtServiceName.value, txtItemName.value);
108+
itemname = txtItemName.value;
109+
sendItemrequest(txtServiceName.value, itemname);
102110
});
103111

104112
btnUnSubscribe.addEventListener("click", function() {
105-
sendItemCloserequest();
113+
//get Selected ID to unsubscribe
114+
let cb:any = document.getElementById("listenerCombo");
115+
if(cb.selectedIndex === -1){ //If user does not select any ID, alert user
116+
window.alert("Select ID first");
117+
return;
118+
}
119+
//get unsubscribe ID from HTML select element
120+
let unsubID:number = parseInt(cb.options[cb.selectedIndex].value);
121+
//Unsubscribe
122+
sendItemCloserequest(unsubID);
123+
124+
//remove unsubscribe ID from HTML select element
125+
cb.removeChild(cb.options[cb.selectedIndex]);
106126
});
107127

108128

@@ -129,7 +149,7 @@ function sendPong(): void {
129149
}
130150

131151
//Create the Item Request JSON message from ItemRequestMsg class and send it to ADS WebSocket
132-
function sendItemrequest(service: string, itemname: string): void {
152+
function sendItemrequest(service: string, item_name: string): void {
133153
//set Item ID value
134154
if (itemID === 0) {
135155
itemID = loginID + 1;
@@ -139,19 +159,22 @@ function sendItemrequest(service: string, itemname: string): void {
139159

140160
let itemrequest: ItemRequestMsg = new ItemRequestMsg(
141161
itemID,
142-
itemname,
162+
item_name,
143163
service
144164
);
145165

146166
ws.send(JSON.stringify(itemrequest));
147167
display(outMessagePre,JSON.stringify(itemrequest));
168+
169+
//pushIDstoDropDownMenu(item_name, itemID);
148170
}
149171

172+
150173
//Create the Item Close Request JSON message from ItemCloseRequestMsg class and send it to ADS WebSocket
151-
function sendItemCloserequest(): void {
174+
function sendItemCloserequest(unsubID:number): void {
152175
//let closeitemrequestMsg: ItemCloseRequestMsg = new ItemCloseRequestMsg(itemID);
153176

154-
let closeitemrequestMsg: CloseMsg = new CloseMsg(itemID);
177+
let closeitemrequestMsg: CloseMsg = new CloseMsg(unsubID);
155178

156179
ws.send(JSON.stringify(closeitemrequestMsg));
157180
display(outMessagePre,JSON.stringify(closeitemrequestMsg));
@@ -168,3 +191,13 @@ function sendCloseLoginrequest(): void {
168191
ws.close();
169192
btnConnect.innerHTML = "Connect";
170193
}
194+
195+
function pushIDstoDropDownMenu(itemname:string, id:number):void {
196+
197+
let cb:any = document.getElementById("listenerCombo");
198+
let opt:any = document.createElement("option");
199+
opt.value = id;
200+
opt.text = `${itemname} ID ${id}`;
201+
cb.options.add(opt);
202+
}
203+

web/index.html

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,50 @@
2020
<!-- Application JS -->
2121
<script src="./dist/ws_app.js"></script>
2222
</head>
23+
2324
<body>
2425
<!-- WebSocket connection UI input form -->
25-
<h4>Thomson Reuters WebSocket API with Web Workers Example</h4>
26+
<h4>Thomson Reuters WebSocket API Market Price Streaming Example with TypeScript</h4>
2627
<form class="form-inline">
2728

28-
<div class="form-group" >
29-
<label for="txtServerurl">Server:</label> &nbsp;
30-
<input type="text" id="txtServerurl" placeholder="ADS IP:WebSocket Port" class="form-control"> &nbsp;&nbsp;
31-
<button id="btnConnect" type="button" class="btn btn-sm btn-primary">Connect</button>
32-
</div>
33-
<br/>
34-
<br/>
35-
<div class="form-group" >
36-
<label for="txtUsername">User:</label> &nbsp;
37-
<input type="text" id="txtUsername" class="form-control">&nbsp;&nbsp;
38-
<button id="btnLogin" type="button" class="btn btn-sm btn-primary">Login</button> &nbsp;&nbsp;
39-
<button id="btnLogout" type="button" class="btn btn-sm btn-primary">Logout</button>
40-
</div>
41-
<br/>
42-
<br/>
43-
<div class="form-group" >
44-
<label for="txtServiceName">Name:</label> &nbsp;
45-
<input type="text" id="txtServiceName" class="form-control" placeholder="ELEKTRON_DD"> &nbsp;&nbsp;
46-
<label for="txtItemName">Name:</label>&nbsp;
47-
<input type="text" id="txtItemName" value="" class="form-control"> &nbsp;&nbsp;
48-
<button id="btnSubscribe" type="button" class="btn btn-sm btn-primary">Subscribe</button> &nbsp;&nbsp;
49-
<button id="btnUnSubscribe" type="button" class="btn btn-sm btn-primary">Unsubscribe</button>
50-
</div>
29+
<div class="form-group">
30+
<label for="txtServerurl">Server:</label> &nbsp;
31+
<input type="text" id="txtServerurl" placeholder="ADS IP:WebSocket Port" class="form-control"> &nbsp;&nbsp;
32+
<button id="btnConnect" type="button" class="btn btn-sm btn-primary">Connect</button>
33+
</div>
34+
<br/>
35+
<br/>
36+
<div class="form-group">
37+
<label for="txtUsername">User:</label> &nbsp;
38+
<input type="text" id="txtUsername" placeholder="user" class="form-control">&nbsp;&nbsp;
39+
<button id="btnLogin" type="button" class="btn btn-sm btn-primary">Login</button> &nbsp;&nbsp;
40+
<button id="btnLogout" type="button" class="btn btn-sm btn-primary">Logout</button>
41+
</div>
42+
<br/>
43+
<br/>
44+
<div class="form-group">
45+
<label for="txtItemName">Item Name:</label>&nbsp;
46+
<input type="text" id="txtItemName" placeholder="TRI.N" value="" class="form-control"> &nbsp;&nbsp;
47+
<label for="txtServiceName">Service Name:</label> &nbsp;
48+
<input type="text" id="txtServiceName" class="form-control" placeholder="[Optional]"> &nbsp;&nbsp;
49+
<button id="btnSubscribe" type="button" class="btn btn-sm btn-primary">Subscribe</button> &nbsp;&nbsp;
50+
<button id="btnUnSubscribe" type="button" class="btn btn-sm btn-primary">Unsubscribe</button>
51+
</div>
52+
<br/>
53+
<br/>
54+
<div class="form-group">
55+
<label for="txtIDSubscription">Subscription IDs:</label>&nbsp;
56+
<select id="listenerCombo" size="2" class="form-control"></select>
57+
<!--<div class="dropdown">
58+
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true"
59+
aria-expanded="true">
60+
Subscription IDs:
61+
<span class="caret"></span>
62+
</button>
63+
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1" id="dropdownIDs">
64+
</ul>
65+
</div>-->
66+
</div>
5167
</form>
5268
<!-- Display outgoing messages and commands -->
5369
<h5>Outgoing Messages:</h5>

0 commit comments

Comments
 (0)