/// <summary> /// 心跳完成回调 /// </summary> /// <param name="connectionID"></param> /// <param name="e"></param> private void completeHeartCallback(SocketSenderArgs arg, SocketError e) { switch (e) { case SocketError.HostDown: case SocketError.ConnectionAborted: case SocketError.ConnectionReset: this.currentPool.CloseConnection(arg.ConnectID); break; } }
public void Notify(string message, string userID, int msgLifeHour = 1) { byte[] buffer = Encoding.Default.GetBytes(message); var arg = new SocketSenderArgs() { ConnectID = userID, Message = message, MessageType = SocketMessageType.Notification, OnSendComplete = completeNotifyCallback, MessageFormatting = this.MessageFormatting, LifeHours = msgLifeHour }; CurrentSender.Enqueue(arg); }
/// <summary> /// 启动一个心跳计时器 /// </summary> /// <param name="heartString"></param> /// <param name="heartFrequency"></param> /// <returns></returns> public Timer InitTimingNotify(Action <SocketSenderArgs, SocketError> sendFailedCallBack, string heartString = "0", int heartFrequency = 60000) { SocketSenderArgs arg = new SocketSenderArgs() { Message = heartString, MessageType = SocketMessageType.HeartTick, OnSendComplete = sendFailedCallBack }; return(new Timer(new TimerCallback( (obj) => { this.Enqueue(obj as SocketSenderArgs); }) , arg, heartFrequency, heartFrequency)); }
/// <summary> /// 向连接池中所有用户发送通知 /// </summary> /// <param name="message"></param> public void Notify(string message, int msgLifeHour = 1) { foreach (var conn in currentPool.GetAll()) { var arg = new SocketSenderArgs() { ConnectID = conn.Key, Message = message, MessageType = SocketMessageType.Notification, OnSendComplete = completeNotifyCallback, MessageFormatting = this.MessageFormatting, LifeHours = msgLifeHour }; CurrentSender.Enqueue(arg); } }
/// <summary> /// 消息发送失败处理 /// </summary> /// <param name="userID"></param> /// <param name="message"></param> private void notifyFailed(SocketSenderArgs arg) { if (this.OfflineContainer == null) { return; } if (arg.LifeHours <= 0) { return; } OfflineMessageArgs temp = new OfflineMessageArgs { CreateTime = DateTime.Now, UserID = arg.ConnectID, Message = arg.Message, LiveHours = arg.LifeHours }; this.OfflineContainer.Add(temp); }
/// <summary> /// 通知发送完成回调 /// </summary> /// <param name="connectionID"></param> /// <param name="e"></param> private void completeNotifyCallback(SocketSenderArgs arg, SocketError e) { bool notifyResult; switch (e) { case SocketError.Success: notifyResult = true; Console.WriteLine("notify suceess {0}", arg.ConnectID); break; default: notifyResult = false; notifyFailed(arg); Console.WriteLine("notify failed {0}", arg.ConnectID); break; } if (this.NotifyComplete != null) { this.NotifyComplete(arg.ConnectID, notifyResult); } }
/// <summary> /// 入队 /// </summary> /// <param name="args"></param> public void Enqueue(SocketSenderArgs args) { concurrentQueue.Enqueue(args); }