Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2014 Red Hat, Inc., and individual contributors
   * as indicated by the @author tags.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 
 package io.undertow.websockets.client;
 
 
 import org.xnio.Pool;
 
 import java.net.URI;
 import java.util.List;
 import java.util.Map;

The Web socket client.

Author(s):
Stuart Douglas
 
 public class WebSocketClient {
 
 
     public static IoFuture<WebSocketChannelconnect(XnioWorker workerfinal Pool<ByteBufferbufferPoolfinal OptionMap optionMapfinal URI uriWebSocketVersion version) {
         return connect(workerbufferPooloptionMapuriversionnull);
     }
 
     public static IoFuture<WebSocketChannelconnect(XnioWorker workerXnioSsl sslfinal Pool<ByteBufferbufferPoolfinal OptionMap optionMapfinal URI uriWebSocketVersion version) {
         return connect(workersslbufferPooloptionMapuriversionnull);
     }
 
     public static IoFuture<WebSocketChannelconnect(XnioWorker workerfinal Pool<ByteBufferbufferPoolfinal OptionMap optionMapfinal URI uriWebSocketVersion versionWebSocketClientNegotiation clientNegotiation) {
         return connect(workernullbufferPooloptionMapuriversionclientNegotiation);
     }
 
     public static IoFuture<WebSocketChannelconnect(XnioWorker workerXnioSsl sslfinal Pool<ByteBufferbufferPoolfinal OptionMap optionMapfinal URI uriWebSocketVersion versionWebSocketClientNegotiation clientNegotiation) {
 
         final FutureResult<WebSocketChannelioFuture = new FutureResult<>();
         final URI newUri;
         try {
             newUri = new URI(uri.getScheme().equals("wss") ? "https" : "http"uri.getUserInfo(), uri.getHost(), uri.getPort() == -1 ? (uri.getScheme().equals("wss") ? 443 : 80) : uri.getPort(), uri.getPath().isEmpty() ? "/" : uri.getPath(), uri.getQuery(), uri.getFragment());
         } catch (URISyntaxException e) {
             throw new RuntimeException(e);
         }
         final WebSocketClientHandshake handshake = WebSocketClientHandshake.create(versionnewUriclientNegotiation);
         final Map<StringStringoriginalHeaders = handshake.createHeaders();
         originalHeaders.put(.uri.getHost());
         final Map<StringList<String>> headers = new HashMap<>();
         for(Map.Entry<StringStringentry : originalHeaders.entrySet()) {
             List<Stringlist = new ArrayList<>();
             list.add(entry.getValue());
             headers.put(entry.getKey(), list);
         }
         if (clientNegotiation != null) {
             clientNegotiation.beforeRequest(headers);
         }
         final IoFuture<? extends StreamConnectionresult;
         if (ssl != null) {
             result = HttpUpgrade.performUpgrade(workersslnullnewUriheadersnew ChannelListener<StreamConnection>() {
                 @Override
                 public void handleEvent(StreamConnection channel) {
                     WebSocketChannel result = handshake.createChannel(channelnewUri.toString(), bufferPool);
                     ioFuture.setResult(result);
                 }
             }, nulloptionMaphandshake.handshakeChecker(newUriheaders));
         } else {
             result = HttpUpgrade.performUpgrade(workernullnewUriheadersnew ChannelListener<StreamConnection>() {
                 @Override
                 public void handleEvent(StreamConnection channel) {
                     WebSocketChannel result = handshake.createChannel(channelnewUri.toString(), bufferPool);
                     ioFuture.setResult(result);
                }
            }, nulloptionMaphandshake.handshakeChecker(newUriheaders));
        }
        result.addNotifier(new IoFuture.Notifier<StreamConnectionObject>() {
            @Override
            public void notify(IoFuture<? extends StreamConnectionresObject attachment) {
                if (res.getStatus() == ..) {
                    ioFuture.setException(res.getException());
                }
            }
        }, null);
        ioFuture.addCancelHandler(new Cancellable() {
            @Override
            public Cancellable cancel() {
                result.cancel();
                return null;
            }
        });
        return ioFuture.getIoFuture();
    }
    private WebSocketClient() {
    }
New to GrepCode? Check out our FAQ X