From bc801a6c36d7d1ae3817cce1ea1bab633353ceb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9C=B0=E5=B9=B3=E7=BA=BF?= <22250530@qq.com> Date: Fri, 23 Oct 2015 11:06:02 +0800 Subject: [PATCH] --- .../redkale/net/client/WebSocketClient.java | 86 +++++++++++++++++++ .../src/com/wentch/redkale/util/Utility.java | 4 + 2 files changed, 90 insertions(+) create mode 100644 android-jdk6-redkale/src/com/wentch/redkale/net/client/WebSocketClient.java diff --git a/android-jdk6-redkale/src/com/wentch/redkale/net/client/WebSocketClient.java b/android-jdk6-redkale/src/com/wentch/redkale/net/client/WebSocketClient.java new file mode 100644 index 000000000..0c121f393 --- /dev/null +++ b/android-jdk6-redkale/src/com/wentch/redkale/net/client/WebSocketClient.java @@ -0,0 +1,86 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.wentch.redkale.net.client; + +import com.wentch.redkale.util.*; +import java.io.*; +import java.net.*; +import java.util.*; +import javax.net.ssl.*; + +/** + * + * @author zhangjx + */ +public class WebSocketClient { + + protected final URI uri; + + protected final Map headers = new HashMap(); + + private Proxy proxy; + + private SSLContext sslContext; + + private Socket socket; + + private WebSocketClient(URI uri, Map headers0) { + this.uri = uri; + if (headers0 != null) this.headers.putAll(headers0); + } + + public URI getURI() { + return uri; + } + + public static WebSocketClient create(URI uri) { + return create(uri, null); + } + + public static WebSocketClient create(URI uri, Map headers) { + return new WebSocketClient(uri, headers); + } + + public WebSocketClient setSSLContext(SSLContext sslContext) { + this.sslContext = sslContext; + return this; + } + + public WebSocketClient setProxy(Proxy proxy) { + this.proxy = proxy; + return this; + } + + public WebSocketClient addHeader(String name, String value) { + this.headers.put(name, value); + return this; + } + + public int getPort() { + int port = uri.getPort(); + if (port > 0) return port; + return "wss".equalsIgnoreCase(uri.getScheme()) ? 443 : 80; + } + + public void connect() throws IOException { + if ("wss".equalsIgnoreCase(uri.getScheme())) { + if (proxy == null) { + this.socket = (sslContext == null ? Utility.getDefaultSSLContext() : sslContext).getSocketFactory().createSocket(uri.getHost(), getPort()); + } else { + Socket s = new Socket(proxy); + s.connect(new InetSocketAddress(uri.getHost(), getPort())); + this.socket = (sslContext == null ? Utility.getDefaultSSLContext() : sslContext).getSocketFactory().createSocket(s, uri.getHost(), getPort(), true); + } + } else { + this.socket = proxy == null ? new Socket() : new Socket(proxy); + this.socket.connect(new InetSocketAddress(uri.getHost(), getPort())); + } + } + + public static void main(String[] args) throws Exception { + System.out.println(new URI("wss://talk.3wyc.com/pipes/ws/listen?test=aa").getQuery()); + } +} diff --git a/android-jdk6-redkale/src/com/wentch/redkale/util/Utility.java b/android-jdk6-redkale/src/com/wentch/redkale/util/Utility.java index 05138fc82..7f0c1e828 100644 --- a/android-jdk6-redkale/src/com/wentch/redkale/util/Utility.java +++ b/android-jdk6-redkale/src/com/wentch/redkale/util/Utility.java @@ -327,6 +327,10 @@ public final class Utility { } //----------------------------------------------------------------------------- + public static javax.net.ssl.SSLContext getDefaultSSLContext(){ + return DEFAULTSSL_CONTEXT; + } + public static Socket createDefaultSSLSocket(InetSocketAddress address) throws IOException { return createDefaultSSLSocket(address.getAddress(), address.getPort()); }