//private static HproseClient orderClient; static ServiceProvider() { hpClient = HproseClient.Create(string.Format("tcp://{0}", ConfigurationManager.AppSettings["hproseIpPort"])) as HproseTcpClient; hpClient.AddFilter(new HproseTokenFilter(ConfigurationManager.AppSettings["hproseSecretKey"])); //orderClient = HproseClient.Create(string.Format("tcp://{0}", ConfigurationManager.AppSettings["orderServicesIpPort"])); }
/// <summary> /// 直接使用服务地址访问 /// </summary> /// <param name="serviceHost"></param> /// <param name="servicePort"></param> /// <param name="onError"></param> public RpcTcpClient(string serviceHost, int servicePort, Action <string, Exception> onError = null) { _client = new HproseTcpClient($"tcp://{serviceHost}:{servicePort}/"); if (onError != null) { _client.OnError += (name, e) => { onError(name, e); }; } }
/// <summary> /// /// </summary> /// <param name="serviceName"></param> /// <param name="serviceRegistryAddress"></param> /// <param name="loadBalancer"></param> /// <param name="onError"></param> public RpcTcpClient(string serviceName, ServiceRegistryAddress serviceRegistryAddress, ILoadBalancer loadBalancer = null, Action <string, Exception> onError = null) { var consulDiscoveryService = new ConsulDiscoveryService(serviceRegistryAddress, loadBalancer); var service = consulDiscoveryService.GetRpcService(serviceName); if (service == null) { throw new ServiceDiscoveryException($"未发现可用的【{serviceName}】 服务"); } _client = new HproseTcpClient(service.ToString()); if (onError != null) { _client.OnError += (name, e) => { onError(name, e); }; } }
static void Main(string[] args) { //HproseTcpClient client0 = new HproseTcpClient("tcp://gl.huwan.net.cn:8086/"); //HproseTcpClient client = new HproseTcpClient("tcp://gl.huwan.net.cn:8083/"); HproseTcpClient client0 = new HproseTcpClient("tcp://192.168.2.117:8082/"); HproseTcpClient client = new HproseTcpClient("tcp://192.168.2.117:8083/"); //使用管理员账号登录 string sysName = "PlayerA"; string sysPass = "******"; string uName = sysName; string pass = userMd5(sysPass); client0.Timeout = 2000; NetMessage user = null; int is_login = 0; string token = ""; string user_id = ""; try { user = client0.Invoke<NetMessage>("login", new object[] { uName, pass }); if (user.tag == msg_tag.sys_login) { string[] body = user.body.Split('|'); switch (body[0]) { case "0": //登录失败(账号或密码有误) is_login = 0; break; case "1": //成功 token = body[1]; user_id = body[2]; is_login = 1; break; case "2": //网络错误 is_login = 2; break; } } } catch (Exception ex) { is_login = 3; Console.WriteLine("登录失败(未知错误)"+ex); Console.ReadKey(); } if(is_login == 0) { Console.WriteLine("登录失败(账号或密码有误)"); }else if(is_login == 2) { Console.WriteLine("登录失败(网络错误)"); }else if(is_login == 3) { Console.WriteLine("登录失败(未知错误)"); }else if(is_login == 1) { pvpdbDataContext DB = new pvpdbDataContext(); var pvprank = from r in DB.t_pvp_rankingList orderby r.rangking select r; foreach(var i in pvprank) { sendgift(i.rangking,i.user_id,token,user_id); } } }
static void Main(string[] args) { //HproseTcpClient client0 = new HproseTcpClient("tcp://gl.huwan.net.cn:8086/"); //HproseTcpClient client = new HproseTcpClient("tcp://gl.huwan.net.cn:8083/"); HproseTcpClient client0 = new HproseTcpClient("tcp://192.168.2.117:8082/"); HproseTcpClient client = new HproseTcpClient("tcp://192.168.2.117:8083/"); //使用管理员账号登录 string sysName = "PlayerA"; string sysPass = "******"; string uName = sysName; string pass = userMd5(sysPass); client0.Timeout = 2000; NetMessage user = null; int is_login = 0; string token = ""; string user_id = ""; try { user = client0.Invoke <NetMessage>("login", new object[] { uName, pass }); if (user.tag == msg_tag.sys_login) { string[] body = user.body.Split('|'); switch (body[0]) { case "0": //登录失败(账号或密码有误) is_login = 0; break; case "1": //成功 token = body[1]; user_id = body[2]; is_login = 1; break; case "2": //网络错误 is_login = 2; break; } } } catch (Exception ex) { is_login = 3; Console.WriteLine("登录失败(未知错误)" + ex); Console.ReadKey(); } if (is_login == 0) { Console.WriteLine("登录失败(账号或密码有误)"); } else if (is_login == 2) { Console.WriteLine("登录失败(网络错误)"); } else if (is_login == 3) { Console.WriteLine("登录失败(未知错误)"); } else if (is_login == 1) { pvpdbDataContext DB = new pvpdbDataContext(); var pvprank = from r in DB.t_pvp_rankingList orderby r.rangking select r; foreach (var i in pvprank) { sendgift(i.rangking, i.user_id, token, user_id); } } }