Flek Web Socket test succesfull and fast


SUBMITTED BY: Guest

DATE: Nov. 24, 2014, 2:11 p.m.

FORMAT: Text only

SIZE: 1.9 kB

HITS: 807

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using Fleck;
  7. namespace FlekTest
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. FleckLog.Level = LogLevel.Debug;
  14. var allSockets = new List<IWebSocketConnection>();
  15. var server = new WebSocketServer("ws://0.0.0.0:509");
  16. server.Start(socket =>
  17. {
  18. socket.OnOpen = () =>
  19. {
  20. Console.WriteLine("Socket Opened");
  21. allSockets.Add(socket);
  22. };
  23. socket.OnClose = () =>
  24. {
  25. Console.WriteLine("Socket" + socket.ConnectionInfo + "Closed");
  26. allSockets.Remove(socket);
  27. };
  28. socket.OnMessage = message =>
  29. {
  30. Console.WriteLine(message);
  31. foreach(IWebSocketConnection sc in allSockets.Where(sc => socket.ConnectionInfo.ClientIpAddress != sc.ConnectionInfo.ClientIpAddress))
  32. {
  33. sc.Send(message);
  34. }
  35. };
  36. });
  37. string input = "";
  38. while (input != "q")
  39. {
  40. foreach (var socket in allSockets.ToList())
  41. {
  42. socket.Send(input);
  43. }
  44. input = Console.ReadLine();
  45. }
  46. server.Dispose();
  47. Console.WriteLine("Server Closed");
  48. Console.ReadKey();
  49. }
  50. }
  51. }

comments powered by Disqus