How to do voice chat through SignalR and ASP.NET Core?
I try out server-side hub connection
public async Task SendAudio(Stream audioStream)
{
// Process the audio stream as needed (e.g., save to file, broadcast to other clients)
// Here, we broadcast the audio to all connected clients
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = audioStream.Read(buffer, 0, buffer.Length)) > 0)
{
await Clients.All.SendAsync("ReceiveAudio", buffer);
}
}
I try out client side connection hub with wpf app
public MainWindow()
{
InitializeComponent();
_hubConnection = new HubConnectionBuilder()
.WithUrl(server)
.Build();
waveIn = new WaveInEvent();
waveIn.DataAvailable += WaveIn_DataAvailable;
UserName = user.Text.ToString();
_hubConnection.On<string, string>("ReceiveMessage", (user, message) =>
{
Dispatcher.Invoke(() =>
{
mychat.Items.Add($"{user}: {message}\n");
});
});
_hubConnection.On<byte[]>("ReceiveAudio", (audioChunk) =>
{
PlayAudioData(audioChunk);
});
}
private void voice_start(object sender, RoutedEventArgs e)
{
waveIn.StartRecording();
isMicrophoneEnabled = true;
startButton.IsEnabled = false;
stopButton.IsEnabled = true;
}
private void WaveIn_DataAvailable(object sender, WaveInEventArgs e)
{
if (isMicrophoneEnabled)
{
byte[] audioFrame = new byte[e.BytesRecorded];
Buffer.BlockCopy(e.Buffer, 0, audioFrame, 0, e.BytesRecorded);
byte[] myByteArray = audioFrame;
MemoryStream stream = new MemoryStream();
stream.Write(myByteArray, 0, myByteArray.Length);
Task.Run(() =>
{
_hubConnection.InvokeAsync("SendAudio", stream);
});
foreach(var data in audioFrame)
{
Console.WriteLine(data);
}
}
}
I said that audio bytes are sent and received between the server and the client, but the bytes are being recorded in real time, but not being sent or received by the server. Is this the problem?