I made Api for SignalR using Asp.net api. it is working in Api side.
but it is not working when i run with Xamarin App.
I put Api code below:
public class ChatHub : Hub { public void Send(string name, string message) { // Call the broadcastMessage method to update clients. Clients.All.broadcastMessage(name, message); } public void SendMessage(string message, int color, string username) { Clients.All.UpdateChatMessage(message, color, username); } }
I put Xamarin Cs code below:
public partial class Chat : ContentPage { HubConnection hubConnection; public Chat() { InitializeComponent(); DoRealTimeSuff(); } async private void DoRealTimeSuff() { SignalRChatSetup(); await SignalRConnect(); } private void SignalRChatSetup() { // var ip = "localhost"; hubConnection = new HubConnectionBuilder().WithUrl($"https://750e01e568f9.ngrok.io/Home/Chat/").Build(); hubConnection.On<string, string>("UpdateChatMessage", (message, user) => { var UpdatedChatMessage = $"{user} says {message}"; this.MessageHolder.Text += UpdatedChatMessage + "\n"; }); } async Task SignalRConnect() { try { await hubConnection.StartAsync(); } catch (Exception ex) { // Connection failed. Fail graciously. } } private async void ButtonSendEventHandler(object sender, System.EventArgs e) { await SignalRSendMessage(this.UserName.Text, this.UserMessage.Text); } async Task SignalRSendMessage(string name, string message) { try { await hubConnection.InvokeAsync("Send", name, message); } catch (Exception ex) { // Send failed. Fail graciously. } } }
Xaml code.
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="Gmdemo.Views.Chat"> <ContentPage.Content> <StackLayout Orientation="Vertical" Margin="50"> <Entry Placeholder="User" x:Name="UserName" /> <Entry Placeholder="Message" x:Name="UserMessage" /> <Button Text="Send Message" Clicked="ButtonSendEventHandler" /> <Label x:Name="MessageHolder" /> </StackLayout> </ContentPage.Content> </ContentPage>
I have no idea why my Xamarin program could not connect with that Api.
When i check "hubConnection = new HubConnectionBuilder().WithUrl($"https://750e01e568f9.ngrok.io/Home/Chat/").Build();".
there is nothing that i get any values. I have no idea which part is wrong.