title: Create websocket server to send/receive complex objects using SuperWebSocket
body: <p>I am new to websocket server and I'm sorry if my question looks non relevant or too easy for some of you.</p>
  
  <p>I am using Superwebsocket to create an websocket server in c#.
  I have setup all need to get the connection between browser and websocket server as per their sample that can be found <a href="https://github.com/kerryjiang/SuperWebSocket/tree/master/Samples/BasicConsole" rel="nofollow">here</a>:</p>
  
  <p>I have an console app as following that will serve as websocket server :</p>
  
  <pre><code> class Program
      {
          static void Main(string[] args)
          {
  
              var appServer = new WebSocketServer();
              //Setup the appServer
              if (!appServer.Setup(2012)) //Setup with listening port
              {
                  Console.WriteLine("Failed to setup!");
                  Console.ReadKey();
                  return;
              }
  
             appServer.NewMessageReceived += new SessionHandler&lt;WebSocketSession, string&gt;(appServer_NewMessageReceived);
             Console.WriteLine();
  
              //Try to start the appServer
              if (!appServer.Start())
              {
                  Console.WriteLine("Failed to start!");
                  Console.ReadKey();
                  return;
              }
  
              Console.WriteLine("The server started successfully, press key 'q' to stop it!");
  
              while (Console.ReadKey().KeyChar != 'q')
              {
                  Console.WriteLine();
                  continue;
              }
              appServer.Stop();
              Console.WriteLine();
              Console.WriteLine("The server was stopped!");
              Console.ReadKey();
          }
  
  
  
          static void appServer_NewMessageReceived(WebSocketSession session, string message)
          {
              //Send the received message back
              session.Send("Server: " + message);
          }
      }
  </code></pre>
  
  <p>and I have an sample html file that will serve as websocket server consumer, all java script code that servers to connect/disconnect to server is following:</p>
  
  <pre><code> &lt;script type="text/javascript"&gt;
          var ws;
  
          function connectToWebsocket() {
              ws = new WebSocket('ws://localhost:2012/');
              // when data is comming from the server, this metod is called
              ws.onmessage = function(evt) {
                 //mesage received
              };
  
              // when the connection is established, this method is called
              ws.onopen = function() {
                 // jQuery("#connectionStatus").html("Connected!");
              };
  
              // when the connection is closed, this method is called
              ws.onclose = function() {
                 // jQuery("#connectionStatus").html("Not Connected!");
              }
          }
  
          ///send message toolbarexample websocket server
          function sendMessage() {
              if (ws) {
                  ws.send("Some text");
              }
          }
  
          function disconnectWebSocket() {
              if (ws) {
                  ws.close();
              }
          }
      &lt;/script&gt;
  </code></pre>
  
  <p>Now everything is working and I have successful handshake , so I am sending and receiving string messages.</p>
  
  <p><strong>Now i have one big misunderstanding that I would like to understand:</strong></p>
  
  <p>Suppose I want to be able to do two commands into this websocket server, for example I would liek to have possibility to send string messages and another possibility to send an complex object so in case if string message is sent from javascript I want <code>appServer_NewMessageReceived</code> method to be fired on websocket server but in other case I want another method to be fired something like <code>complexObjectWasReceived(WebSocketSession session, Prsone(type of object) object)</code> and in same way I want to be able to send commands from websocket server to browser to to different methods not just to onMessage function.</p>
  
  <p>I can't find any good examples where it's possible to do something like that, all examples are to send/receive text messages, I will be very grateful if somebody can explain how I can achieve that or to provide some good links where I can find what I need.</p>
  
  <p><em>//Note:I want this websocket server to work locally for each user, and it will work as a bridge between browser and one device that will be installed on user machine that will read/write magnetic cards based on commands sent from browser.</em></p>
  
  <p>**UPDATE:**I have uploaded an sample solution that have all above code , you can find it <a href="https://www.dropbox.com/s/0e55uejp7e752p0/TestWebSocketServer.rar?dl=0" rel="nofollow">here</a>.</p>
  
  <p>Thanks all for reading and hope for your help. </p>