Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
The MIT License Copyright (c) 2010 Tad Glines Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
 package com.glines.socketio.client.jre;
 
 
 
 
 public class SocketIOConnectionXHRBase implements SocketIOConnection {
 	private final String host;
 	private final short port;
 	private final String transport;
 	private HttpClient client;
 	protected String sessionId;
 	protected long timeout;
 	protected boolean timedout = false;
 	protected Future<?> timeoutTask;
 	protected HttpExchange currentGet;
 	protected HttpExchange currentPost;
 	protected final boolean isConnectionPersistent;
 	protected AtomicBoolean doingSend = new AtomicBoolean(false);
 	protected AtomicLong messageId = new AtomicLong(0);
 	protected String closeId = null;
 	protected boolean disconnectWhenEmpty = false;
 
 			String hostshort portString transportboolean isConnectionPersistent) {
 		this. = listener;
 		this. = host;
 		this. = port;
 		this. = transport;
 		this. = isConnectionPersistent;
 	}
 	
 	protected void startTimeoutTimer() {
 		if (! &&  > 0) {
 				public void run() {
 					SocketIOConnectionXHRBase.this.onTimeout("Inactivity Timer Timeout");
 				}
 		}
 	}
 
 	protected void clearTimeoutTimer() {
 		if ( != null) {
 			 = null;
 		}
 	}
 	
	public void connect() {
			throw new IllegalStateException("Not CLOSED!");
		}
		 = new HttpClient();
		.setIdleTimeout(30*1000); //30 seconds
		try {
catch (Exception e) {
			 = null;
			_disconnect(."Failed to initialize");
			return;
		}
	}
	public void close() {
		 = "" + .get();
	}
	public void disconnect() {
	}
	protected void _disconnect(DisconnectReason reasonString error) {
		switch () {
			break;
		case :
			break;
		case :
			if ( != null) {
else {
			}
			break;
		case :
			.onDisconnect(reasonerror);
			break;
		}
		if ( != null) {
			 = null;
		}
		if ( != null) {
			 = null;
		}
		if ( != null) {
			try {
catch (Exception e) {
				// Ignore
			}
			 = null;
		}
	}
		return ;
	}
	public void sendMessage(String messagethrows SocketIOException {
	}
	public void sendMessage(int messageTypeString messagethrows SocketIOException {
		sendFrame(new SocketIOFrame(..messageTypemessage));
	}
	protected void sendFrame(SocketIOFrame frame) {
        .offer(frame);
	}
	protected void checkSend() {
		if (.compareAndSet(falsetrue)) {
            Collection<SocketIOFrameelements = new LinkedList<SocketIOFrame>();
            .drainTo(elements);
            for (SocketIOFrame frameelements) {
					str.append(frame.encode());
				}
		}
	}
	protected void onTimeout(String error) {
	}
	protected void onData(String data) {
	}
	protected void onMessageBlock(String data) {
		for (SocketIOFrame frame: SocketIOFrame.parse(data)) {
			onFrame(frame);
		}
	}
	protected void onFrame(SocketIOFrame frame) {
		switch (frame.getFrameType()) {
				 = frame.getData();
			}
			break;
			 = Integer.parseInt(frame.getData());
			break;
		case :
			onClose(frame.getData());
			break;
		case :
			onPing(frame.getData());
			break;
		case :
			onPong(frame.getData());
			break;
		case :
			break;
		default:
			break;
		}
	}
	protected void onClose(String data) {
			if ( != null && .equals(data)) {
			}
else {
		}
	}
	protected void onPing(String data) {
	}
	protected void onPong(String data) {
		// Ignore
	}
	protected void onMessageData(int messageTypeString data) {
		if (messageType == .) {
			.onMessage(messageTypedata);
		}
	}
	protected String getRequestURI() {
		if ( != null) {
			return "/socket.io/" +  + "/" +  + "/" + System.currentTimeMillis();
else {
			return "/socket.io/" +  + "//" + System.currentTimeMillis();
		}
	}
	protected String getPostURI() {
		return getRequestURI() + "/send";
	}
	protected void doGet() {
		HttpExchange exch = new HttpExchange() {
			protected void onConnectionFailed(Throwable x) {
				 = null;
			}
			protected void onException(Throwable x) {
				 = null;
			}
			protected void onExpire() {
				 = null;
				_disconnect(."Request timed-out");
			}
			protected void onResponseComplete() throws IOException {
				 = null;
					if (this.getStatus() == 200) {
						}
else {
						_disconnect(."HTTP response code: " + this.getStatus());
					}
				}
			}
			protected void onResponseContent(Buffer contentthrows IOException {
				onData(content.toString());
			}
		};
		exch.setMethod("GET");
		exch.setAddress(new Address());
		try {
			.send(exch);
			 = exch;
catch (IOException e) {
		}
	}
	protected String getPostContentType() {
		return "application/x-www-form-urlencoded; charset=utf-8";
	}
	protected Buffer formatPostData(String data) {
		return new ByteArrayBuffer("data=" + URIUtil.encodePath(data));
	}
	protected void doSend(String data) {
		HttpExchange exch = new HttpExchange() {
			protected void onConnectionFailed(Throwable x) {
				 = null;
				.set(false);
			}
			protected void onException(Throwable x) {
				 = null;
				.set(false);
			}
			protected void onExpire() {
				 = null;
				.set(false);
				_disconnect(."Request timed-out");
			}
			protected void onResponseComplete() throws IOException {
				 = null;
				if (this.getStatus() != 200) {
					_disconnect(."POST Failed with response code: " + this.getStatus());
else {
					.set(false);
				}
			}
		};
		exch.setMethod("GET");
		try {
			.send(exch);
			 = exch;
catch (IOException e) {
			.set(false);
			_disconnect(."Failed to create SEND request");
		}
	}
New to GrepCode? Check out our FAQ X