ChatChannel.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // ----------------------------------------------------------------------------------------------------------------------
  2. // <summary>The Photon Chat Api enables clients to connect to a chat server and communicate with other clients.</summary>
  3. // <remarks>ChatClient is the main class of this api.</remarks>
  4. // <copyright company="Exit Games GmbH">Photon Chat Api - Copyright (C) 2014 Exit Games GmbH</copyright>
  5. // ----------------------------------------------------------------------------------------------------------------------
  6. #if UNITY_4_7 || UNITY_5 || UNITY_5_3_OR_NEWER
  7. #define SUPPORTED_UNITY
  8. #endif
  9. namespace Photon.Chat
  10. {
  11. using System.Collections.Generic;
  12. using System.Text;
  13. #if SUPPORTED_UNITY || NETFX_CORE
  14. using Hashtable = ExitGames.Client.Photon.Hashtable;
  15. using SupportClass = ExitGames.Client.Photon.SupportClass;
  16. #endif
  17. /// <summary>
  18. /// A channel of communication in Photon Chat, updated by ChatClient and provided as READ ONLY.
  19. /// </summary>
  20. /// <remarks>
  21. /// Contains messages and senders to use (read!) and display by your GUI.
  22. /// Access these by:
  23. /// ChatClient.PublicChannels
  24. /// ChatClient.PrivateChannels
  25. /// </remarks>
  26. public class ChatChannel
  27. {
  28. /// <summary>Name of the channel (used to subscribe and unsubscribe).</summary>
  29. public readonly string Name;
  30. /// <summary>Senders of messages in chronological order. Senders and Messages refer to each other by index. Senders[x] is the sender of Messages[x].</summary>
  31. public readonly List<string> Senders = new List<string>();
  32. /// <summary>Messages in chronological order. Senders and Messages refer to each other by index. Senders[x] is the sender of Messages[x].</summary>
  33. public readonly List<object> Messages = new List<object>();
  34. /// <summary>If greater than 0, this channel will limit the number of messages, that it caches locally.</summary>
  35. public int MessageLimit;
  36. /// <summary>Unique channel ID.</summary>
  37. public int ChannelID;
  38. /// <summary>Is this a private 1:1 channel?</summary>
  39. public bool IsPrivate { get; protected internal set; }
  40. /// <summary>Count of messages this client still buffers/knows for this channel.</summary>
  41. public int MessageCount { get { return this.Messages.Count; } }
  42. /// <summary>
  43. /// ID of the last message received.
  44. /// </summary>
  45. public int LastMsgId { get; protected set; }
  46. private Dictionary<object, object> properties;
  47. /// <summary>Whether or not this channel keeps track of the list of its subscribers.</summary>
  48. public bool PublishSubscribers { get; protected set; }
  49. /// <summary>Maximum number of channel subscribers. 0 means infinite.</summary>
  50. public int MaxSubscribers { get; protected set; }
  51. /// <summary>Subscribed users.</summary>
  52. public readonly HashSet<string> Subscribers = new HashSet<string>();
  53. /// <summary>Used internally to create new channels. This does NOT create a channel on the server! Use ChatClient.Subscribe.</summary>
  54. public ChatChannel(string name)
  55. {
  56. this.Name = name;
  57. }
  58. /// <summary>Used internally to add messages to this channel.</summary>
  59. public void Add(string sender, object message, int msgId)
  60. {
  61. this.Senders.Add(sender);
  62. this.Messages.Add(message);
  63. this.LastMsgId = msgId;
  64. this.TruncateMessages();
  65. }
  66. /// <summary>Used internally to add messages to this channel.</summary>
  67. public void Add(string[] senders, object[] messages, int lastMsgId)
  68. {
  69. this.Senders.AddRange(senders);
  70. this.Messages.AddRange(messages);
  71. this.LastMsgId = lastMsgId;
  72. this.TruncateMessages();
  73. }
  74. /// <summary>Reduces the number of locally cached messages in this channel to the MessageLimit (if set).</summary>
  75. public void TruncateMessages()
  76. {
  77. if (this.MessageLimit <= 0 || this.Messages.Count <= this.MessageLimit)
  78. {
  79. return;
  80. }
  81. int excessCount = this.Messages.Count - this.MessageLimit;
  82. this.Senders.RemoveRange(0, excessCount);
  83. this.Messages.RemoveRange(0, excessCount);
  84. }
  85. /// <summary>Clear the local cache of messages currently stored. This frees memory but doesn't affect the server.</summary>
  86. public void ClearMessages()
  87. {
  88. this.Senders.Clear();
  89. this.Messages.Clear();
  90. }
  91. /// <summary>Provides a string-representation of all messages in this channel.</summary>
  92. /// <returns>All known messages in format "Sender: Message", line by line.</returns>
  93. public string ToStringMessages()
  94. {
  95. StringBuilder txt = new StringBuilder();
  96. for (int i = 0; i < this.Messages.Count; i++)
  97. {
  98. txt.AppendLine(string.Format("{0}: {1}", this.Senders[i], this.Messages[i]));
  99. }
  100. return txt.ToString();
  101. }
  102. internal void ReadChannelProperties(Dictionary<object, object> newProperties)
  103. {
  104. if (newProperties != null && newProperties.Count > 0)
  105. {
  106. if (this.properties == null)
  107. {
  108. this.properties = new Dictionary<object, object>(newProperties.Count);
  109. }
  110. foreach (var pair in newProperties)
  111. {
  112. if (pair.Value == null)
  113. {
  114. this.properties.Remove(pair.Key);
  115. }
  116. else
  117. {
  118. this.properties[pair.Key] = pair.Value;
  119. }
  120. }
  121. object temp;
  122. if (this.properties.TryGetValue(ChannelWellKnownProperties.PublishSubscribers, out temp))
  123. {
  124. this.PublishSubscribers = (bool)temp;
  125. }
  126. if (this.properties.TryGetValue(ChannelWellKnownProperties.MaxSubscribers, out temp))
  127. {
  128. this.MaxSubscribers = (int)temp;
  129. }
  130. }
  131. }
  132. internal void AddSubscribers(string[] users)
  133. {
  134. if (users == null)
  135. {
  136. return;
  137. }
  138. for (int i = 0; i < users.Length; i++)
  139. {
  140. this.Subscribers.Add(users[i]);
  141. }
  142. }
  143. #if CHAT_EXTENDED
  144. internal void ReadUserProperties(string userId, Dictionary<object, object> changedProperties)
  145. {
  146. throw new System.NotImplementedException();
  147. }
  148. internal bool TryGetChannelProperty<T>(object propertyKey, out T propertyValue)
  149. {
  150. propertyValue = default(T);
  151. object temp;
  152. if (properties != null && properties.TryGetValue(propertyKey, out temp) && temp is T)
  153. {
  154. propertyValue = (T)temp;
  155. return true;
  156. }
  157. return false;
  158. }
  159. public bool TryGetCustomChannelProperty<T>(string propertyKey, out T propertyValue)
  160. {
  161. return this.TryGetChannelProperty(propertyKey, out propertyValue);
  162. }
  163. #endif
  164. }
  165. }