我有一个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");
};
}
我的用户界面最初表明已建立连接。
现在它正在接收来自服务器的消息,我被困住了。我也试过这个:
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");
};
}
我的代码的正确方法和错误是哪种方式?
感谢
答案 0 :(得分:1)
要处理来自服务器的调用,请使用以下语法:
proxy.On<PckType>("broadcastMessage", msg => {});
其中PckType
是与使用以下代码发送的类型服务器等效的类型:
Clients.Caller.broadcastMessage(pck);
SignalR充当RPC服务,这意味着从客户端调用的方法必须存在于服务器上,反之亦然。当然,这仅适用于Hub
方法。