Andrew Simpson
Andrew Simpson

Reputation: 7314

Struggling using SignalR in WP8

I have a Windows Phone 8 client.

I am using SignalR to communicate with my server.

I need my UI to update with messages from my server.

I know the server part is correct as I have set break points and have used a HTML5 client.

The issue is with WP8

I have never used WP8 before so I am not sure if I am doing it correctly.

I have this:

    public MainPage()
    {
        this.InitializeComponent();
        this.NavigationCacheMode = NavigationCacheMode.Required;

          connection.Start().ContinueWith(task =>
        {
            if (task.IsFaulted)
            {
                UpdateConnectionState("Not Connected");            
            }
            else
            {
                UpdateConnectionState(string.Format("Success! Connected with client connection id {0}", connection.ConnectionId));
                hubuserid = connection.ConnectionId;
                //not important for now LogIn();
            }
        });

        connection.Received += data =>
        {
            UpdateConnectionState(data);
        };
        connection.Error += ex =>
        {
            UpdateConnectionState(string.Format("An error occurred {0}", ex.Message));
        };
        connection.Closed += () =>
        {
            UpdateConnectionState(string.Format("Connection with client id {0} closed", connection.ConnectionId));
        };
        connection.Reconnected += () =>
        {
            UpdateConnectionState("The connection was re-established");
        };
    }

My UI initially states a connection has been made.

It is now receiving messages from the Server that I am stuck at. I have also tried this:

  private async void UpdateTime(string data)
    {
        await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {
            txtInfo.Text = data;
        });
    }
    public MainPage()
    {
        this.InitializeComponent();
        this.NavigationCacheMode = NavigationCacheMode.Required;

        proxy.On<string>("internetUpTime", UpdateTime);

        connection.Start().ContinueWith(task =>
        {
            if (task.IsFaulted)
            {
                UpdateConnectionState("Not Connected");            
            }
            else
            {
                UpdateConnectionState(string.Format("Success! Connected with client connection id {0}", connection.ConnectionId));
                hubuserid = connection.ConnectionId;
            }
        });

        //connection.Received += data =>
        //{
        //    UpdateConnectionState(data);
        //};
        connection.Error += ex =>
        {
            UpdateConnectionState(string.Format("An error occurred {0}", ex.Message));
        };
        connection.Closed += () =>
        {
            UpdateConnectionState(string.Format("Connection with client id {0} closed", connection.ConnectionId));
        };
        connection.Reconnected += () =>
        {
            UpdateConnectionState("The connection was re-established");
        };
    }

Which way is the correct way and what is wrong with my code?

thanks

Upvotes: 0

Views: 74

Answers (1)

Toni Petrina
Toni Petrina

Reputation: 7112

To handle calls from the server, use the following syntax:

proxy.On<PckType>("broadcastMessage", msg => {});

Where PckType is the type that is the equivalent to the type server sent with the following code:

Clients.Caller.broadcastMessage(pck);

SignalR acts as a RPC service which means methods called from the client must exist on the server and vice versa. Of course, this is only true for the Hub approach.

Upvotes: 1

Related Questions