From 7fe670c1d30b34e93c3c657c5f8960e85149e51c Mon Sep 17 00:00:00 2001 From: Jimmy Nguyen Date: Mon, 22 Apr 2024 19:10:43 -0500 Subject: [PATCH] Added other public app constructors --- src/main/java/uta/cse3310/App.java | 131 +++++++++++++++++++---------- 1 file changed, 86 insertions(+), 45 deletions(-) diff --git a/src/main/java/uta/cse3310/App.java b/src/main/java/uta/cse3310/App.java index f1d287b..40a27b6 100644 --- a/src/main/java/uta/cse3310/App.java +++ b/src/main/java/uta/cse3310/App.java @@ -36,39 +36,40 @@ * OTHER DEALINGS IN THE SOFTWARE. */ - package uta.cse3310; - - import java.io.BufferedReader; - import java.io.IOException; - import java.io.InputStreamReader; - import java.net.InetSocketAddress; - import java.net.UnknownHostException; - import java.nio.ByteBuffer; - import java.util.Collections; - - import org.java_websocket.WebSocket; - import org.java_websocket.drafts.Draft; - import org.java_websocket.drafts.Draft_6455; - import org.java_websocket.handshake.ClientHandshake; - import org.java_websocket.server.WebSocketServer; - import java.util.Timer; - import java.util.TimerTask; - import java.util.Vector; - import java.util.HashMap; - import java.util.Map; - import java.util.List; - import java.time.Instant; - import java.time.Duration; - - - import com.google.gson.Gson; - import com.google.gson.GsonBuilder; - - import java.util.ArrayList; - - public abstract class App extends WebSocketServer { - - private Vector activeGames; +package uta.cse3310; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.InetSocketAddress; +import java.net.UnknownHostException; +import java.nio.ByteBuffer; +import java.util.Collections; + +import org.java_websocket.WebSocket; +import org.java_websocket.drafts.Draft; +import org.java_websocket.drafts.Draft_6455; +import org.java_websocket.handshake.ClientHandshake; +import org.java_websocket.server.WebSocketServer; +import java.util.Timer; +import java.util.TimerTask; +import java.util.Vector; +import java.util.HashMap; +import java.util.Map; +import java.util.List; +import java.time.Instant; +import java.time.Duration; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import java.util.ArrayList; + +public class App extends WebSocketServer { + + public int games = 0; + int GameId = 1; + private Vector activeGames = new Vector(); private Map playerMap; private Map playerNickMap; @@ -79,6 +80,14 @@ public App(int webSocketPort) { this.playerNickMap = new HashMap<>(); } + public App(InetSocketAddress address) { + super(address); + } + + public App(int webSocketPort, Draft_6455 draft) { + super(new InetSocketAddress(webSocketPort), Collections.singletonList(draft)); + } + @Override public void onOpen(WebSocket conn, ClientHandshake handshake) { // Logic for handling websocket on open event @@ -94,6 +103,21 @@ public void onMessage(WebSocket conn, String message) { // Logic for handling websocket message event } + @Override + public void onError(WebSocket conn, Exception ex) { + ex.printStackTrace(); + if (conn != null) { + // some errors like port binding failed may not be assignable to a specific + // websocket + } + } + + @Override + public void onStart() { + System.out.println("Server started!"); + setConnectionLostTimeout(0); + } + public void updateLobby() { // Logic for updating the lobby } @@ -168,20 +192,37 @@ public class Game { // Define game properties and methods here } - public class ConcreteApp extends App { + // public class ConcreteApp extends App { - public ConcreteApp(int webSocketPort) { - super(webSocketPort); - } + // public ConcreteApp(int webSocketPort) { + // super(webSocketPort); + // } - @Override - public void onStart() { - // Logic for handling server start - } + // @Override + // public void onStart() { + // // Logic for handling server start + // } + + // @Override + // public void onError(WebSocket conn, Exception ex) { + // // Logic for handling errors + // } + // } + + public static void main(String[] args) { + + // Set up the http server + int port = 9080; + HttpServer H = new HttpServer(port, "./html"); + H.begin(); + System.out.println("http Server started on port:" + port); + + // create and start the websocket server + + port = 9880; + App A = new App(port); + A.start(); + System.out.println("websocket Server started on port: " + port); - @Override - public void onError(WebSocket conn, Exception ex) { - // Logic for handling errors - } } } \ No newline at end of file