Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. namespace ConsoleApp2 {
  2.  
  3. class ServerV200 {
  4.  
  5. List<IWebSocketConnection> allSockets;
  6. Guid id;
  7. Thread thread;
  8.  
  9. static void Main(string[] args) {
  10. new ServerV200().socket();
  11. }
  12.  
  13.  
  14.  
  15. public void socket() {
  16.  
  17. allSockets = new List<IWebSocketConnection>();
  18. var server = new WebSocketServer("ws://0.0.0.0:2208");
  19. server.Start(socket => {
  20. socket.OnOpen = () => {
  21. Console.WriteLine("OnOpen()");
  22. allSockets.Add(socket);
  23. id = socket.ConnectionInfo.Id;
  24. thread = new Thread(delegate () { WriteY(true, id); });
  25. thread.Start();
  26. };
  27. socket.OnClose = () => {
  28. Console.WriteLine("OnClose()");
  29. //thread.Abort(true);
  30. //thread.Join();
  31. WriteY(false, id);
  32. //Console.WriteLine("OnClose() thread: " + thread);
  33. allSockets.Remove(socket);
  34. };
  35. socket.OnMessage = message => {
  36. Console.WriteLine(message);
  37. allSockets.ToList().ForEach(s => s.Send("Echo: " + message));
  38. };
  39. });
  40.  
  41.  
  42. var input = Console.ReadLine();
  43. while (input != "exit") {
  44. foreach (var socket in allSockets.ToList()) {
  45. socket.Send(input);
  46. }
  47. input = Console.ReadLine();
  48. }
  49.  
  50. }
  51.  
  52. void SendToSocketById(String input, Guid id) {
  53. Console.WriteLine("input "+input+" "+id);
  54. var socket = allSockets.Find(client => client.ConnectionInfo.Id == id);
  55. socket.Send(input);
  56. }
  57.  
  58. public void WriteY(bool action, Guid id) {
  59. int i = 0;
  60. while (action) {
  61. SendToSocketById(""+i, id);
  62. i++;
  63. Thread.Sleep(2000);
  64. }
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement