Advertisement
nicholasterry

sample ws code

Oct 10th, 2014
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using SuperSocket.SocketBase;
  6.  
  7. namespace SuperWebSocket.Samples.BasicConsole
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.WriteLine("Press any key to start the WebSocketServer!");
  14.  
  15.             Console.ReadKey();
  16.             Console.WriteLine();
  17.  
  18.             var appServer = new WebSocketServer();
  19.  
  20.             //Setup the appServer
  21.             if (!appServer.Setup(2012)) //Setup with listening port
  22.             {
  23.                 Console.WriteLine("Failed to setup!");
  24.                 Console.ReadKey();
  25.                 return;
  26.             }
  27.  
  28.             appServer.NewMessageReceived += new SessionHandler<WebSocketSession, string>(appServer_NewMessageReceived);
  29.  
  30.             Console.WriteLine();
  31.  
  32.             //Try to start the appServer
  33.             if (!appServer.Start())
  34.             {
  35.                 Console.WriteLine("Failed to start!");
  36.                 Console.ReadKey();
  37.                 return;
  38.             }
  39.  
  40.             Console.WriteLine("The server started successfully, press key 'q' to stop it!");
  41.  
  42.             while (Console.ReadKey().KeyChar != 'q')
  43.             {
  44.                 Console.WriteLine();
  45.                 continue;
  46.             }
  47.  
  48.             //Stop the appServer
  49.             appServer.Stop();
  50.  
  51.             Console.WriteLine();
  52.             Console.WriteLine("The server was stopped!");
  53.             Console.ReadKey();
  54.         }
  55.  
  56.         static void appServer_NewMessageReceived(WebSocketSession session, string message)
  57.         {
  58.             //Send the received message back
  59.             session.Send("Server: " + message);
  60.         }
  61.     }
  62. }
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81. using System;
  82. using System.Collections.Generic;
  83. using System.Linq;
  84. using System.Text;
  85. using WebSocket4Net;
  86. using System.Threading.Tasks;
  87. using System.Threading;
  88.  
  89. namespace SuperWebSocket.PerformanceTest.TestClient
  90. {
  91.     class Program
  92.     {
  93.         private static JsonWebSocket[] m_WebSockets;
  94.  
  95.         private static Semaphore m_Semaphore;
  96.  
  97.         private static volatile bool m_Stopped = false;
  98.  
  99.         private static long m_Sent = 0;
  100.  
  101.         private static long m_Received = 0;
  102.  
  103.         private static int m_ConnectedClients = 0;
  104.  
  105.         private static Timer m_PrintTimer;
  106.  
  107.         static void Main(string[] args)
  108.         {
  109.             int clientCount = 100;
  110.             m_WebSockets = new JsonWebSocket[clientCount];
  111.             m_Semaphore = new Semaphore(0, clientCount);
  112.  
  113.             m_PrintTimer = new Timer(OnPrintTimerCallback, null, 1000 * 5, 1000 * 5);
  114.  
  115.             int group = 100;
  116.  
  117.             for (var i = 0; i < clientCount; i += group)
  118.             {
  119.                 int to = Math.Min(i + group, clientCount);
  120.  
  121.                 Parallel.For(i, to, (j) =>
  122.                 {
  123.                     m_WebSockets[j] = CreateWebSocket();
  124.                 });
  125.  
  126.                 Thread.Sleep(1000);
  127.             }
  128.  
  129.             Console.WriteLine("All clients have been created!");
  130.  
  131.             while (!Console.ReadLine().ToLower().Equals("q"))
  132.                 continue;
  133.  
  134.             m_Stopped = true;
  135.             m_PrintTimer.Change(Timeout.Infinite, Timeout.Infinite);
  136.  
  137.             int closedNumber = 0;
  138.  
  139.             while (closedNumber < clientCount)
  140.             {
  141.                 m_Semaphore.WaitOne();
  142.                 closedNumber++;
  143.             }
  144.  
  145.             Console.WriteLine("All clients quit!");
  146.             Console.ReadLine();
  147.         }
  148.  
  149.         static JsonWebSocket CreateWebSocket()
  150.         {
  151.             var websocket = new JsonWebSocket("ws://127.0.0.1:2011/");
  152.            
  153.             websocket.On<string>("ECHO", HandleEchoResponse);
  154.             websocket.Closed += new EventHandler(websocket_Closed);
  155.             websocket.Error += new EventHandler<SuperSocket.ClientEngine.ErrorEventArgs>(websocket_Error);
  156.             websocket.Opened += new EventHandler(websocket_Opened);
  157.             websocket.Open();
  158.             return websocket;
  159.         }
  160.  
  161.         static void websocket_Opened(object sender, EventArgs e)
  162.         {
  163.             Interlocked.Increment(ref m_ConnectedClients);
  164.             RunTest((JsonWebSocket)sender);
  165.         }
  166.  
  167.         static void websocket_Error(object sender, SuperSocket.ClientEngine.ErrorEventArgs e)
  168.         {
  169.             Console.WriteLine(e.Exception.Message + Environment.NewLine + e.Exception.StackTrace);
  170.         }
  171.  
  172.         static void OnPrintTimerCallback(object state)
  173.         {
  174.             Console.WriteLine("Connected: {0}, Sent: {1}, Received: {2}", m_ConnectedClients, m_Sent, m_Received);
  175.         }
  176.  
  177.         static void websocket_Closed(object sender, EventArgs e)
  178.         {
  179.             m_Semaphore.Release();
  180.         }
  181.  
  182.         private static void HandleEchoResponse(JsonWebSocket websocket, string content)
  183.         {
  184.             Interlocked.Increment(ref m_Received);
  185.  
  186.             if (m_Stopped)
  187.             {
  188.                 websocket.Close();
  189.                 return;
  190.             }
  191.  
  192.             //Thread.Sleep(10);
  193.             RunTest(websocket);
  194.         }
  195.  
  196.         private static void RunTest(JsonWebSocket websocket)
  197.         {
  198.             websocket.Send("ECHO", Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString() + Guid.NewGuid().ToString());
  199.             Interlocked.Increment(ref m_Sent);
  200.         }
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement