Skip to content

Instantly share code, notes, and snippets.

@Tapanila
Created September 17, 2014 08:42
Show Gist options
  • Select an option

  • Save Tapanila/325bd3924714e5d9b9dc to your computer and use it in GitHub Desktop.

Select an option

Save Tapanila/325bd3924714e5d9b9dc to your computer and use it in GitHub Desktop.
private async Task ConnectoToSignalR()
{
hubConnection = new HubConnection(App.MobileService.ApplicationUri.AbsoluteUri);
hubConnection.Headers["x-zumo-application"] = App.MobileService.ApplicationKey;
IHubProxy proxy = hubConnection.CreateHubProxy("ChatHub");
await hubConnection.Start();
hubConnection.StateChanged += hubConnection_StateChanged;
proxy.On<string>("hello", async msg =>
{
var callbackDialog = new MessageDialog(msg);
callbackDialog.Commands.Add(new UICommand("OK"));
await callbackDialog.ShowAsync();
});
proxy.On<CloudObject>("CloudHello", async msg =>
{
var msgString = String.Format("name: {0} and owner {1} ", msg.Name, msg.Owner);
var callbackDialog = new MessageDialog(msgString);
callbackDialog.Commands.Add(new UICommand("OK"));
CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
await callbackDialog.ShowAsync();
});
});
}
async void hubConnection_StateChanged(StateChange obj)
{
if (obj.NewState != ConnectionState.Connected)
{
var dialog = new MessageDialog("Not connected");
await dialog.ShowAsync();
}
else
{
var dialog = new MessageDialog("Connected");
await dialog.ShowAsync();
}
}
@Tapanila
Copy link
Author

public class ChatHub : Hub
{
    public ApiServices Services { get; set; }

    public void SendObject(CloudObject cloudObject)
    {
        Services.GetRealtime<ChatHub>().Clients.All.CloudHello(cloudObject);
    }

    public string Send(string message)
    {
        Services.GetRealtime<ChatHub>().Clients.All.hello(message);
        return "Message sent!";
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment