在Android上连接到Pushbullet的安全Websocket [英] Connecting to Pushbullet's Secure Websocket on Android

查看:109
本文介绍了在Android上连接到Pushbullet的安全Websocket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Pushbullet很棒的服务有一个websocket流,您可以订阅它,然后侦听对设备的推送,这正是我想要的.我希望我的App能够连接到消息流,并根据消息的内容进行处理.

The awesome service that is Pushbullet have a websocket stream that you can subscribe to and then listen for pushes to your device, which is exactly what I want. I want my App to be able to connect to the messages stream and do stuff based on what they say.

我尝试使用 https://github.com/andrepew/Java-WebSocket/tree/1.3.0-Android-SSL-Fix (从

I've tried using https://github.com/andrepew/Java-WebSocket/tree/1.3.0-Android-SSL-Fix (forked from https://github.com/TooTallNate/Java-WebSocket), but am getting no luck. After a bit of a timeout, the connection response comes back with

onClose -1, draft org.java_websocket.drafts.Draft_10@a38fd36 refuses handshake, false

05-22 03:24:30.709  15423-15904 java.lang.NullPointerException: ssl == null
05-22 03:24:30.710  15423-15904 com.android.org.conscrypt.NativeCrypto.SSL_read_BIO(Native Method)
05-22 03:24:30.710  15423-15904 com.android.org.conscrypt.OpenSSLEngineImpl.unwrap(OpenSSLEngineImpl.java:477)
05-22 03:24:30.710  15423-15904 javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:1006)

我要获取的代码(没有我的访问令牌...)甚至尝试信任所有主机"建议的黑客行为,

my code to get that is (without my access token...) even trying the "trust all hosts" suggested hack,

private void createConnection()
{
    URI uri = URI.create("wss://stream.pushbullet.com/websocket/" + pushAT);

    final WebSocketClient mWebSocketClient = new WebSocketClient(uri) {
        @Override
        public void onOpen(ServerHandshake serverHandshake) {
            Log.d(TAG, "onOpen " + serverHandshake);
        }

        @Override
        public void onMessage(String s) {
            Log.d(TAG, "onMessage " + s);
        }

        @Override
        public void onClose(int i, String s, boolean b) {
            Log.d(TAG, "onClose " + i + ", " + s + ", " + b);
        }

        @Override
        public void onError(Exception e) {
            Log.d(TAG, "onError " + e);
            e.printStackTrace();
        }
    };

    Log.d(TAG, "Connecting to " + uri);
    trustAllHosts(mWebSocketClient);

    mWebSocketClient.connect();
    Log.d(TAG, "Connecting to " + uri);
}

public void trustAllHosts(WebSocketClient wsc) {
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return new java.security.cert.X509Certificate[]{};
        }

        public void checkClientTrusted(X509Certificate[] chain,
                                       String authType) throws CertificateException {
        }

        public void checkServerTrusted(X509Certificate[] chain,
                                       String authType) throws CertificateException {
        }
    }};


    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        wsc.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(sc));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

推荐答案

在Android上,使用流传输(HTTP连接寿命长)而不是WebSocket可能会更轻松.这是我在Android上为此工作的一些示例代码:

You might have an easier time using the streaming (long lived HTTP connection) instead of WebSocket on Android. Here's some sample code I have working on Android for this:

final URL url = new URL("https://stream.pushbullet.com/streaming/" + <USER_API_KEY>);
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(38000);
connection.setConnectTimeout(8000);
connection.setRequestMethod("GET");
connection.setDoInput(true);

L.i("Connecting to stream server");

connection.connect();

final int responseCode = connection.getResponseCode();
if (responseCode != 200) {
    throw new QuietException("Unable to connect to stream, server returned " + responseCode);
}

final InputStream inputStream = connection.getInputStream();

try {
    final BufferedReader stream = new BufferedReader(new InputStreamReader(inputStream));

    final String line = stream.readLine();
    final JSONObject message = new JSONObject(line);
    // And now you can do something based on this message
} finally {
    inputStream.close();
}

这将在您打开应用程序时很好用.如果您想一直获取消息,那会有点困难.您需要通过GCM接收消息,这意味着为我们创建设备以将消息发送到您的应用.如果您需要这样做,建议您发送电子邮件至api@pushbullet.com以获取更多帮助.

This would work great while your app is open. If you want to get messages all the time, that's a bit harder. You'd need to receive the messages via GCM, which would mean creating a device for us to send the messages to for your app. If that's what you'd need, I suggest emailing api@pushbullet.com for more help.

这篇关于在Android上连接到Pushbullet的安全Websocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆