okhttp3.ws.WebSocketCall Java Examples

The following examples show how to use okhttp3.ws.WebSocketCall. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WebSocketOKActivity.java    From android with MIT License 6 votes vote down vote up
private void connectToServer() {
    String ip = etIP.getText().toString();
    String port = etPort.getText().toString();
    if (TextUtils.isEmpty(ip) || TextUtils.isEmpty(port)) {
        Snackbar.make(viewMain, "IP and Port 不能为空", Snackbar.LENGTH_LONG).show();
        return;
    }

    mClient = new OKClient();

    mAddress = String.format("ws://%s:%s", ip, port);

    OkHttpClient.Builder builder = new OkHttpClient.Builder().readTimeout(0, TimeUnit.NANOSECONDS);
    OkHttpClient okHttpClient = builder.build();
    Request request = new Request.Builder()
            .url(mAddress)
            .build();
    WebSocketCall socketCall = WebSocketCall.create(okHttpClient, request);
    socketCall.enqueue(mClient);

    tvStatus.setText(mAddress);
}
 
Example #2
Source File: MirrorService.java    From mirror with GNU General Public License v3.0 6 votes vote down vote up
private void onResolveSuccess(NsdServiceInfo nsdServiceInfo) {
    MirrorInfo mirrorInfo = new MirrorInfo(nsdServiceInfo);
    PreferenceUtils.setMirror(this, MirrorUtils.buildMirrorHttpUrl(mirrorInfo));
    RxBus.getInstance().post(new MirrorResolveSuccessEvent(mirrorInfo));

    OkHttpClient client = new OkHttpClient.Builder()
            .connectTimeout(MirrorUtils.MIRROR_CONNECT_TIMEOUT, TimeUnit.MILLISECONDS)
            .readTimeout(MirrorUtils.MIRROR_READ_TIMEOUT, TimeUnit.MILLISECONDS)
            .retryOnConnectionFailure(true)
            .build();
    Request request = new Request.Builder()
            .url(MirrorUtils.buildMirrorWebSocketUrl(mirrorInfo))
            .build();
    mWebSocketCall = WebSocketCall.create(client, request);
    mWebSocketCall.enqueue(this);
}
 
Example #3
Source File: BaseHttpClientGenerator.java    From hawkular-agent with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a websocket that connects to the given URL.
 *
 * @param url where the websocket server is
 * @param headers headers to pass in the connect request
 * @return the websocket
 */
public WebSocketCall createWebSocketCall(String url, Map<String, String> headers) {
    String base64Credentials = buildBase64Credentials();

    Request.Builder requestBuilder = new Request.Builder()
            .url(url)
            .addHeader("Authorization", "Basic " + base64Credentials)
            .addHeader("Accept", "application/json");

    if (headers != null) {
        for (Map.Entry<String, String> header : headers.entrySet()) {
            requestBuilder.addHeader(header.getKey(), header.getValue());
        }
    }

    Request request = requestBuilder.build();
    WebSocketCall wsc = WebSocketCall.create(getHttpClient(), request);
    return wsc;
}
 
Example #4
Source File: CommandCli.java    From hawkular-agent with Apache License 2.0 6 votes vote down vote up
private static CliWebSocketListener sendCommand(Config config) throws Exception {

        OkHttpClient httpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(5, TimeUnit.MINUTES).build();

        Request request = new Request.Builder()
                .url(config.serverUrl)
                .addHeader("Authorization", Credentials.basic(config.username, config.password))
                .addHeader("Accept", "application/json")
                .build();

        WebSocketCall wsc = WebSocketCall.create(httpClient, request);
        CliWebSocketListener listener = new CliWebSocketListener(httpClient, wsc, config);
        return listener;
    }
 
Example #5
Source File: BJWebSocketClient.java    From bjnetwork with Apache License 2.0 5 votes vote down vote up
public void connect() {
    logInfo("connect() while environment is " +
            "(state="+mState+", address="+address+", " +
            "SendMsgQueueSize="+mSendMessageThread.mMessageQueue.size()+")");

    if (mState != State.Offline) return;

    if (TextUtils.isEmpty(address)) {
        throw new NullPointerException("address is empty!");
    }

    if (mReconnectSignalHandler == null) {
        HandlerThread handlerThread = new HandlerThread("ReconnectSignalHandlerThread");
        handlerThread.start();
        mReconnectSignalHandler = new ReconnectSignalHandler(this, handlerThread);
    }

    setAndNotifyStateChanged(State.Connecting);

    Request request = new Request.Builder().url(getAddress())
            .build();
    mWebSocketCall = WebSocketCall.create(mNetworkClient.getOkHttpClient(),
            request);
    mWSListener = new WSListener();
    mWebSocketCall.enqueue(mWSListener);

    if (mSendMessageThread.getState() != Thread.State.NEW) {
        mSendMessageThread = mSendMessageThread.clone();
    }
}
 
Example #6
Source File: CommandCli.java    From hawkular-agent with Apache License 2.0 4 votes vote down vote up
public CliWebSocketListener(OkHttpClient httpClient, WebSocketCall wsc, Config config) {
    this.httpClient = httpClient;
    this.config = config;
    wsc.enqueue(this);
}