Graph database Analysis of the Steam Network
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.

136 lines
3.0 KiB

  1. package net.jrtechs.www.server.old;
  2. import org.java_websocket.WebSocket;
  3. import org.java_websocket.handshake.ClientHandshake;
  4. import org.java_websocket.server.WebSocketServer;
  5. import org.json.JSONObject;
  6. import java.net.InetSocketAddress;
  7. import java.util.HashSet;
  8. import java.util.Set;
  9. /**
  10. * Socket server which listens for clients
  11. *
  12. * @author Jeffery Russell 5-26-18
  13. */
  14. public class Server extends WebSocketServer
  15. {
  16. /** port to listen on **/
  17. private static int TCP_PORT = 4444;
  18. /** clients connected to the server **/
  19. private Set<Client> clients;
  20. /**
  21. * Initializes the server and creates an empty set of clients
  22. */
  23. public Server()
  24. {
  25. super(new InetSocketAddress(TCP_PORT));
  26. System.out.println(super.getAddress());
  27. clients = new HashSet<>();
  28. }
  29. @Override
  30. public void onOpen(WebSocket conn, ClientHandshake handshake)
  31. {
  32. System.out.println("New connection from " +
  33. conn.getRemoteSocketAddress().getAddress().getHostAddress());
  34. }
  35. @Override
  36. public void onClose(WebSocket conn, int code, String reason, boolean remote)
  37. {
  38. this.removeClient(conn);
  39. System.out.println("Closed connection to " +
  40. conn.getRemoteSocketAddress().getAddress().getHostAddress());
  41. }
  42. @Override
  43. public void onMessage(WebSocket conn, String message)
  44. {
  45. JSONObject object = new JSONObject(message);
  46. if(object.has("graph"))
  47. {
  48. for (Client client : clients)
  49. {
  50. if(client.getSocket() == conn)
  51. {
  52. return;
  53. }
  54. }
  55. Client newClient = new Client(conn, object.getString("id"),
  56. object.getInt("graph"));
  57. clients.add(newClient);
  58. newClient.start();
  59. }
  60. else if(object.has("go"))
  61. {
  62. for(Client client: clients)
  63. {
  64. if(client.getSocket() == conn)
  65. {
  66. client.sendNextRequest();
  67. }
  68. }
  69. }
  70. }
  71. @Override
  72. public void onError(WebSocket conn, Exception ex)
  73. {
  74. ex.printStackTrace();
  75. if (conn != null)
  76. {
  77. clients.remove(conn);
  78. // do some thing if required
  79. }
  80. System.out.println("ERROR from " + conn.getRemoteSocketAddress()
  81. .getAddress().getHostAddress());
  82. }
  83. @Override
  84. public void onStart() {
  85. }
  86. /**
  87. * Removes a client from the main list of clients
  88. * based on the websocket that needs to be removed.
  89. *
  90. * @param conn
  91. */
  92. public void removeClient(WebSocket conn)
  93. {
  94. for(Client c: clients)
  95. {
  96. if(c.getSocket() == conn)
  97. {
  98. this.clients.remove(c);
  99. c.stop();
  100. }
  101. }
  102. }
  103. /**
  104. * Starts the web socket server
  105. *
  106. * @param args
  107. */
  108. public static void main(String[] args)
  109. {
  110. new Server().start();
  111. }
  112. }