You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.0 KiB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
  1. #if NOTIFY
  2. using Notifications;
  3. #endif
  4. using System;
  5. using System.Collections;
  6. using System.Threading;
  7. using WebSocketSharp;
  8. namespace Example
  9. {
  10. public struct NfMessage
  11. {
  12. public string Summary;
  13. public string Body;
  14. public string Icon;
  15. }
  16. public class ThreadState
  17. {
  18. public bool Enabled { get; set; }
  19. public AutoResetEvent Notification { get; private set; }
  20. public ThreadState()
  21. {
  22. Enabled = true;
  23. Notification = new AutoResetEvent(false);
  24. }
  25. }
  26. public class Program
  27. {
  28. private static Queue _msgQ = Queue.Synchronized(new Queue());
  29. private static void enNfMessage(string summary, string body, string icon)
  30. {
  31. var msg = new NfMessage
  32. {
  33. Summary = summary,
  34. Body = body,
  35. Icon = icon
  36. };
  37. _msgQ.Enqueue(msg);
  38. }
  39. public static void Main(string[] args)
  40. {
  41. ThreadState ts = new ThreadState();
  42. WaitCallback notifyMsg = state =>
  43. {
  44. while (ts.Enabled || _msgQ.Count > 0)
  45. {
  46. Thread.Sleep(500);
  47. if (_msgQ.Count > 0)
  48. {
  49. NfMessage msg = (NfMessage)_msgQ.Dequeue();
  50. #if NOTIFY
  51. Notification nf = new Notification(msg.Summary,
  52. msg.Body,
  53. msg.Icon);
  54. nf.AddHint("append", "allowed");
  55. nf.Show();
  56. #else
  57. Console.WriteLine("{0}: {1}", msg.Summary, msg.Body);
  58. #endif
  59. }
  60. }
  61. ts.Notification.Set();
  62. };
  63. ThreadPool.QueueUserWorkItem(notifyMsg);
  64. //using (WebSocket ws = new WebSocket("ws://echo.websocket.org", "echo"))
  65. //using (WebSocket ws = new WebSocket("wss://echo.websocket.org", "echo"))
  66. using (WebSocket ws = new WebSocket("ws://localhost:4649"))
  67. {
  68. ws.OnOpen += (sender, e) =>
  69. {
  70. ws.Send("Hi, all!");
  71. };
  72. ws.OnMessage += (sender, e) =>
  73. {
  74. if (!String.IsNullOrEmpty(e.Data))
  75. {
  76. enNfMessage("[WebSocket] Message", e.Data, "notification-message-im");
  77. }
  78. };
  79. ws.OnError += (sender, e) =>
  80. {
  81. enNfMessage("[WebSocket] Error", e.Message, "notification-message-im");
  82. };
  83. ws.OnClose += (sender, e) =>
  84. {
  85. enNfMessage(
  86. String.Format("[WebSocket] Close({0}:{1})", (ushort)e.Code, e.Code),
  87. e.Reason,
  88. "notification-message-im");
  89. };
  90. ws.Connect();
  91. Thread.Sleep(500);
  92. Console.WriteLine("\nType \"exit\" to exit.\n");
  93. string data;
  94. while (true)
  95. {
  96. Thread.Sleep(500);
  97. Console.Write("> ");
  98. data = Console.ReadLine();
  99. if (data == "exit")
  100. //if (data == "exit" || !ws.IsAlive)
  101. {
  102. break;
  103. }
  104. ws.Send(data);
  105. }
  106. }
  107. ts.Enabled = false;
  108. ts.Notification.WaitOne();
  109. }
  110. }
  111. }