/** Transport connected. {@link IOTransport} calls this when a connection is established. */ public void transportConnected() { setState(STATE_READY); if (reconnectTask != null) { reconnectTask.cancel(); reconnectTask = null; } resetTimeout(); synchronized (outputBuffer) { if (transport.canSendBulk()) { ConcurrentLinkedQueue<String> outputBuffer = this.outputBuffer; this.outputBuffer = new ConcurrentLinkedQueue<String>(); try { // DEBUG String[] texts = outputBuffer.toArray(new String[outputBuffer.size()]); log.debug("Bulk start:"); for (String text : texts) { log.debug("> " + text); } log.debug("Bulk end"); // DEBUG END transport.sendBulk(texts); } catch (IOException e) { this.outputBuffer = outputBuffer; } } else { String text; while ((text = outputBuffer.poll()) != null) { sendPlain(text); } } this.keepAliveInQueue = false; } }
/** Invalidates an {@link IOTransport}, used for forced reconnecting. */ private void invalidateTransport() { if (transport != null) { transport.invalidate(); } transport = null; }
/** * Sends a plain message to the {@link IOTransport}. * * @param text the Text to be send. */ private void sendPlain(String text) { synchronized (outputBuffer) { if (getState() == STATE_READY) { try { log.debug("> " + text); transport.send(text); } catch (Exception e) { log.debug("IOEx: saving"); outputBuffer.add(text); } } else { outputBuffer.add(text); } } }
/** Cleanup. IOConnection is not usable after calling this. */ private void cleanup() { setState(STATE_INVALID); if (transport != null) { transport.disconnect(); } sockets.clear(); synchronized (connections) { List<IOConnection> con = connections.get(urlStr); if (con != null && con.size() > 1) { con.remove(this); } else { connections.remove(urlStr); } } log.debug("cleanup"); backgroundTimer.cancel(); }
/** Connect transport */ private void connectTransport() { if (getState() == STATE_INVALID) { return; } setState(STATE_CONNECTING); if (protocols.contains(WebSocketTransport.TRANSPORT_NAME)) { transport = WebSocketTransport.create(this.url.getProtocol() + "://" + this.url.getAuthority(), this); } else if (protocols.contains(XhrTransport.TRANSPORT_NAME)) { transport = XhrTransport.create(this.url.getProtocol() + "://" + this.url.getAuthority(), this); } else { error( new SocketIOException( "Server supports no available transports. You should reconfigure the server to support a available transport")); return; } transport.connect(); }
/** * Returns the name of the used transport * * @return the name of the currently used transport */ public String getTransport() { IOTransport transport = this.connection.getTransport(); return transport != null ? transport.getName() : null; }