[英]Windows phone 8 signalR connection method
我是Windows 8平台编程的新手,所以我陷入了困境。 在我的项目中,我使用SignalR(我已正确下载并安装了软件包),并尝试使用以下代码简单地实现连接: 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是我要访问的网站的网址。
但是,我没有成功从服务器收到响应(并且我知道如果连接成功,它会返回一些信息)。
我是否需要做更多或不同的事情? 谢谢你的帮助。
在开始连接之前,请分配事件。 因此,事件将被正确映射。 我使用异常变量捕获了异常,以查看您收到的错误。 另外,您还必须检查在信号托管应用程序中定义的身份验证方法。 如果基于cookie,则必须从Windows Phone传递cookie详细信息。 否则,禁用对服务器代码的身份验证。
公共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)
{
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.