Skip to content

Commit

Permalink
Added other public app constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
JYNguyen committed Apr 23, 2024
1 parent b7a5eca commit 7fe670c
Showing 1 changed file with 86 additions and 45 deletions.
131 changes: 86 additions & 45 deletions src/main/java/uta/cse3310/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Game> 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<Game> activeGames = new Vector<Game>();
private Map<WebSocket, Player> playerMap;
private Map<String, WebSocket> playerNickMap;

Expand All @@ -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.<Draft>singletonList(draft));
}

@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
// Logic for handling websocket on open event
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
}
}

0 comments on commit 7fe670c

Please sign in to comment.