public void LeaveChat(int chatId, int profileId) { if (chatController.LeaveChat(chatId, profileId)) { Chat chat = chatController.FindChat(chatId); if (chat != null) { List <Tuple <object, int, string> > callbacks = new List <Tuple <object, int, string> >(); List <Profile> profiles = new List <Profile>(); foreach (var tuple in chat.Users) { profiles.Add(tuple.Item1); if (tuple.Item2 != null) { callbacks.Add(new Tuple <object, int, string>(tuple.Item2, tuple.Item1.ProfileID, tuple.Item3)); } } foreach (Tuple <object, int, string> messageCallBack in callbacks) { try { IMessageCallBack callback = (IMessageCallBack)messageCallBack.Item1; callback.GetOnlineProfiles(profiles, messageCallBack.Item3); } catch (Exception) { chatController.LeaveChat(chatId, messageCallBack.Item2); } } } } }
private static IMessageCallBack GetCallBack(string callBackTypeName) { IMessageCallBack callBack = null; var parts = callBackTypeName.Split(','); var assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName.Equals(parts[0])); if (assembly == null) { assembly = Assembly.Load(parts[0]); } var callBackType = assembly.GetType(parts[1]); if (callBackType == null) { return(null); } { callBack = (IMessageCallBack)Activator.CreateInstance(callBackType); } return(callBack); }
/// <summary> /// 删除订阅者 /// </summary> /// <param name="clientId"></param> public static void RemoveSubscriber(string clientId) { Logger.InfoFormat("取消订阅 [{0}]", clientId); if (_Subscribers.ContainsKey(clientId)) { IMessageCallBack outObj = null; _Subscribers.TryRemove(clientId, out outObj); } }
/// <summary> /// 删除订阅者 /// </summary> /// <param name="callBack"></param> public static void RemoveSubscriber(IMessageCallBack callBack) { _Subscribers.ToList().ForEach(subscribe => { if (subscribe.Value == callBack) { Logger.InfoFormat("[{0}], 连接关闭", subscribe.Key); RemoveSubscriber(subscribe.Key); } }); }
public void Writing(int chatId) { foreach (var tuple in chatController.FindChat(chatId).Users) { try { IMessageCallBack callback = (IMessageCallBack)tuple.Item2; callback.WritingMessage(tuple.Item3); } catch (Exception) { chatController.LeaveChat(chatId, tuple.Item1.ProfileID); } } }
public void DeleteMessage(int profileId, int id, int chatId) { if (messageController.DeleteMessage(profileId, id)) { foreach (var tuple in chatController.FindChat(chatId).Users) { try { IMessageCallBack callback = (IMessageCallBack)tuple.Item2; callback.RemoveMessage(id, tuple.Item3); } catch (Exception) { chatController.LeaveChat(chatId, tuple.Item1.ProfileID); } } } }
/// <summary> /// 添加订阅者 /// </summary> /// <param name="clientId"></param> /// <param name="callBack"></param> public static void AddSubscriber(string clientId, IMessageCallBack callBack) { string ipAddress = string.Empty; try { //获取传进的消息属性 var properties = OperationContext.Current.IncomingMessageProperties; //获取消息发送的远程终结点IP和端口 var endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty; ipAddress = endpoint.Address; } catch (Exception ex) { Logger.Error("获取订阅客户端IP出错", ex); } Logger.InfoFormat("添加订阅 [{0}], IPAddress [{1}]", clientId, ipAddress); _Subscribers[clientId] = callBack; }
public CallResult Do() { IMessageCallBack callBack = OperationContext.Current.GetCallbackChannel <IMessageCallBack>(); callBack.OnCallbackMessage("开始运行"); callBack.OnCallback(); for (int i = 0; i < 100; i++) { callBack.OnCallbackMessage($"{i}/100"); Thread.Sleep(50); } callBack.OnCallbackMessage("运行结束"); return(CallResult.Ok()); }
public void CreateMessage(int profileId, string text, int chatId) { Message message = messageController.CreateMessage(profileId, text, chatId); if (message != null) { foreach (var tuple in chatController.FindChat(chatId).Users) { try { IMessageCallBack callback = (IMessageCallBack)tuple.Item2; callback.AddMessage(message, tuple.Item3); } catch (Exception) { chatController.LeaveChat(chatId, tuple.Item1.ProfileID); } } } }
public void JoinChat(int chatId, int profileId, string clientId) { object callbackObj = OperationContext.Current.GetCallbackChannel <IMessageCallBack>(); IMessageCallBack callback = (IMessageCallBack)callbackObj; if (chatController.JoinChat(chatId, profileId, callbackObj, clientId)) { Chat chat = chatController.FindChat(chatId); List <Tuple <object, int, string> > callbacks = new List <Tuple <object, int, string> >(); List <Profile> profiles = new List <Profile>(); foreach (var tuple in chat.Users) { profiles.Add(tuple.Item1); if (tuple.Item2 != null) { callbacks.Add(new Tuple <object, int, string>(tuple.Item2, tuple.Item1.ProfileID, tuple.Item3)); } } callback.GetChat(chat, clientId); callback.GetMessages(messageController.GetMessages(chatId), clientId); callback.GetOnlineProfiles(profiles, clientId); callback.Show(true, clientId); foreach (Tuple <object, int, string> messageCallBack in callbacks) { try { callback = (IMessageCallBack)messageCallBack.Item1; callback.GetOnlineProfiles(profiles, clientId); } catch (Exception) { chatController.LeaveChat(chatId, messageCallBack.Item2); } } } else { callback.Show(false, clientId); } }
public void SaveChat(int profileId, Chat chat) { if (chatController.SaveChat(profileId, chat) && chat.ActivityId != 0) { chat = chatController.FindChat(chat.ActivityId); if (chat != null) { foreach (var tuple in chat.Users) { try { IMessageCallBack callback = (IMessageCallBack)tuple.Item2; callback.GetChat(chat, tuple.Item3); } catch (Exception) { Offline(tuple.Item1.ProfileID); } } } } }
public void DeleteChat(int profileId, int id) { Chat chat = chatController.FindChat(id); if (chatController.DeleteChat(profileId, id)) { if (chat != null) { foreach (var tuple in chat.Users) { try { IMessageCallBack callback = (IMessageCallBack)tuple.Item2; callback.Show(false, tuple.Item3); } catch (Exception) { Offline(tuple.Item1.ProfileID); } } } } }
public void InviteToChat(int chatId, string name) { IMessageCallBack callback = OperationContext.Current.GetCallbackChannel <IMessageCallBack>(); callback.Invite(new ChatService().Invite(chatId, name)); }
public async Task <ExecutionResult <EventSubscription> > Subscribe(string channelName, IMessageCallBack callBackAction) { if (_diagnosticSource.IsEnabled("Subscribe")) { _diagnosticSource.Write("Subscribe", channelName); } var getChannelResult = await _eventChannelRepository.GetChannelAsync(s => s.Name == channelName); if (!getChannelResult.IsSuccessful) { return(BadResult <EventSubscription>()); } var channel = getChannelResult.Result; if (channel == null) { return(BadResult <EventSubscription>()); } var eventSubscriptionResult = await _eventSubscriptionService.SubscribeAsync(new EventSubscription { Channel = channel, Key = Guid.NewGuid().ToString(), CallBackType = $"{callBackAction.GetType().Assembly.FullName},{callBackAction.GetType().FullName}" }); if (!eventSubscriptionResult.IsSuccessful) { return(BadResult <EventSubscription>()); } var getEventMessageResult = await _eventMessageService.GetMessageBySubscriberAsync(eventSubscriptionResult.Result); if (!getEventMessageResult.IsSuccessful) { return(BadResult <EventSubscription>()); } if (getEventMessageResult.Result != null) { callBackAction.Invoke(getEventMessageResult.Result).Wait(); } CallBacks.TryAdd(eventSubscriptionResult.Result.Key, callBackAction); return(new ExecutionResult <EventSubscription>(true, eventSubscriptionResult.Result)); }