@@ -39,23 +39,21 @@ async function startAsync() {
39
39
isStreaming = false ;
40
40
}
41
41
});
42
+
43
+ connection .on (" NewStream" , function (name ) {
44
+ addStream (name);
45
+ });
46
+
47
+ connection .on (" RemoveStream" , function (name ) {
48
+ removeStream (name);
49
+ });
50
+
42
51
await connection .start ();
43
52
44
53
const streams = await connection .invoke (" ListStreams" );
45
54
if (streams .length > 0 ) {
46
- const list = document .getElementById (' streamList' );
47
- const listBody = list .getElementsByTagName (' ul' )[0 ];
48
- listBody .innerHTML = ' ' ;
49
55
for (let i = 0 ; i < streams .length ; i++ ) {
50
- const elem = document .createElement (' li' );
51
- const strmButton = document .createElement (' input' );
52
- strmButton .id = " button" + streams[i];
53
- strmButton .type = " button" ;
54
- strmButton .value = " Watch stream" ;
55
- strmButton .onclick = function () { watchStream (streams[i]); };
56
- elem .innerText = streams[i];
57
- elem .appendChild (strmButton);
58
- listBody .appendChild (elem);
56
+ addStream (streams[i]);
59
57
}
60
58
}
61
59
@@ -98,9 +96,14 @@ async function startAsync() {
98
96
startAsync ();
99
97
100
98
async function watchStream (streamName ) {
99
+ const watchBtn = document .getElementById (" button " + streamName);
100
+ watchBtn .setAttribute (" disabled" , " disabled" );
101
+ const closeBtn = document .getElementById (" button close " + streamName);
102
+ closeBtn .removeAttribute (" disabled" );
103
+
101
104
const feed = document .getElementById (" myPre" );
102
105
103
- connection .stream (" WatchStream" , streamName).subscribe ({
106
+ const ISub = connection .stream (" WatchStream" , streamName).subscribe ({
104
107
next : (item ) => {
105
108
feed .innerHTML = item;
106
109
},
@@ -113,5 +116,54 @@ async function watchStream(streamName) {
113
116
console .log (" Failed to watch the stream: " + err);
114
117
},
115
118
});
119
+
120
+ closeBtn .onclick = function () {
121
+ ISub .dispose ();
122
+ closeBtn .setAttribute (" disabled" , " disabled" );
123
+ watchBtn .removeAttribute (" disabled" );
124
+ };
125
+ }
126
+
127
+ function addStream (streamName ) {
128
+ const list = document .getElementById (' streamList' );
129
+ const listBody = list .getElementsByTagName (' ul' )[0 ];
130
+ if (listBody .children [0 ].innerHTML === " No streams available" ) {
131
+ // Clear the list
132
+ listBody .innerHTML = ' ' ;
133
+ }
134
+
135
+ const strmButton = document .createElement (' input' );
136
+ strmButton .id = " button " + streamName;
137
+ strmButton .type = " button" ;
138
+ strmButton .value = " Watch stream" ;
139
+ strmButton .onclick = function () { watchStream (streamName); };
140
+
141
+ const strmEndButton = document .createElement (' input' );
142
+ strmEndButton .id = " button close " + streamName;
143
+ strmEndButton .type = " button" ;
144
+ strmEndButton .value = " Stop watching stream" ;
145
+ strmEndButton .setAttribute (" disabled" , " disabled" );
146
+
147
+ const elem = document .createElement (' li' );
148
+ elem .id = " li " + streamName;
149
+ elem .innerText = streamName;
150
+ elem .appendChild (strmButton);
151
+ elem .appendChild (strmEndButton);
152
+ listBody .appendChild (elem);
153
+ }
154
+
155
+ function removeStream (streamName ) {
156
+ const list = document .getElementById (' streamList' );
157
+ const listBody = list .getElementsByTagName (' ul' )[0 ];
158
+ if (listBody .children .length === 1 ) {
159
+ listBody .innerHTML = ' ' ;
160
+ const elem = document .createElement (' li' );
161
+ elem .innerText = " No streams available" ;
162
+ listBody .appendChild (elem);
163
+ return ;
164
+ }
165
+
166
+ const li = document .getElementById (" li " + streamName);
167
+ listBody .removeChild (li);
116
168
}
117
169
</script >
0 commit comments