private int currentCount = 0; //timer #endregion #region functions private void WriteToStream(ChatMessageEntity messageEntity) { ObjectInversion inversion = new ObjectInversion(); byte[] byteArr = inversion.SerializeTo((object)messageEntity); writer.Write(byteArr); writer.Flush(); }
private void heartbeatTimer_Tick(object sender, EventArgs e) { tickCountInStep++; if (tickCountInStep == tickCount) { if (ChatClientCollection.Count > 0) { //计数器自动递增 expiryCountInStep++; foreach (ChatClient ChatClient in ChatClientLists) { ChatClientOnLineCounter[ChatClient]++; } //连续监测三次之后,开始监测集合中的掉线情况 if (expiryCountInStep == expiryCount) { //寻找集合中“掉线”的用户 var disconnectedChatClients = ChatClientOnLineCounter.Where(p => p.Value >= 3).ToList(); foreach (var disconnectedChatClient in disconnectedChatClients) { txtLog.Append("用户" + disconnectedChatClient.Key.name + "掉线!"); //删除集合中被视为掉线的用户 ChatClientLists.Remove(disconnectedChatClient.Key); ChatClientOnLineCounter.Remove(disconnectedChatClient.Key); //开始广播发送掉线用户 ChatMessageEntity entity = new ChatMessageEntity(); entity.MessageType = ChatStateEnum.OffLine; EndPoint curOfflineChatClientEP = disconnectedChatClient.Key.client.Client.RemoteEndPoint; string ChatClientName = disconnectedChatClient.Key.name; entity.MessageContentEx.Add(curOfflineChatClientEP, ChatClientName); ObjectInversion inversion = new ObjectInversion(); byte[] byteArr = inversion.SerializeTo((object)entity); try { foreach (ChatClient ChatClient in ChatClientLists) { ChatClient.writer.Write(byteArr); ChatClient.writer.Flush(); } } catch { } } expiryCountInStep = 0; } } tickCountInStep = 0; } }
private void BroadCastChatClients() { ChatMessageEntity entity = new ChatMessageEntity(); entity.MessageType = ChatStateEnum.OnLine; entity.MessageContentEx = ChatClientCollection; ObjectInversion inversion = new ObjectInversion(); byte[] byteArr = inversion.SerializeTo((object)entity); foreach (ChatClient ChatClient in ChatClientLists) { ChatClient.writer.Write(byteArr); ChatClient.writer.Flush(); } }