/* * (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)); } }
public static IOTransport create(URL url, IOConnection iOConnection) { return new WebsocketTransport( URI.create( new StringBuilder( String.valueOf(PATTERN_HTTP.matcher(url.toString()).replaceFirst("ws"))) .append(IOConnection.SOCKET_IO_1) .append(TRANSPORT_NAME) .append("/") .append(iOConnection.getSessionId()) .toString()), iOConnection); }
/** * Creates a new Transport for the given url an {@link IOConnection}. * * @param url the url * @param connection the connection * @return the iO transport */ public static IOTransport create(URL url, IOConnection connection) { try { URL xhrUrl = new URL( url.toString() + IOConnection.SOCKET_IO_1 + TRANSPORT_NAME + "/" + connection.getSessionId()); return new XhrTransport(xhrUrl, connection); } catch (MalformedURLException e) { throw new RuntimeException( "Malformed Internal url. This should never happen. Please report a bug.", e); } }
/** * Sets url and callback and initiates connecting if both are present * * @param url the url * @param callback the callback * @return true if connecting has been initiated, false if not */ private boolean setAndConnect(URL url, IOCallback callback) { if (this.connection != null) throw new RuntimeException( "You can connect your SocketIO instance only once. Use a fresh instance instead."); if ((this.url != null && url != null) || (this.callback != null && callback != null)) return false; if (url != null) { this.url = url; } if (callback != null) { this.callback = callback; } if (this.callback != null && this.url != null) { final String origin = this.url.getProtocol() + "://" + this.url.getAuthority(); this.namespace = this.url.getPath(); if (this.namespace.equals("/")) { this.namespace = ""; } this.connection = IOConnection.register(origin, this); return true; } return false; }
/** * Set the socket factory used for SSL connections. * * @param socketFactory */ public static void setDefaultSSLSocketFactory(SSLContext sslContext) { IOConnection.setSslContext(sslContext); }