Example of Public Component in .NET

This section contains an extended example of implementation of the public component of the "Chat" module using the .NET language. This example includes implementation of the feature of getting information about online users in service groups.

For working of this code, one should add the reference to the "System.Web.Extensions" library at the Visual Studio project.

Comments to the Example

The following line of the example contains the address where requests are sent:

TeamWoxChatGroups groups = new TeamWoxChatGroups("http://your.domain.com/");

You should change the "http://your.domain.com/" address to the address of your own TeamWox system. The internal TeamWox page that is requested in the code is specified in the following line:

m_TeamwoxURLGetChatGroup = serverHost + "public/chat/groups?json";

The /public/chat/group page is called, and the result is returned in the JSON format. That is the page from which the list of available groups and their online/offline status are requested.

Full Code

The full code of implementation of the public component is given below:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Web.Script.Serialization;
//---
namespace SendRequestChatOnline
  {
  /// <summary>
  /// Information about each group containing its status and name
  /// </summary>
  class ChatGroup
    {
    public string ID { get; set; }
    public bool OnLine { get; set; }
    public string Name { get; set; }
    }
  /// <summary>
  /// The class for obtaining information about all groups and their statuses
  /// </summary>
  class StatusGroups
    {
    public int Config { get; set; }
    public List<ChatGroup> Groups { get; set; }
    }
  /// <summary>
  /// Conversion of data from json string to the StatusGroups object
  /// </summary>
  public class StatusGroupConverter : JavaScriptConverter
    {
    private static readonly Type[] m_SupportedTypes = new[] { typeof(StatusGroups) };
    public override IEnumerable<Type> SupportedTypes { get { return m_SupportedTypes; } }
    /// <summary>
    /// Desearilization of obtained data into the necessary object
    /// </summary>
    /// <param name="dictionary">obtained data</param>
    /// <param name="type">data type</param>
    /// <param name="serializer">class of serializer</param>
    /// <returns>filled class StatusGroups or null</returns>
    public override object Deserialize(IDictionary<string,object> dictionary,
                                        Type type,
                                        JavaScriptSerializer serializer)
      {
      StatusGroups obj = new StatusGroups
                           {
                             Config = (int)dictionary["config"],                         //--- id
                             Groups = ParsingGroups(dictionary["groups"] as ArrayList)   //--- list of available groups
                           };
      //---
      return obj;
      }
    /// <summary>
    /// Parser for groups
    /// </summary>
    /// <param name="listGroups">list of grouos</param>
    /// <returns></returns>
    private static List<ChatGroup> ParsingGroups(ArrayList listGroups)
      {
      List<ChatGroup> resultItems = new List<ChatGroup>();
      //---
      foreach(IDictionary<string,object> dictionary in listGroups)
        {
        if(dictionary == null)
          continue;
        //---
        resultItems.Add(new ChatGroup
                       {
                         ID = (string)dictionary["id"],           //--- id of group
                         Name = (string)dictionary["name"],       //--- name of group
                         OnLine = (int)dictionary["online"] == 1  //--- status of group
                       });
        }
      //---
      return resultItems;
      }
    /// <summary>
    /// Stopper of serialization. Not used
    /// </summary>
    public override IDictionary<string,object> Serialize(object obj,JavaScriptSerializer serializer)
      {
      return null;
      }
    }
  /// <summary>
  /// The class for working with information about groups and their statues
  /// </summary>
  class TeamWoxChatGroups
    {
    private readonly string m_TeamwoxURLGetChatGroup;
    public TeamWoxChatGroups(string serverHost)
      {
      m_TeamwoxURLGetChatGroup = serverHost + "public/chat/groups?json";
      }
    /// <summary>
    /// Sending of http request
    /// </summary>
    private static string OpenUrlGet(string url)
      {
      HttpWebRequest request =  (HttpWebRequest)WebRequest.Create(url);
      request.Method = "GET";
      request.Headers.Add("UserAgent","Web-site");
      //--- fill information to be sent to the TeamWox server
      request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
      request.AllowAutoRedirect = false;
      request.KeepAlive = false;
      //--- make request
      HttpWebResponse response = (HttpWebResponse)request.GetResponse();
      Stream responseStream = response.GetResponseStream();
      StreamReader reader = new StreamReader(responseStream);
      string result = reader.ReadToEnd();
      reader.Dispose();
      //---
      response.Close();
      responseStream.Dispose();
      //---
      return result;
      }
    /// <summary>
    /// Getting data from json
    /// </summary>
    /// <param name="json">string in the json fromat</param>
    /// <returns>the object with information about groups</returns>
    private static StatusGroups ParsingStringJson(string json)
      {
      JavaScriptSerializer serializer = new JavaScriptSerializer();
      serializer.RegisterConverters(new List<JavaScriptConverter> { new StatusGroupConverter() });
      return serializer.Deserialize<StatusGroups>(json);
      }
    /// <summary>
    /// Getting information about all groups and their statuses
    /// </summary>
    /// <returns></returns>
    public StatusGroups QueryGetChatGroup()
      {
      string stringJson = OpenUrlGet(m_TeamwoxURLGetChatGroup);
      //---
      return ParsingStringJson(stringJson);
      }
    }
  /// <summary>
  /// The main class for calling of console application
  /// </summary>
  class Program
    {
    static void Main()
      {
      //--- creation of the main class
      TeamWoxChatGroups groups = new TeamWoxChatGroups("http://your.domain.com/");
      //--- getting information about groups
      StatusGroups statusGroups = groups.QueryGetChatGroup();
      //--- displaying of information about groups
      if(statusGroups != null && statusGroups.Groups != null)
        {
        foreach(ChatGroup chatGroup in statusGroups.Groups)
          {
          Console.WriteLine(chatGroup.Name + " " + (chatGroup.OnLine ? " On line" : "Off line"));
          }
        }
      Console.ReadLine();
      }
    }
  }