简体   繁体   中英

Windows phone 8 signalR connection method

I am new in programming on Windows 8 platform so I'm stuck. In my project I use SignalR (I downloaded and installed the package correctly) and tried to implement a connection simply, with this code : http://code.msdn.microsoft.com/wpapps/Windows-Phone-8-Chat-1fa5eccf

        public Play()
    {
        InitializeComponent();

        _connection = new HubConnection(websiteUrl);
        _myHub = _connection.CreateHubProxy("GameManager");
        InitializeConnection();
    }

    public async Task InitializeConnection()
    {
        try
        {
            var obj = new
            {
                header = 0,
                data = App.login,

            };
            var result = JsonConvert.SerializeObject(obj, Formatting.Indented);

            await _connection.Start();
            await _myHub.Invoke("SendConnection", result);
            IsConnected = true;

            _myHub.On<string>("recieved", data =>
            {
                if (OnRecieved != null)
                {
                    OnRecieved(this, new ChatEventArgs { MessageSent = data });
                    System.Diagnostics.Debug.WriteLine("DATA : " + data);
                }
            });

            if (OnConnected != null)
            {
                OnConnected(this, new EventArgs());
            }
        }
        catch
        {

        }
    } 

websiteUrl is the Url of the website I'm trying to reach.

However, I do not succeed in receiving a response from the server (and I know it returns something if the connection is successful).

Do I need to do something more or differently ? Thanks for your help.

Please assign the event before starting the connection. So the event will be properly mapped. I have catch the exception with an exception variable to see what error you are receiving. Also you have to check the authentication method defined in your signarl hosting application. If its cookie based then you have to pass the cookie details from windows phone. Otherwise disable authentication on server code.

public Play() { InitializeComponent();

    _connection = new HubConnection(websiteUrl);
    _myHub = _connection.CreateHubProxy("GameManager");
    InitializeConnection();
}

public async Task InitializeConnection()
{
    try
    {
        var obj = new
        {
            header = 0,
            data = App.login,

        };
        var result = JsonConvert.SerializeObject(obj, Formatting.Indented);

       _myHub.On<string>("recieved", data =>
        {
            if (OnRecieved != null)
            {
                OnRecieved(this, new ChatEventArgs { MessageSent = data });
                System.Diagnostics.Debug.WriteLine("DATA : " + data);
            }
        });


        await _connection.Start();
        await _myHub.Invoke("SendConnection", result);
        IsConnected = true;


        if (OnConnected != null)
        {
            OnConnected(this, new EventArgs());
        }
    }
    catch(Exception ex)
    {

    }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM