Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Jenkins Skip to content Jenkins log in Dashboard Mat Carter My Views All cosc220_2022_java #499 Jacoco minigames.client ChatWebSocket Status Changes Console Output View Build Information Git Build Data Test Result Coverage Report Previous Build Next Build Package: ChatWebSocket ChatWebSocket name instruction branch complexity line method ChatWebSocket(Vertx, Chat, MinigameNetworkClient) M: 3 C: 32 91% M: 0 C: 0 100% M: 0 C: 1 100% M: 2 C: 13 87% M: 0 C: 1 100% checkNewMessage(HttpClient) M: 3 C: 16 84% M: 0 C: 0 100% M: 0 C: 1 100% M: 2 C: 5 71% M: 0 C: 1 100% incomingMessageDeconstructor(String) M: 19 C: 57 75% M: 1 C: 3 75% M: 1 C: 2 67% M: 6 C: 14 70% M: 0 C: 1 100% lambda$checkNewMessage$0(WebSocketFrame) M: 0 C: 5 100% M: 0 C: 0 100% M: 0 C: 1 100% M: 0 C: 2 100% M: 0 C: 1 100% lambda$checkNewMessage$1(WebSocket) M: 0 C: 10 100% M: 0 C: 0 100% M: 0 C: 1 100% M: 0 C: 3 100% M: 0 C: 1 100% lambda$checkNewMessage$2(Throwable) M: 4 C: 0 0% M: 0 C: 0 100% M: 1 C: 0 0% M: 2 C: 0 0% M: 1 C: 0 0% lambda$sendMessage$3(JsonObject, WebSocket) M: 6 C: 0 0% M: 0 C: 0 100% M: 1 C: 0 0% M: 1 C: 0 0% M: 1 C: 0 0% lambda$sendMessage$4(Throwable) M: 4 C: 0 0% M: 0 C: 0 100% M: 1 C: 0 0% M: 1 C: 0 0% M: 1 C: 0 0% loadPastMessages(ArrayList) M: 41 C: 0 0% M: 2 C: 0 0% M: 2 C: 0 0% M: 9 C: 0 0% M: 1 C: 0 0% sendMessage(String) M: 48 C: 0 0% M: 0 C: 0 100% M: 1 C: 0 0% M: 13 C: 0 0% M: 1 C: 0 0% start() M: 3 C: 16 84% M: 0 C: 0 100% M: 0 C: 1 100% M: 2 C: 4 67% M: 0 C: 1 100% static {...} M: 0 C: 7 100% M: 0 C: 0 100% M: 0 C: 1 100% M: 0 C: 2 100% M: 0 C: 1 100% Coverage 1: package minigames.client; 2: 3: import io.vertx.core.AbstractVerticle; 4: import io.vertx.core.Future; 5: import io.vertx.core.Vertx; 6: import io.vertx.core.http.HttpClient; 7: import io.vertx.core.http.HttpClientOptions; 8: import io.vertx.core.http.WebSocket; 9: import io.vertx.core.http.WebSocketConnectOptions; 10: import io.vertx.core.json.DecodeException; 11: import io.vertx.core.json.JsonObject; 12: import java.time.LocalDateTime; 13: import java.time.format.DateTimeFormatter; 14: import java.util.ArrayList; 15: import java.util.Arrays; 16: import org.apache.logging.log4j.LogManager; 17: import org.apache.logging.log4j.Logger; 18: 19: public class ChatWebSocket extends AbstractVerticle { 20: public static final Integer port = MinigameNetworkClient.port; 21: public static final String serverURL = "localhost"; 22: public static final String serverURI = "/chat"; 23: public String userId; 24: public String userName; 25: public String time; 26: public String message; 27: public Future<WebSocket> future; 28: private HttpClient client; 29: private Chat frame; 30: 31: private MinigameNetworkClient mn; 32: private static final Logger logger = LogManager.getLogger(ChatWebSocket.class); 33: 34: /* 35: * Vertx options variable that allow various HTTP parameters to be easily and 36: * neatly set and passed to the WebSocket. 37: * Additionally, good for maintainability as it allows said parameters to be 38: * easily identified and reconfigured if 39: * needed. 40: */ 41: WebSocketConnectOptions options = 42: new WebSocketConnectOptions() 43: .setHost(serverURL) 44: .setPort(port) 45: .setURI(serverURI) 46: .setAllowOriginHeader(true) 47: .putHeader("Accept", "application/json") 48: .setSsl(false); 49: 50: /* 51: * ChatWebSocket is the constructor for this class, and instantiates the chat 52: * window. 53: * 54: * @param a Vertx verticle instance 55: * 56: * @param mnClient reference to the minigame network client 57: * 58: * @return null 59: * 60: * @exception Exception any exception will be caught and the stack trace printed 61: * to console 62: */ 63: public ChatWebSocket(Vertx vertx, Chat chatFrame, MinigameNetworkClient mnClient) { 64: try { 65: this.mn = mnClient; 66: this.frame = chatFrame; 67: frame.setupWindow(); 68: } catch (Exception e) { 69: e.printStackTrace(); 70: } 71: } 72: 73: /* 74: * start() is called when the class is instantiated. It creates a new vertx 75: * HttpClient instance. Additionally, it calls 76: * checkNewMessage() passing the said HttpClient as a parameter. 77: * 78: * @param null 79: * 80: * @return null 81: * 82: * @exception Exception any exception will be caught and the stack trace printed 83: * to console 84: */ 85: public void start() { 86: try { 87: client = vertx.createHttpClient(new HttpClientOptions().setShared(true)); 88: checkNewMessage(client); 89: } catch (Exception e) { 90: e.printStackTrace(); 91: } 92: } 93: 94: /* 95: * checkNewMessage() checks the server for new messages and creates the Web 96: * Socket for the client. It takes a vertx 97: * HttpClient as a parameter, and uses this to create a Web Socket, passing it 98: * the vertx options variable declared 99: * above. When this Web Socket successfully receives data from the server, it 100: * calls incomingMessageDeconstructor() and 101: * passes the received information to that function. 102: * 103: * @param a vertx HttpClient 104: * 105: * @return null 106: * 107: * @exception Exception any exception will be caught and the stack trace printed 108: * to console. Any non-fatal issues 109: * encountered by the Web Socket will also be caught and printed to console. 110: */ 111: public void checkNewMessage(HttpClient client) { 112: try { 113: 114: future = client.webSocket(options); 115: future.onSuccess( 116: webSocket -> { 117: // set handler for incoming messages 118: frame.setService(this); 119: webSocket.frameHandler( 120: frame -> { 121: incomingMessageDeconstructor(frame.textData()); 122: }); 123: }) 124: .onFailure( 125: err -> { 126: System.out.println(err); 127: }); 128: } catch (Exception e) { 129: e.printStackTrace(); 130: } 131: } 132: 133: /* 134: * incomingMessageDeconstructor() deconstructs incoming message objects. It 135: * takes a String JSON input (information in 136: * the JSON key:value configuration but of a String type due to the limited Web 137: * Socket frame output types) and turns it 138: * back into a JSON object. It then parses this object and assigns each value 139: * that should be contained within to their 140: * own variable. This information is then displayed by the chat window. 141: * 142: * @param A String object containing data in the JSON specification - should 143: * contain a user ID, a name, a time, and a 144: * message. 145: * 146: * @return null 147: * 148: * @exception Exception any exception will be caught and the stack trace printed 149: * to console 150: */ 151: public void incomingMessageDeconstructor(String json) { 152: try { 153: // if message from web socket is prefixed with "OLD" websocket is sending 154: // previous messages 155:• if (json.length() >= 3 && json.substring(0, 3).equals("OLD")) { 156: // separate text into individual messages 157: ArrayList<String> oldMessages = 158: new ArrayList<String>(Arrays.asList(json.split("-")[1].split("\n"))); 159: // pass list of all messages to loadPastMessages 160: loadPastMessages(oldMessages); 161: 162: } else { 163: JsonObject messageJson = null; 164: try { 165: messageJson = new JsonObject(json); 166: } catch (DecodeException de) { 167: logger.debug("Could not convert received message to JSON", de); 168: return; 169: } 170: 171: userId = messageJson.getString("userid"); 172: userName = messageJson.getString("name"); 173: time = messageJson.getString("time"); 174: message = messageJson.getString("message"); 175: frame.updateText((userName + " said (at " + time + ") :\n" + message + "\n")); 176: } 177: } catch (Exception e) { 178: e.printStackTrace(); 179: } 180: } 181: 182: /* 183: * sendMessage() is called when a message is to be sent and is passed said 184: * message as a String. It stores the exact 185: * time it is called in the desired format, and then creates a new JSON Object. 186: * The JSON object is passed the ID of the 187: * sender, the name of the sender, the time recorded, and the message. This is 188: * then all written to an immutable text 189: * frame which is then sent to the server via the previously created Web Socket. 190: * 191: * @param String message to be sent 192: * 193: * @return null 194: * 195: * @exception Exception any exception will be caught and the stack trace printed 196: * to console. Any non-fatal issues 197: * encountered by the Web Socket will also be caught and printed to console. 198: */ 199: public void sendMessage(String outgoingMessage) { 200: try { 201: DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); 202: LocalDateTime now = LocalDateTime.now(); 203: JsonObject chatMessage = new JsonObject(); 204: chatMessage.put("userid", mn.getLoginEmail()); 205: chatMessage.put("name", mn.getLoginName()); 206: chatMessage.put("time", dateTimeFormat.format(now)); 207: chatMessage.put("message", outgoingMessage); 208: future.onSuccess(ws -> ws.writeFinalTextFrame(chatMessage.encode())) 209: .onFailure(err -> System.out.println(err)); 210: } catch (Exception e) { 211: e.printStackTrace(); 212: } 213: } 214: 215: /* 216: * loadPastMessages is called when the client first connects to the server, to 217: * load past messages for the user, iterates through past messages and updates 218: * text in chat panel. 219: * 220: * @param ArrayList<String> each item in array list a past users message and its 221: * related data, e.g username and time sent 222: * 223: * 224: * @return Null 225: * 226: * Author: Raven Clodd 227: */ 228: public void loadPastMessages(ArrayList<String> oldMessages) { 229: 230:• for (int i = 1; i < oldMessages.size(); i++) { 231: 232: String item = oldMessages.get(i); 233: 234: String[] messageData = item.split(","); 235: 236: String userId = messageData[0]; 237: String userName = messageData[1]; 238: String time = messageData[2]; 239: String message = messageData[3]; 240: 241: frame.updateText(userName + " said (at " + time + ") :\n" + message + "\n"); 242: } 243: } 244: } REST API Jenkins 2.414 Get involved Website