public override void Execute(SocketServer server, SocketSession session, SockectRequestMessage requestInfo) { PubishInformaction info = null; try { if (TryReadPubishInformaction(requestInfo, out info)) { var msgServer = (MessageServer)server; msgServer.MessageAgent.Pubish(info); } else { throw new MessageAgentException("未能正常解析推送数据!"); } } catch (Exception ex) { if (info != null) { session.Logger.Error(string.Format("推送失败,请查看推送信息是否完整!异常消息:{0}{1}推送主题:{2}推送内容:{3}", ex.Message, Environment.NewLine, info.Topic, info.Content), ex); } else { session.Logger.Error(string.Format("推送失败,请查看推送信息是否完整!异常消息:{0}{1}推送信息为空!", ex.Message, Environment.NewLine), ex); } } }
private bool TryReadPubishInformaction(SockectPackageMessage package, out PubishInformaction info) { info = null; if (package.Body.Length < 5) { return(false); } var len = BitConverter.ToInt32(package.Body, 0); string topic; if (package.TryReadFromText(package.Body, 4, len, out topic)) { string content; if (package.TryReadFromText(package.Body, 4 + len, package.Body.Length - 4 - len, out content)) { info = new PubishInformaction() { Topic = topic, Content = content }; return(true); } } return(false); }
public bool SocketPubish(SubscribeInformaction subInfo, PubishInformaction pubInfo, string sessionId) { var session = Server.GetSessionByID(sessionId); if (session != null && session.Status == SessionStatus.Started && session.Connected) { try { var topicBytes = session.TextToBytes(pubInfo.Topic); var contentBytes = session.TextToBytes(pubInfo.Content); var lenBytes = BitConverter.GetBytes(topicBytes.Length); var body = new byte[topicBytes.Length + contentBytes.Length + 4]; Array.Copy(lenBytes, 0, body, 0, 4); Array.Copy(topicBytes, 0, body, 4, topicBytes.Length); Array.Copy(contentBytes, 0, body, 4 + topicBytes.Length, contentBytes.Length); session.SendBytes(pulishRouteCode, body); return(true); } catch (Exception) { return(false); } } else { return(false); } }
public void SaveFailureRecords(PubishInformaction pubish, IEnumerable <MessageSubscribeRecord> subscribes) { foreach (var item in subscribes) { var db = Connection.GetDatabase(); var key = GetFailureRecordsKey(item.SubscribeInformaction.SubscribeId, item.SubscribeInformaction.Topic); var json = JsonConvert.SerializeObject(pubish); db.ListRightPush(key, json); } }
public bool WebPubish(SubscribeInformaction subInfo, PubishInformaction pubInfo) { if (!subInfo.IsWebSiteSubscriber) { throw new Exception("Web推送不支持非Web应用!"); } try { var url = new Uri(new UriBuilder(subInfo.WebSiteURI).Uri, pulishWebRouteCode); var content = url.PostJson(pubInfo); return(true); } catch (Exception) { return(false); } }
public void RemotePublish(PubishInformaction info) { List <Task> tasks = new List <Task>(); lock (EventAggregator.handlers) { var topicHandlers = EventAggregator.handlers.Where(o => o.Topic == info.Topic).ToList(); foreach (var item in topicHandlers) { tasks.Add(Task.Factory.StartNew((o) => { item.Handle((PubishInformaction)o); }, info)); } } //Task.WaitAll(tasks.ToArray()); }
/// <summary> /// 处理接收到的推送消息,并推送给当前代理的订阅方 /// </summary> /// <param name="info"></param> public void ReceiveMessage(PubishInformaction info) { IEnumerable <MessageSubscribeRecord> topicSubscribers = Server.MessageStore.GetSubscribes(info.Topic); if (topicSubscribers == null || topicSubscribers.Count() == 0) { return; } List <MessageSubscribeRecord> errorPubishRecords = new List <MessageSubscribeRecord>(); var isSuccess = false; foreach (var item in topicSubscribers) { try { //优先进行socket推送 isSuccess = SocketPubish(item.SubscribeInformaction, info, item.CurrentSessionId); //使用Socket推送,如果失败检查是否支持使用Web推送 if (!isSuccess && item.SubscribeInformaction.IsWebSiteSubscriber) { isSuccess = WebPubish(item.SubscribeInformaction, info); } } catch (Exception) { isSuccess = false; } if (!isSuccess) { errorPubishRecords.Add(item); } } Server.MessageStore.SaveFailureRecords(info, errorPubishRecords); }
public void Pubish(PubishInformaction info) { ISubscriber sub = Connection.GetSubscriber(); sub.Publish(info.Topic, info.Content); }
public void Pubish(PubishInformaction info) { Server.MessageAgent.ReceiveMessage(info); }
/// <summary> /// 推送给消息队列 /// </summary> /// <param name="info"></param> public void Pubish(PubishInformaction info) { Server.MessageQueue.Pubish(info); }
public void Handle(PubishInformaction evnt) { var eventArgs = JsonConvert.DeserializeObject <TEvent>(evnt.Content); Handle(eventArgs); }