Code example for SocketChannel

Methods: configureBlocking, connect, register

0
	private boolean tryToConnect(InetSocketAddress remote) {
		// The WebSocket constructor expects a SocketChannel that is 
		// non-blocking, and has a Selector attached to it. 
		try { 
			client = SocketChannel.open();
			client.configureBlocking(false);
			client.connect(remote);
 
			selector = Selector.open();
 
			this.conn = new WebSocket(client,
					new LinkedBlockingQueue<ByteBuffer>(), this);
			// At first, we're only interested in the 'CONNECT' keys. 
			client.register(selector, SelectionKey.OP_CONNECT);
 
		} catch (IOException ex) {
			onIOError(conn, ex);
			return false; 
		} 
 
		return true;