當前位置: 首頁>>代碼示例>>Java>>正文


Java WebSocketVersion類代碼示例

本文整理匯總了Java中org.jboss.netty.handler.codec.http.websocketx.WebSocketVersion的典型用法代碼示例。如果您正苦於以下問題:Java WebSocketVersion類的具體用法?Java WebSocketVersion怎麽用?Java WebSocketVersion使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


WebSocketVersion類屬於org.jboss.netty.handler.codec.http.websocketx包,在下文中一共展示了WebSocketVersion類的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: connect

import org.jboss.netty.handler.codec.http.websocketx.WebSocketVersion; //導入依賴的package包/類
public static ClientChannel connect(LoggerFactory loggerFactory, URI uri,
        int connectTimeoutMillis) throws ChannelException {
    Logger logger = loggerFactory.create(String.format("WebSocketClientHandler-%s", uri));

    WebSocketClientChannel clientChannel = new WebSocketClientChannel();
    clientChannel.setUri(uri);

    ConnectingChannelHandler handler = new ConnectingChannelHandler();
    clientChannel.setChannelHandler(handler);

    WebSocketClientUpstreamHandler wsHandler = new WebSocketClientUpstreamHandler(logger,
            clientChannel);
    ChannelPipeline pipeline = Channels.pipeline();
    pipeline.addLast("decoder", new HttpResponseDecoder());
    pipeline.addLast("encoder", new HttpRequestEncoder());
    // connect
    Channel channel = prepareAndConnect(logger, uri, pipeline, wsHandler,
            uri.getScheme().equalsIgnoreCase("wss"), connectTimeoutMillis);
    // handshake
    try {
        WebSocketClientHandshaker handshaker = wsFactory.newHandshaker(uri,
                WebSocketVersion.V13, null, true, WebSocketClientHelper.getHeaders(uri));
        wsHandler.handshaker = handshaker;
        handshaker.handshake(channel);
        // return maybe fast than call
        if (!wsHandler.handshaker.isHandshakeComplete() && (handler.error == null)) {
            synchronized (handler.syncObject) {
                handler.syncObject.wait(connectTimeoutMillis);
            }
        }
    } catch (Exception e) {
        throw new ChannelException(Text.WS_HANDSHAKE_ERROR, e);
    }

    if (wsHandler.handshaker.isHandshakeComplete()) {
        return clientChannel;
    }
    if (handler.error != null) {
        throw new ChannelException(Text.CONNECT_FAIL + ": " + handler.error.getMessage(),
                handler.error);
    }

    throw new ChannelException(Text.CONNECT_TIMEOUT);
}
 
開發者ID:kuiwang,項目名稱:my-dev,代碼行數:45,代碼來源:WebSocketClient.java

示例2: NettyWSClient

import org.jboss.netty.handler.codec.http.websocketx.WebSocketVersion; //導入依賴的package包/類
public NettyWSClient(String uriStr, WebSocketClient webSocketClient)
{
	
	this.t = webSocketClient;
	this.uri = URI.create(uriStr);
	

	String protocol = this.uri.getScheme();
	if (!protocol.equals("ws"))
	{
		throw new IllegalArgumentException("Unsupported protocol: " + protocol);
	}
	
	ClientBootstrap bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
	
	Channel ch = null;

	HashMap<String, String> customHeaders = new HashMap<String, String>();
	customHeaders.put("MyHeader", "MyValue");
	
	// Connect with V13 (RFC 6455 aka HyBi-17). You can change it to V08 or V00.
	// If you change it to V00, ping is not supported and remember to change
	// HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline.
	this.handshaker = new WebSocketClientHandshakerFactory().newHandshaker(this.uri, WebSocketVersion.V13, null, false, customHeaders);
	
	bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
		public ChannelPipeline getPipeline() throws Exception
		{
			ChannelPipeline pipeline = Channels.pipeline();
			
			pipeline.addLast("decoder", new HttpResponseDecoder());
			pipeline.addLast("encoder", new HttpRequestEncoder());
			pipeline.addLast("ws-handler", NettyWSClient.this);
			return pipeline;
		}
	});
	
	ChannelFuture future = bootstrap.connect(new InetSocketAddress(this.uri.getHost(), this.uri.getPort()));
	future.syncUninterruptibly();
	ch = future.getChannel();
	try
	{
		this.handshaker.handshake(ch);
	}
	catch (Exception e)
	{
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
 
開發者ID:EricssonResearch,項目名稱:trap,代碼行數:51,代碼來源:NettyWSClient.java


注:本文中的org.jboss.netty.handler.codec.http.websocketx.WebSocketVersion類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。