简体   繁体   English

在WP8中使用SignalR苦苦挣扎

[英]Struggling using SignalR in WP8

I have a Windows Phone 8 client. 我有Windows Phone 8客户端。

I am using SignalR to communicate with my server. 我正在使用SignalR与服务器进行通信。

I need my UI to update with messages from my server. 我需要UI来更新服务器中的消息。

I know the server part is correct as I have set break points and have used a HTML5 client. 我知道服务器部分是正确的,因为我已经设置了断点并使用了HTML5客户端。

The issue is with WP8 问题在于WP8

I have never used WP8 before so I am not sure if I am doing it correctly. 我以前从未使用过WP8,所以不确定我是否做得正确。

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. 我的UI最初指出已建立连接。

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 谢谢

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: 其中PckType是与通过以下代码发送的类型服务器等效的类型:

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. SignalR充当RPC服务,这意味着从客户端调用的方法必须存在于服务器上,反之亦然。 Of course, this is only true for the Hub approach. 当然,这仅适用于Hub方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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