Sunday, October 12, 2014

ChatSerive



WEB SERVICE CHAT APPLICATION

  • ChatService

public class ClassObj
{
    public int ID { get; set; }
    public String Message { get; set; }
}

public static class ListObj
{
    public static List<ClassObj> _listObj = new List<ClassObj>();   
}


[WebMethod]
    public List<ClassObj> Chat( int _id ,String _Msg)
    {
        ClassObj obj = new ClassObj();
        obj.ID =_id;
        obj.Message = _Msg;

        ListObj._listObj.Add(obj);
        return ListObj._listObj;
    }

[WebMethod]
     public  void ChatClear()
    {
         ListObj._listObj.Clear();
    }



  • ChatClient





<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:TextBox ID="TextBox2" runat="server" Width="273px" placeholder="User ID"></asp:TextBox>
        <br />
   
        <asp:ListBox ID="ListBox1" runat="server" Height="152px" Width="284px">
        </asp:ListBox>
        <br />
        <asp:TextBox ID="TextBox1" runat="server" Width="219px"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Chat" Width="56px" />
   
        <br />
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Clear Chat" />
   
    </div>
    </form>
</body>





public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Enter User ID');", true);          
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        ServiceReference1.ServiceSoapClient client = new ServiceSoapClient();
        ServiceReference1.ClassObj[] _classObj = client.Chat(Convert.ToInt32(TextBox2.Text), TextBox1.Text);
        String ss="";
        ListBox1.Items.Clear();
        for (int i = 0; i < _classObj.Count(); i++)
        {
            ListBox1.Items.Add(_classObj[i].ID.ToString());
            ListBox1.Items.Add(_classObj[i].Message.ToString());
        }
       
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        ServiceReference1.ServiceSoapClient client = new ServiceSoapClient();
        client.ChatClear();
        ListBox1.Items.Clear();

    }
}

No comments:

Post a Comment