SignalR dropping connection

TonyCaseyTonyCasey IEMember ✭✭

Hi guys,

I have a Signalr app that I am trying to get to invoke a method on the hub.

I have the exact same code running in a WindowsForms app and there is no issue, but on the Android when I call the invoke, I get the following...

System.InvalidOperationException: The connection has not been established.

at at Microsoft.AspNet.SignalR.Client.Connection.Send (string)

at at Microsoft.AspNet.SignalR.Client.Hubs.HubProxy.Invoke (string,object[]) <0x00609>

at at Microsoft.AspNet.SignalR.Client.Hubs.HubProxy.Invoke (string,object[])

at FSM.Android.Activity1.SendMessage () [0x00001] in c:\Users\Splay\Documents\Visual Studio 11\Projects\FSM.Android\Activity1.cs:76

at FSM.Android.Activity1.b__0 (object,System.EventArgs) [0x00001] in c:\Users\Splay\Documents\Visual Studio 11\Projects\FSM.Android\Activity1.cs:35

at Android.Views.View/IOnClickListenerImplementor.OnClick (Android.Views.View) [0x0000b] in /Users/builder/data/lanes/monodroid-lion-bs1/03814ac5/source/monodroid/src/Mono.Android/platforms/android-10/src/generated/Android.Views.View.cs:643

at Android.Views.View/IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ (intptr,intptr,intptr) [0x00010] in /Users/builder/data/lanes/monodroid-lion-bs1/03814ac5/source/monodroid/src/Mono.Android/platforms/android-10/src/generated/Android.Views.View.cs:614

at at (wrapper dynamic-method) object.182b4f8c-4934-4a2d-a76b-17834e966e49 (intptr,intptr,intptr)

The connection is established and running fine. Any new post that come in are being seen. I have tried a range of different tactics to try an get it to send but with no joy. As I said, the same code works perfect on WindowsForms..

    private IHubProxy _proxy;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        // Get our button from the layout resource,
        // and attach an event to it
        Button button = FindViewById<Button>(Resource.Id.MyButton);

        //button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };

        button.Click += delegate { SendMessage(); };

        Connect();
    }

    private void ReceivedMessage(string name, string message)
    {
        var a = name;
        var b = message;
        Console.WriteLine("Message received : " + a + " " + b );   
    }

    private void Connect()
    {
        var _connection = new HubConnection("http://192.168.1.4/FSM.WebApp/");

        _proxy = _connection.CreateHubProxy("ChatHub");

        _proxy.On<string, string>("broadcastMessage", ReceivedMessage);

        _connection.Error += ex => Console.WriteLine("An error occurred {0}", ex.Message);

        _connection.Closed += () => Console.WriteLine("Connection with client id {0} closed", _connection.ConnectionId);


        _connection.Start().ContinueWith(task =>
                                         {
            if (task.IsFaulted)
            {
                Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException());
            }
            else
            {
                Console.WriteLine("Success! Connected with client connection id {0}", _connection.ConnectionId);
            }
        });
    }


    private void SendMessage(){

        _proxy.Invoke("FromAndroid", "Hello World");

    }

Posts

  • TonyCaseyTonyCasey IEMember ✭✭

    The method on the hub looks like this...

        public void FromAndroid(string message)
        {
            Clients.Others.broadcastMessage("Android", message);
        }
    
  • JonathanPryorJonathanPryor USXamarin Team Xamurai

    Silly question, but how do you know that "[t]he connection is established and running fine" when SignalR believes otherwise?

    Based on the SignalR Connection.Send() source (hopefully that's the right source!), InvalidOperationException will be thrown when it's still connecting with the server. Calling Connection.Start() only sets the state to ConnectionState.Connecting; the state isn't set to ConnectionState.Connected until Connection.StartTransport() executes, which in turn won't execute until Connection.Negotiate() executes, which won't necessarily happen until you Wait() on the Task returned from Connection.Start().

    Are you calling connection.Start().Wait()?

  • TonyCaseyTonyCasey IEMember ✭✭

    Hi Jonathan,

    I know (well think) that the connection is fine because I can use the other clients to send messages and the "ReceivedMessage" method fires off and prints the message to the console for me.

    Running the exact same code also works perfect in WindowsForms.

    I tried the connection.Start().Wait() method and that basically stopped any further code executing, I think its a Synchronous method as opposed to aSync, I could be wrong.

    Considering the code works ok in WindowsForms I am guessing it might be the Microsoft.AspNet.SignalR.Client.dll I am using which is the only difference between the two.

    When I try using the same version, I get the Client Certificates issue which I cam across before so that's a no go.

    If there was a version of the dll available for download specifically for mono and android it would be great, I just can't seem to find one.

    Cheers

  • TonyCaseyTonyCasey IEMember ✭✭
    edited April 2013

    Got it !!

    My suspicions were right, it was the client dll that had the issue.

    Turns out, I was using the wrong one.

    When you download the Signalr sample projects, you should build the following one...

    Samples\SignalR-master\src\Microsoft.AspNet.SignalR.Client.Android

    Pick up the dll from the bin/debug folder and that is the correct one.

    I am sure somebody has probably said that somewhere before but I just never copped it. Hopefully anyone else out there as dim as me might find this helpful :-)

    Thanks for your help as always Jonathan.

Sign In or Register to comment.