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.

51 lines
1019 B

12 years ago
12 years ago
  1. using System;
  2. using System.Threading;
  3. using WebSocketSharp;
  4. using WebSocketSharp.Server;
  5. namespace Example2
  6. {
  7. public class Chat : WebSocketService
  8. {
  9. private static int _num = 0;
  10. private string _name;
  11. private string _prefix;
  12. public Chat ()
  13. : this (null)
  14. {
  15. }
  16. public Chat (string prefix)
  17. {
  18. _prefix = !prefix.IsNullOrEmpty () ? prefix : "anon#";
  19. }
  20. private string getName ()
  21. {
  22. var name = Context.QueryString ["name"];
  23. return !name.IsNullOrEmpty () ? name : (_prefix + getNum ());
  24. }
  25. private static int getNum ()
  26. {
  27. return Interlocked.Increment (ref _num);
  28. }
  29. protected override void OnOpen ()
  30. {
  31. _name = getName ();
  32. }
  33. protected override void OnMessage (MessageEventArgs e)
  34. {
  35. Sessions.Broadcast (String.Format ("{0}: {1}", _name, e.Data));
  36. }
  37. protected override void OnClose (CloseEventArgs e)
  38. {
  39. Sessions.Broadcast (String.Format ("{0} got logged off...", _name));
  40. }
  41. }
  42. }