Developing the Web socket Server application

  1. Open the visual studio and create class library(Example myWsApp)
  2. Add the WsBase.dll as reference
  3. Create new class and drive from WsSession
  4. using WsBase;
    
    namespace TestSocket
    {
      
        public class WebChat : WsSession
        {
  5. Override the method OnTextMessageReceived(string message) to take the action on  text data received from client.
     
            protected override void OnTextMessageReceived(string message)
            {
                string clientMessage = message;
    
                //first data from the client
                if (firstTime == true)
                {
                    firstTime = false;
    
                    //first data should be userName;
                    if (clientMessage.StartsWith("UserName:"))
                    {
                        //get the user name
                        userName = clientMessage.Substring("userName:".Length);
    
                        //broadcast send
                        sender.Broadcast("users : " + GetActiveNames());
                        sender.Broadcast("message :" + userName + " join now");
                    }
                    else
                        sender.Send("messag : Invalid User");
                }
            }
    
    Use sender.Send() to send the data to client. sender, member of WsSession object, is WsResponse object responsible for sending reply to client.
  6. Override the OnPushData() method if you want to write push application.
     protected override void OnPushData()
            {
                sender.Send("Current time : " + System.DateTime.Now.ToString() + ", Number of Clients : " + application.GetNumberOfSession());
    
            }

Last edited Jun 15, 2012 at 2:29 AM by chandru0507, version 1

Comments

No comments yet.