ChatChannel









How do I use Chat Channel
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: ChatGui.cs Copy
215     private void GuiSendsMsg()
216     {
217         if (string.IsNullOrEmpty(this.inputLine))
218         {
219
220             GUI.FocusControl("");
221             return;
222         }
223
224         if (this.inputLine[0].Equals('\\'))
225         {
226             string[] tokens = this.inputLine.Split(new char[] {' '}, 2);
227             if (tokens[0].Equals("\\help"))
228             {
229                 this.PostHelpToCurrentChannel();
230             }
231             if (tokens[0].Equals("\\state"))
232             {
233                 int newState = int.Parse(tokens[1]);
234                 this.chatClient.SetOnlineStatus(newState, new string[] { "i am state " + newState }); // this is how you set your own state and (any) message
235             }
236             else if (tokens[0].Equals("\\subscribe") && !string.IsNullOrEmpty(tokens[1]))
237             {
238                 this.chatClient.Subscribe(tokens[1].Split(new char[] {' ', ','}));
239             }
240             else if (tokens[0].Equals("\\unsubscribe") && !string.IsNullOrEmpty(tokens[1]))
241             {
242                 this.chatClient.Unsubscribe(tokens[1].Split(new char[] {' ', ','}));
243             }
244             else if (tokens[0].Equals("\\clear"))
245             {
246                 if (this.doingPrivateChat)
247                 {
248                     this.chatClient.PrivateChannels.Remove(this.selectedChannelName);
249                 }
250                 else
251                 {
252                     ChatChannel channel;
253                     if (this.chatClient.TryGetChannel(this.selectedChannelName, this.doingPrivateChat, out channel))
254                     {
255                         channel.ClearMessages();
256                     }
257                 }
258             }
259             else if (tokens[0].Equals("\\msg") && !string.IsNullOrEmpty(tokens[1]))
260             {
261                 string[] subtokens = tokens[1].Split(new char[] {' ', ','}, 2);
262                 string targetUser = subtokens[0];
263                 string message = subtokens[1];
264                 this.chatClient.SendPrivateMessage(targetUser, message);
265             }
266         }
267         else
268         {
269             if (this.doingPrivateChat)
270             {
271                 this.chatClient.SendPrivateMessage(this.userIdInput, this.inputLine);
272             }
273             else
274             {
275                 this.chatClient.PublishMessage(this.selectedChannelName, this.inputLine);
276             }
277         }
278
279         this.inputLine = "";
280         GUI.FocusControl("");
281     }
File name: ChatGui.cs Copy
283     private void PostHelpToCurrentChannel()
284     {
285         ChatChannel channelForHelp = this.selectedChannel;
286         if (channelForHelp != null)
287         {
288             channelForHelp.Add("info", ChatGui.HelpText);
289         }
290         else
291         {
292             Debug.LogError("no channel for help");
293         }
294     }
File name: ChatGui.cs Copy
348     public void OnStatusUpdate(string user, int status, bool gotMessage, object message)
349     {
350         // this is how you get status updates of friends.
351         // this demo simply adds status updates to the currently shown chat.
352         // you could buffer them or use them any other way, too.
353
354         ChatChannel activeChannel = this.selectedChannel;
355         if (activeChannel != null)
356         {
357             activeChannel.Add("info", string.Format("{0} is {1}. Msg:{2}", user, status, message));
358         }
359
360         Debug.LogWarning("status: " + string.Format("{0} is {1}. Msg:{2}", user, status, message));
361     }
File name: ChatChannel.cs Copy
40         public ChatChannel(string name)
41         {
42             this.Name = name;
43         }
File name: ChatClient.cs Copy
80         public ChatClient(IChatClientListener listener)
81         {
82             this.listener = listener;
83             this.State = ChatState.Uninitialized;
84
85             this.PublicChannels = new Dictionary();
86             this.PrivateChannels = new Dictionary();
87         }
File name: ChatClient.cs Copy
534         public bool TryGetChannel(string channelName, bool isPrivate, out ChatChannel channel)
535         {
536             if (!isPrivate)
537             {
538                return this.PublicChannels.TryGetValue(channelName, out channel);
539             }
540             else
541             {
542                 return this.PrivateChannels.TryGetValue(channelName, out channel);
543             }
544         }
File name: ChatClient.cs Copy
688         private void HandlePrivateMessageEvent(EventData eventData)
689         {
690             //Console.WriteLine(SupportClass.DictionaryToString(eventData.Parameters));
691
692             var message = (object)eventData.Parameters[(byte)ChatParameterCode.Message];
693             var sender = (string)eventData.Parameters[(byte)ChatParameterCode.Sender];
694
695             string channelName;
696             if (this.UserId != null && this.UserId.Equals(sender))
697             {
698                 var target = (string)eventData.Parameters[(byte)ChatParameterCode.UserId];
699                 channelName = this.GetPrivateChannelNameByUser(target);
700             }
701             else
702             {
703                 channelName = this.GetPrivateChannelNameByUser(sender);
704             }
705
706             ChatChannel channel;
707             if (!this.PrivateChannels.TryGetValue(channelName, out channel))
708             {
709                 channel = new ChatChannel(channelName);
710                 channel.IsPrivate = true;
711                 this.PrivateChannels.Add(channel.Name, channel);
712             }
713
714             channel.Add(sender, message);
715             this.listener.OnPrivateMessage(sender, message, channelName);
716         }
File name: ChatClient.cs Copy
718         private void HandleChatMessagesEvent(EventData eventData)
719         {
720             var messages = (object[])eventData.Parameters[(byte)ChatParameterCode.Messages];
721             var senders = (string[])eventData.Parameters[(byte)ChatParameterCode.Senders];
722             var channelName = (string)eventData.Parameters[(byte)ChatParameterCode.Channel];
723
724             ChatChannel channel;
725             if (!this.PublicChannels.TryGetValue(channelName, out channel))
726             {
727                 // TODO: log that channel wasn't found
728                 return;
729             }
730
731             channel.Add(senders, messages);
732             this.listener.OnGetMessages(channelName, senders, messages);
733         }
File name: ChatClient.cs Copy
735         private void HandleSubscribeEvent(EventData eventData)
736         {
737             var channelsInResponse = (string[])eventData.Parameters[ChatParameterCode.Channels];
738             var results = (bool[])eventData.Parameters[ChatParameterCode.SubscribeResults];
739
740             for (int i = 0; i < channelsInResponse.Length; i++)
741             {
742                 if (results[i])
743                 {
744                     string channelName = channelsInResponse[i];
745                     if (!this.PublicChannels.ContainsKey(channelName))
746                     {
747                         ChatChannel channel = new ChatChannel(channelName);
748                         this.PublicChannels.Add(channel.Name, channel);
749                     }
750                 }
751             }
752
753             this.listener.OnSubscribed(channelsInResponse, results);
754         }

Download file with original file name:ChatChannel

ChatChannel 96 lượt xem

Gõ tìm kiếm nhanh...