private bool ServerConfigRequestState = false; //获取服务端配置读取状态 /// <summary> /// 创建连接 /// </summary> public void CreateConnection() { InitComm();//初始化通信 clientObj.WcfService = baseServiceClient; string serverConfig = null; baseServiceClient.Open(); AddMessageHeader(baseServiceClient.InnerDuplexChannel as IContextChannel, "", (() => { clientObj.ClientID = baseServiceClient.CreateClient(clientObj.ClientName);//创建连接获取ClientID if (ServerConfigRequestState == false) { //重新获取服务端配置,如:是否压缩Json、是否加密Json serverConfig = baseServiceClient.MiddlewareConfig(); ServerConfigRequestState = true; } })); if (!string.IsNullOrEmpty(serverConfig)) { IsHeartbeat = serverConfig.Split(new char[] { '#' })[0] == "1" ? true : false; HeartbeatTime = Convert.ToInt32(serverConfig.Split(new char[] { '#' })[1]); IsMessage = serverConfig.Split(new char[] { '#' })[2] == "1" ? true : false; MessageTime = Convert.ToInt32(serverConfig.Split(new char[] { '#' })[3]); IsCompressJson = serverConfig.Split(new char[] { '#' })[4] == "1" ? true : false; IsEncryptionJson = serverConfig.Split(new char[] { '#' })[5] == "1" ? true : false; serializeType = (SerializeType)Convert.ToInt32(serverConfig.Split(new char[] { '#' })[6]); if (IsHeartbeat) { //开启发送心跳 if (timer == null) { StartTimer(); } else { timer.Start(); } } else { if (timer != null) { timer.Stop(); } } } if (backConfig != null) { backConfig(IsMessage, MessageTime); } if (createconnAction != null) { createconnAction(); } }
/// <summary> /// 发送心跳 /// </summary> /// <returns></returns> private bool Heartbeat() { DuplexBaseServiceClient _wcfService = clientObj.WcfService; if (_wcfService.State == CommunicationState.Closed || _wcfService.State == CommunicationState.Faulted) { ReConnection(false);//连接服务主机失败,重连 } try { bool ret = false; string serverConfig = null; AddMessageHeader(_wcfService.InnerDuplexChannel as IContextChannel, "", (() => { ret = _wcfService.Heartbeat(clientObj.ClientID); if (ServerConfigRequestState == false) { //重新获取服务端配置,如:是否压缩Json、是否加密Json serverConfig = _wcfService.MiddlewareConfig(); ServerConfigRequestState = true; } })); if (!string.IsNullOrEmpty(serverConfig)) { IsHeartbeat = serverConfig.Split(new char[] { '#' })[0] == "1" ? true : false; HeartbeatTime = Convert.ToInt32(serverConfig.Split(new char[] { '#' })[1]); IsMessage = serverConfig.Split(new char[] { '#' })[2] == "1" ? true : false; MessageTime = Convert.ToInt32(serverConfig.Split(new char[] { '#' })[3]); IsCompressJson = serverConfig.Split(new char[] { '#' })[4] == "1" ? true : false; IsEncryptionJson = serverConfig.Split(new char[] { '#' })[5] == "1" ? true : false; serializeType = (SerializeType)Convert.ToInt32(serverConfig.Split(new char[] { '#' })[6]); if (backConfig != null) { backConfig(IsMessage, MessageTime); } if (IsHeartbeat) { //开启发送心跳 if (timer == null) { StartTimer(); } else { timer.Start(); } } else { if (timer != null) { timer.Stop(); } } } if (ret == false)//表示服务主机关闭过,丢失了clientId,必须重新创建连接 { _wcfService.Abort(); CreateConnection(); MiddlewareLogHelper.WriterLog(LogType.MidLog, true, Color.Red, "上级中间件已丢失客户端信息,重新创建客户端连接成功!"); } return(ret); } catch (Exception err) { MiddlewareLogHelper.WriterLog(LogType.MidLog, true, Color.Red, "上级中间件失去连接!\n" + clientObj.PluginName + "\n" + err.Message); ServerConfigRequestState = false; //ReConnection(false);//连接服务主机失败,重连 return(false); } }