/* * (non-Javadoc) * * @see java.lang.Thread#run() */ @Override public void run() { connection.transportConnected(); while (isConnect()) { try { String line; URL url = new URL(XhrTransport.this.url.toString() + "?t=" + System.currentTimeMillis()); urlConnection = (HttpURLConnection) url.openConnection(); SSLContext context = IOConnection.getSslContext(); if (urlConnection instanceof HttpsURLConnection && context != null) { ((HttpsURLConnection) urlConnection).setSSLSocketFactory(context.getSocketFactory()); } if (!queue.isEmpty()) { urlConnection.setDoOutput(true); OutputStream output = urlConnection.getOutputStream(); if (queue.size() == 1) { line = queue.poll(); output.write(line.getBytes(CHARSET)); } else { Iterator<String> iter = queue.iterator(); while (iter.hasNext()) { String junk = iter.next(); line = IOConnection.FRAME_DELIMITER + junk.length() + IOConnection.FRAME_DELIMITER + junk; output.write(line.getBytes(CHARSET)); iter.remove(); } } output.close(); InputStream input = urlConnection.getInputStream(); byte[] buffer = new byte[1024]; while (input.read(buffer) > 0) {} input.close(); } else { setBlocked(true); InputStream plainInput = urlConnection.getInputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(plainInput, CHARSET)); while ((line = input.readLine()) != null) { if (connection != null) connection.transportData(line); } setBlocked(false); } } catch (IOException e) { if (connection != null && interrupted() == false) { connection.transportError(e); return; } } try { sleep(100); } catch (InterruptedException e) { } } connection.transportDisconnected(); }
public WebsocketTransport(URI uri, IOConnection iOConnection) { super(uri); this.connection = iOConnection; SSLContext sslContext = IOConnection.getSslContext(); if ("wss".equals(uri.getScheme()) && sslContext != null) { setWebSocketFactory(new DefaultSSLWebSocketClientFactory(sslContext)); } }