sljavad
sljavad
CC#
Created by sljavad on 2/7/2023 in #help
❔ cannot use InvokeAsync when streaming in signalR
FYI, i opened an issue for this you can follow it on github https://github.com/dotnet/aspnetcore/issues/46503
8 replies
CC#
Created by sljavad on 2/7/2023 in #help
❔ cannot use InvokeAsync when streaming in signalR
is this a problem in signalR ? should i open issue in github ?
8 replies
CC#
Created by sljavad on 2/7/2023 in #help
❔ cannot use InvokeAsync when streaming in signalR
my stream channel closes when code reaches to line await Clients.Caller.InvokeAsync<bool>("stream_handshake", "someData", token.Token); it invokes a method in the client and waits for response , but the porgram will not continue from this line when client returns the response. here is stream_handshake listener in client side:
connection.On<string, bool>("stream_handshake", async (input) =>
{
return true;
});
connection.On<string, bool>("stream_handshake", async (input) =>
{
return true;
});
it just return true , but server side will not get it and stream closes.
8 replies
CC#
Created by sljavad on 2/7/2023 in #help
❔ cannot use InvokeAsync when streaming in signalR
this code will Invoke StreamHandshake on the server and waits for response :
var res = await connection.InvokeAsync<bool>("StreamHandshake", "stream_handshake");
if (res)
{
await connection.SendAsync("ImageMessage", "hello");
}
var res = await connection.InvokeAsync<bool>("StreamHandshake", "stream_handshake");
if (res)
{
await connection.SendAsync("ImageMessage", "hello");
}
StreamHandshake method in the server side :
public async Task<bool> StreamHandshake(string data)
{
if (data == "stream_handshake")
{
try
{
var token = new CancellationTokenSource();
var res = await Clients.Caller.InvokeAsync<bool>("stream_handshake", "someData", token.Token);
return true;
}
catch (Exception ex)
{
Console.WriteLine("EX in StreamHandshake");
Console.WriteLine(ex.Message);
return false;
}

}
return false;
}
public async Task<bool> StreamHandshake(string data)
{
if (data == "stream_handshake")
{
try
{
var token = new CancellationTokenSource();
var res = await Clients.Caller.InvokeAsync<bool>("stream_handshake", "someData", token.Token);
return true;
}
catch (Exception ex)
{
Console.WriteLine("EX in StreamHandshake");
Console.WriteLine(ex.Message);
return false;
}

}
return false;
}
8 replies
CC#
Created by sljavad on 2/7/2023 in #help
❔ cannot use InvokeAsync when streaming in signalR
here is my server side code that reads stream:
public async Task ReplayFromAdminToServer(ChannelReader<int> replay)
{
try
{
while (await replay.WaitToReadAsync())
{
while (replay.TryRead(out var item))
{
Console.WriteLine(item);
}
}
}
catch (Exception ex)
{
Console.WriteLine("EXCEPTION");
Console.WriteLine(ex.Message);
}

Console.WriteLine("END OF ReplayFromAdminToServer");
}
public async Task ReplayFromAdminToServer(ChannelReader<int> replay)
{
try
{
while (await replay.WaitToReadAsync())
{
while (replay.TryRead(out var item))
{
Console.WriteLine(item);
}
}
}
catch (Exception ex)
{
Console.WriteLine("EXCEPTION");
Console.WriteLine(ex.Message);
}

Console.WriteLine("END OF ReplayFromAdminToServer");
}
here is client code that sends stream :
var channel = Channel.CreateUnbounded<int>();
await connection.SendAsync("ReplayFromAdminToServer", channel.Reader);
for (int i = 0; i < 1000; i++)
{
await Task.Delay(100);
await channel.Writer.WriteAsync(i);
richTextBox1.AppendText(i.ToString()+"\r\n");
}
var channel = Channel.CreateUnbounded<int>();
await connection.SendAsync("ReplayFromAdminToServer", channel.Reader);
for (int i = 0; i < 1000; i++)
{
await Task.Delay(100);
await channel.Writer.WriteAsync(i);
richTextBox1.AppendText(i.ToString()+"\r\n");
}
8 replies