diff --git a/src/main/java/net/jrtechs/www/client/index.html b/src/main/java/net/jrtechs/www/client/index.html new file mode 100644 index 0000000..dbf4942 --- /dev/null +++ b/src/main/java/net/jrtechs/www/client/index.html @@ -0,0 +1,26 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/net/jrtechs/www/server/Client.java b/src/main/java/net/jrtechs/www/server/Client.java new file mode 100644 index 0000000..c62f7dc --- /dev/null +++ b/src/main/java/net/jrtechs/www/server/Client.java @@ -0,0 +1,31 @@ +package net.jrtechs.www.server; + +import org.java_websocket.WebSocket; + +public class Client extends Thread +{ + + private WebSocket client; + + + + public Client(WebSocket client) + { + this.client = client; + } + + public void recievedMessage(String message) + { + + } + + public WebSocket getSocket() + { + return this.client; + } + + @Override + public void run() { + + } +} diff --git a/src/main/java/net/jrtechs/www/server/Server.java b/src/main/java/net/jrtechs/www/server/Server.java new file mode 100644 index 0000000..d5067ab --- /dev/null +++ b/src/main/java/net/jrtechs/www/server/Server.java @@ -0,0 +1,109 @@ +package net.jrtechs.www.server; + +import org.java_websocket.WebSocket; +import org.java_websocket.handshake.ClientHandshake; +import org.java_websocket.server.WebSocketServer; + +import java.net.InetSocketAddress; +import java.util.HashSet; +import java.util.Set; + + +/** + * Socket server which listens for clients + * + * @author Jeffery Russell 5-26-18 + */ +public class Server extends WebSocketServer +{ + /** port to listen on **/ + private static int TCP_PORT = 4444; + + /** clients connected to the server **/ + private Set clients; + + + /** + * Initializes the server and creates an empty set of clients + */ + public Server() + { + super(new InetSocketAddress(TCP_PORT)); + clients = new HashSet<>(); + } + + + @Override + public void onOpen(WebSocket conn, ClientHandshake handshake) + { + clients.add(new Client(conn)); + System.out.println("New connection from " + + conn.getRemoteSocketAddress().getAddress().getHostAddress()); + } + + @Override + public void onClose(WebSocket conn, int code, String reason, boolean remote) + { + this.removeClient(conn); + + System.out.println("Closed connection to " + + conn.getRemoteSocketAddress().getAddress().getHostAddress()); + } + + @Override + public void onMessage(WebSocket conn, String message) + { + System.out.println("Message from client: " + message); + for (Client client : clients) + { + if(client.getSocket() == conn) + { + client.recievedMessage(message); + } + } + } + + @Override + public void onError(WebSocket conn, Exception ex) + { + //ex.printStackTrace(); + if (conn != null) + { + clients.remove(conn); + // do some thing if required + } + System.out.println("ERROR from " + conn.getRemoteSocketAddress() + .getAddress().getHostAddress()); + } + + + /** + * Removes a client from the main list of clients + * based on the websocket that needs to be removed. + * + * @param conn + */ + public void removeClient(WebSocket conn) + { + for(Client c: clients) + { + if(c.getSocket() == conn) + { + this.clients.remove(c); + c.stop(); + } + } + } + + + /** + * Starts the web socket server + * + * @param args + */ + public static void main(String[] args) + { + new Server().start(); + } + +}