Skip to content

Commit 4bb21e0

Browse files
committed
Adding some UI usability
1 parent 6199ea8 commit 4bb21e0

File tree

2 files changed

+68
-14
lines changed

2 files changed

+68
-14
lines changed

Hubs/StreamHub.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public async Task StartStream(string streamName, ChannelReader<string> streamCon
3232
{
3333
// TODO:
3434
// Only allow each client to stream one at a time
35-
// Pass stream through to watchers
3635

3736
var channel = Channel.CreateBounded<string>(options: new BoundedChannelOptions(2) {
3837
FullMode = BoundedChannelFullMode.DropOldest
@@ -44,6 +43,8 @@ public async Task StartStream(string streamName, ChannelReader<string> streamCon
4443
throw new HubException("This stream name has already been taken.");
4544
}
4645

46+
await Clients.Others.SendAsync("NewStream", streamName);
47+
4748
try
4849
{
4950
// TODO: I didn't think `Context.ConnectionAborted` was needed here... need to check that out
@@ -62,6 +63,7 @@ public async Task StartStream(string streamName, ChannelReader<string> streamCon
6263
finally
6364
{
6465
_streamManager.RemoveStream(streamName);
66+
await Clients.Others.SendAsync("RemoveStream", streamName);
6567
channel.Writer.Complete();
6668
}
6769
}

Pages/Index.cshtml

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,21 @@ async function startAsync() {
3939
isStreaming = false;
4040
}
4141
});
42+
43+
connection.on("NewStream", function (name) {
44+
addStream(name);
45+
});
46+
47+
connection.on("RemoveStream", function (name) {
48+
removeStream(name);
49+
});
50+
4251
await connection.start();
4352
4453
const streams = await connection.invoke("ListStreams");
4554
if (streams.length > 0) {
46-
const list = document.getElementById('streamList');
47-
const listBody = list.getElementsByTagName('ul')[0];
48-
listBody.innerHTML = '';
4955
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]);
5957
}
6058
}
6159
@@ -98,9 +96,14 @@ async function startAsync() {
9896
startAsync();
9997
10098
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+
101104
const feed = document.getElementById("myPre");
102105
103-
connection.stream("WatchStream", streamName).subscribe({
106+
const ISub = connection.stream("WatchStream", streamName).subscribe({
104107
next: (item) => {
105108
feed.innerHTML = item;
106109
},
@@ -113,5 +116,54 @@ async function watchStream(streamName) {
113116
console.log("Failed to watch the stream: " + err);
114117
},
115118
});
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);
116168
}
117169
</script>

0 commit comments

Comments
 (0)