繁体   English   中英

在WP8中使用SignalR苦苦挣扎

[英]Struggling using SignalR in WP8

我有Windows Phone 8客户端。

我正在使用SignalR与服务器进行通信。

我需要UI来更新服务器中的消息。

我知道服务器部分是正确的,因为我已经设置了断点并使用了HTML5客户端。

问题在于WP8

我以前从未使用过WP8,所以不确定我是否做得正确。

我有这个:

    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");
        };
    }

我的UI最初指出已建立连接。

现在它正在从服务器中接收我所停留的消息。 我也尝试过这个:

  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");
        };
    }

哪种方法是正确的方法,我的代码有什么问题?

谢谢

要处理来自服务器的呼叫,请使用以下语法:

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

其中PckType是与通过以下代码发送的类型服务器等效的类型:

Clients.Caller.broadcastMessage(pck);

SignalR充当RPC服务,这意味着从客户端调用的方法必须存在于服务器上,反之亦然。 当然,这仅适用于Hub方法。

暂无
暂无

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

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