@SuppressWarnings("serial") @Override public void run() { try { /** ***************************** */ /* Create connection */ /* to Poker Server */ /** ***************************** */ pokerClient = new ServerClient(InetAddress.getByName("localhost"), 9090); /** ***************************** */ /* Send initialize message */ /** ***************************** */ log.info("Initializing " + sessionId + ", type " + type.name()); pokerClient.send( MessageFactory.createMessage( "", EventType.INIT, new Hashtable<String, String>() { { put("sessionId", new Long(sessionId).toString()); put("type", type.name()); } }, null, null)); /** ***************************** */ /* Continue to either forward */ /* messages from browser to */ /* Poker Server (type READ) */ /* or from Poker Server */ /* to browser (type WRITE) */ /** ***************************** */ while (pokerClient.isValid()) { if (this.type.equals(WebClient.types.READ)) { String message = readMessage(); pokerClient.send(message); } else if (this.type.equals(WebClient.types.WRITE)) { String message = pokerClient.read(); this.sendMessage(message.getBytes()); } } } catch (Exception ex) { this.close(); } }
/** Close connection to browser */ public void close() { try { if (pokerClient != null) pokerClient.close(); this.webSocket.close(); } catch (IOException e) { e.printStackTrace(); } if (this.type.equals(WebClient.types.WRITE)) PokerWeb.reUseSessionId(sessionId); }