在Silverlight开发基于http协议客户端连接器(三)
2010-09-02 17:44 姜 萌@cnblogs 阅读(365) 评论(0) 编辑 收藏 举报
下面应用前面设计的这个连接器演练一个登录服务并保持session。
先定消息,就是用LoginMessage好了。
这里说明一下,很多页面访问的时候都是需要验证的,比如qq、178的游戏页面都会包含appid,sessionkey,uid之类的信息,这样就能够防止非注册用户访问他们的webgame。所以我们还要收集html中的这些数据用于登录用。在silverlight中可以通过HTML Bridge来获取DOM,其实用起来也很简单,在js中写function,再在sl中调用HtmlPage.Window.Invoke(方法名,参数名……)。
下面是获取某页面验证信息用的的工具类:
PageMessageUtil
public static class PageMessageUtil
{
public static PageSignEntity GetData()
{
if ((string)HtmlPage.Window.Invoke("get178PageData", "178_sig_uid") != "")
{
var entity = new PageSignEntity();
entity.Id = (string)HtmlPage.Window.Invoke("get178PageData", "178_sig_uid");
entity.Signtime = (string)HtmlPage.Window.Invoke("get178PageData", "178_sig_time");
entity.Appid = (string)HtmlPage.Window.Invoke("get178PageData", "178_sig_appId");
entity.Appversion = (string)HtmlPage.Window.Invoke("get178PageData", "178_sig_v");
entity.Isaddapp = (string)HtmlPage.Window.Invoke("get178PageData", "178_sig_added");
entity.Sessionkey = (string)HtmlPage.Window.Invoke("get178PageData", "178_sig_sessionId");
entity.Friendlist = (string)HtmlPage.Window.Invoke("get178PageData", "178_sig_friends");
entity.Signsumber = (string)HtmlPage.Window.Invoke("get178PageData", "178_sig_key");
return entity;
}
else
{
MessageBox.Show("没有给定参数");
System.Diagnostics.Debug.WriteLine("没有给定参数");
return null;
}
}
}
{
public static PageSignEntity GetData()
{
if ((string)HtmlPage.Window.Invoke("get178PageData", "178_sig_uid") != "")
{
var entity = new PageSignEntity();
entity.Id = (string)HtmlPage.Window.Invoke("get178PageData", "178_sig_uid");
entity.Signtime = (string)HtmlPage.Window.Invoke("get178PageData", "178_sig_time");
entity.Appid = (string)HtmlPage.Window.Invoke("get178PageData", "178_sig_appId");
entity.Appversion = (string)HtmlPage.Window.Invoke("get178PageData", "178_sig_v");
entity.Isaddapp = (string)HtmlPage.Window.Invoke("get178PageData", "178_sig_added");
entity.Sessionkey = (string)HtmlPage.Window.Invoke("get178PageData", "178_sig_sessionId");
entity.Friendlist = (string)HtmlPage.Window.Invoke("get178PageData", "178_sig_friends");
entity.Signsumber = (string)HtmlPage.Window.Invoke("get178PageData", "178_sig_key");
return entity;
}
else
{
MessageBox.Show("没有给定参数");
System.Diagnostics.Debug.WriteLine("没有给定参数");
return null;
}
}
}
PageSignEntity
public class PageSignEntity
{
public PageSignEntity()
{
}
public string Id { get; set; }
public string Signtime { get; set; }
public string Appid { get; set; }
public string Appversion { get; set; }
public string Isaddapp { get; set; }
public string Sessionkey { get; set; }
private string friendList;
public string Friendlist
{
get { return friendList; }
set
{
friendList = value ?? string.Empty;
}
}
public string Signsumber { get; set; }
}
{
public PageSignEntity()
{
}
public string Id { get; set; }
public string Signtime { get; set; }
public string Appid { get; set; }
public string Appversion { get; set; }
public string Isaddapp { get; set; }
public string Sessionkey { get; set; }
private string friendList;
public string Friendlist
{
get { return friendList; }
set
{
friendList = value ?? string.Empty;
}
}
public string Signsumber { get; set; }
}
继承Message,设置有效的url参数
LoginMessage
public class LoginMessage : Message
{
public readonly static string Uid = "178_sig_uid";
public readonly static string Signtime = "178_sig_time";
public readonly static string Appid = "178_sig_appId";
public readonly static string Appversion = "178_sig_v";
public readonly static string Isaddapp = "178_sig_added";
public readonly static string Sessionkey = "178_sig_sessionId";
public readonly static string Friendlist = "178_sig_friends";
public readonly static string Signsumber = "178_sig_key";
public LoginMessage()
{
init();
}
public LoginMessage(PageSignEntity dataEntity)
{
init();
base.Parameters[LoginMessage.Uid] = dataEntity.Id;
base.Parameters[LoginMessage.Appid] = dataEntity.Appid;
base.Parameters[LoginMessage.Appversion] = dataEntity.Appversion;
base.Parameters[LoginMessage.Signtime] = dataEntity.Signtime;
base.Parameters[LoginMessage.Signsumber] = dataEntity.Signsumber;
base.Parameters[LoginMessage.Sessionkey] = dataEntity.Sessionkey;
base.Parameters[LoginMessage.Isaddapp] = dataEntity.Isaddapp;
base.Parameters[LoginMessage.Friendlist] = dataEntity.Friendlist;
}
private void init()
{
base.MessageId = "10000";
//可以用反射少些代码,或用code generater的方式
base.AvaliableParameterNames.Add(Uid);
base.AvaliableParameterNames.Add(Signtime);
base.AvaliableParameterNames.Add(Appid);
base.AvaliableParameterNames.Add(Appversion);
base.AvaliableParameterNames.Add(Isaddapp);
base.AvaliableParameterNames.Add(Sessionkey);
base.AvaliableParameterNames.Add(Friendlist);
base.AvaliableParameterNames.Add(Signsumber);
}
}
{
public readonly static string Uid = "178_sig_uid";
public readonly static string Signtime = "178_sig_time";
public readonly static string Appid = "178_sig_appId";
public readonly static string Appversion = "178_sig_v";
public readonly static string Isaddapp = "178_sig_added";
public readonly static string Sessionkey = "178_sig_sessionId";
public readonly static string Friendlist = "178_sig_friends";
public readonly static string Signsumber = "178_sig_key";
public LoginMessage()
{
init();
}
public LoginMessage(PageSignEntity dataEntity)
{
init();
base.Parameters[LoginMessage.Uid] = dataEntity.Id;
base.Parameters[LoginMessage.Appid] = dataEntity.Appid;
base.Parameters[LoginMessage.Appversion] = dataEntity.Appversion;
base.Parameters[LoginMessage.Signtime] = dataEntity.Signtime;
base.Parameters[LoginMessage.Signsumber] = dataEntity.Signsumber;
base.Parameters[LoginMessage.Sessionkey] = dataEntity.Sessionkey;
base.Parameters[LoginMessage.Isaddapp] = dataEntity.Isaddapp;
base.Parameters[LoginMessage.Friendlist] = dataEntity.Friendlist;
}
private void init()
{
base.MessageId = "10000";
//可以用反射少些代码,或用code generater的方式
base.AvaliableParameterNames.Add(Uid);
base.AvaliableParameterNames.Add(Signtime);
base.AvaliableParameterNames.Add(Appid);
base.AvaliableParameterNames.Add(Appversion);
base.AvaliableParameterNames.Add(Isaddapp);
base.AvaliableParameterNames.Add(Sessionkey);
base.AvaliableParameterNames.Add(Friendlist);
base.AvaliableParameterNames.Add(Signsumber);
}
}
我们从WebConnectorContext继承来一个LkDemoDomainService。
LkDemoDomainService
public class LkDemoDomainService : WebConnectorContext
{
public LkDemoDomainService(string baseAddress)
: base(baseAddress)
{
}
public void LoginGameFromPageSign()
{
LoginGameFromPageSign("http://localhost:8085/igscdemo/login.sa%22);
}
public void LoginGameFromPageSign(string loginUrl)
{
var msg = new LoginMessage(PageMessageUtil.GetData());
var loginService = new LkDemoDomainService(loginUrl);
}
public void Login(Message msg, Action<WebClientWrapper, string> action)
{
loginService.AddMessageToSend(msg, (client, s) =>
{
//解析返回的结果,略
base.AppCookie = loginService.AppCookie;//保存用户sessionid等
//提示登录结果,略
});
}
public override void AddMessageToSend(Message msg, Action<WebClientWrapper, string> action)
{
base.AddMessageToSend(msg, action);
}
}
{
public LkDemoDomainService(string baseAddress)
: base(baseAddress)
{
}
public void LoginGameFromPageSign()
{
LoginGameFromPageSign("http://localhost:8085/igscdemo/login.sa%22);
}
public void LoginGameFromPageSign(string loginUrl)
{
var msg = new LoginMessage(PageMessageUtil.GetData());
var loginService = new LkDemoDomainService(loginUrl);
}
public void Login(Message msg, Action<WebClientWrapper, string> action)
{
loginService.AddMessageToSend(msg, (client, s) =>
{
//解析返回的结果,略
base.AppCookie = loginService.AppCookie;//保存用户sessionid等
//提示登录结果,略
});
}
public override void AddMessageToSend(Message msg, Action<WebClientWrapper, string> action)
{
base.AddMessageToSend(msg, action);
}
}
之后我们假如有LichKingGameDomainService继承于WebConnectorContext,只需将登录的AppCookie设置给LichKingGameDomainService对象即可使LichKingGameDomainService对象发送出去的消息含有用户的sessionid等cookie值。