com.neovisionaries.ws.client.WebSocketFactory Java Examples

The following examples show how to use com.neovisionaries.ws.client.WebSocketFactory. 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: SystemStatusService.java    From AirMapSDK-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Initialize an SystemStatusService to receive system health updates
 *
 * @param context An Android Context
 */
public SystemStatusService(Context context){
    webSocketFactory = new WebSocketFactory();
    try {
        webSocket = webSocketFactory.createSocket(systemStatusSocketUrl, 5000);
    } catch (IOException e) {
        Timber.e(e);
        e.printStackTrace();
    }
    webSocket.addListener(new AirMapWebSocketAdapter());
    webSocket.addHeader("x-api-key", AirMap.getApiKey());
    webSocket.setUserInfo(AirMap.getAuthToken());
    listeners = new ArrayList<>();
    components = new HashMap<>();
    notNormalComponents = new ArrayList<>();
    gson = new Gson();
    handler = new Handler(Looper.getMainLooper());
}
 
Example #2
Source File: Meteor.java    From Android-DDP with Apache License 2.0 6 votes vote down vote up
/**
 * Opens a connection to the server over websocket
 *
 * @param isReconnect whether this is a re-connect attempt or not
 */
private void openConnection(final boolean isReconnect) {
	if (isReconnect) {
		if (mConnected) {
			initConnection(mSessionID);
			return;
		}
	}

	// create a new WebSocket connection for the data transfer
	try {
		mWebSocket = new WebSocketFactory().setConnectionTimeout(30000).createSocket(mServerUri);
	}
	catch (final IOException e) {
		mCallbackProxy.onException(e);
	}

	mWebSocket.setMissingCloseFrameAllowed(true);
	mWebSocket.setPingInterval(25 * 1000);
	mWebSocket.addListener(mWebSocketListener);
	mWebSocket.connectAsynchronously();
}
 
Example #3
Source File: WebSocketBase.java    From shinny-futures-android with GNU General Public License v3.0 5 votes vote down vote up
private void connect(long currentTime) {
    mConnectTime = currentTime;

    if (mTimer != null) {
        mTimer.cancel();
        mTimer = null;
    }

    if (mTimerTask != null) {
        mTimerTask.cancel();
        mTimerTask = null;
    }
    try {
        if (mWebSocketClient != null) mWebSocketClient.clearListeners();
        mWebSocketClient = new WebSocketFactory()
                .setVerifyHostname(TRANSACTION_URL.equals(mUrls.get(0)))
                .setConnectionTimeout(5000)
                .createSocket(mUrls.get(mIndex))
                .setMissingCloseFrameAllowed(false)
                .addListener(this)
                .addHeader("User-Agent", sDataManager.USER_AGENT + " " + sDataManager.APP_VERSION)
                .addHeader("SA-Machine", Amplitude.getInstance().getDeviceId())
                .addHeader("SA-Session", Amplitude.getInstance().getDeviceId())
                .addExtension(WebSocketExtension.PERMESSAGE_DEFLATE);
    } catch (IOException e) {
        e.printStackTrace();
    }
    mIndex += 1;
    if (mIndex == mUrls.size()) mIndex = 0;
    mWebSocketClient.connectAsynchronously();
}
 
Example #4
Source File: DecentWalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public DecentWalletManager(String endpoint, DecentListener adapter, String words) {
  this.endpoint = endpoint;
  this.adapter = adapter;
  BrainKeyDict.words = words;
  this.wsFactory = new WebSocketFactory();
  this.wallet = new DecentWallet();
  if (wallet.isInitialized()) {
    adapter.onWalletRestored();
  } else {
    adapter.onImportRequired();
  }
}
 
Example #5
Source File: DecentWalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public DecentWalletManager(String endpoint, DecentListener adapter) {
  this.endpoint = endpoint;
  this.adapter = adapter;
  this.wsFactory = new WebSocketFactory();
  this.wallet = new DecentWallet();
  if (wallet.isInitialized()) {
    adapter.onWalletRestored();
  } else {
    adapter.onImportRequired();
  }
}
 
Example #6
Source File: NvSpeechServiceClient.java    From SpeechToText-WebSockets-Java with MIT License 5 votes vote down vote up
@Override
public MessageSender start(SpeechServiceConfig config, MessageReceiver receiver) throws Exception {
    String connectionId = newGuid();
    ConnectionTelemetry telemetry = ConnectionTelemetry.forId(connectionId);

    WebSocketFactory factory = new WebSocketFactory();
    webSocket = factory.createSocket(config.getConnectionUrl(connectionId));
    webSocket.addListener(new NvMessageReceiver(socketCloseLatch, receiver, telemetry));
    telemetry.recordConnectionStarted();
    webSocket.connect();
    return new NvMessageSender(connectionId, webSocket);
}
 
Example #7
Source File: Socket.java    From socketcluster-client-java with Apache License 2.0 5 votes vote down vote up
public Socket(String URL) {
    this.URL = URL;
    factory = new WebSocketFactory().setConnectionTimeout(5000);
    counter = new AtomicInteger(1);
    acks = new HashMap<>();
    channels = new ConcurrentHashMap<>();
    adapter = getAdapter();
    headers = new HashMap<>();
    putDefaultHeaders();
}
 
Example #8
Source File: SessionConfig.java    From JDA with Apache License 2.0 5 votes vote down vote up
public SessionConfig(
    @Nullable SessionController sessionController, @Nullable OkHttpClient httpClient,
    @Nullable WebSocketFactory webSocketFactory, @Nullable VoiceDispatchInterceptor interceptor,
    EnumSet<ConfigFlag> flags, int maxReconnectDelay, int largeThreshold)
{
    this.sessionController = sessionController == null ? new ConcurrentSessionController() : sessionController;
    this.httpClient = httpClient;
    this.webSocketFactory = webSocketFactory == null ? newWebSocketFactory() : webSocketFactory;
    this.interceptor = interceptor;
    this.flags = flags;
    this.maxReconnectDelay = maxReconnectDelay;
    this.largeThreshold = largeThreshold;
}
 
Example #9
Source File: ShardingSessionConfig.java    From JDA with Apache License 2.0 5 votes vote down vote up
public ShardingSessionConfig(
    @Nullable SessionController sessionController, @Nullable VoiceDispatchInterceptor interceptor,
    @Nullable OkHttpClient httpClient, @Nullable OkHttpClient.Builder httpClientBuilder,
    @Nullable WebSocketFactory webSocketFactory, @Nullable IAudioSendFactory audioSendFactory,
    EnumSet<ConfigFlag> flags, EnumSet<ShardingConfigFlag> shardingFlags,
    int maxReconnectDelay, int largeThreshold)
{
    super(sessionController, httpClient, webSocketFactory, interceptor, flags, maxReconnectDelay, largeThreshold);
    if (httpClient == null)
        this.builder = httpClientBuilder == null ? IOUtil.newHttpClientBuilder() : httpClientBuilder;
    else
        this.builder = null;
    this.audioSendFactory = audioSendFactory;
    this.shardingFlags = shardingFlags;
}
 
Example #10
Source File: Socket.java    From socketcluster-client-java with Apache License 2.0 4 votes vote down vote up
/**
 * used to set up TLS/SSL connection to server for more details visit neovisionaries websocket client
 */

public WebSocketFactory getFactorySettings() {
    return factory;
}
 
Example #11
Source File: JDABuilder.java    From JDA with Apache License 2.0 4 votes vote down vote up
/**
 * Builds a new {@link net.dv8tion.jda.api.JDA} instance and uses the provided token to start the login process.
 * <br>The login process runs in a different thread, so while this will return immediately, {@link net.dv8tion.jda.api.JDA} has not
 * finished loading, thus many {@link net.dv8tion.jda.api.JDA} methods have the chance to return incorrect information.
 * For example {@link JDA#getGuilds()} might return an empty list or {@link net.dv8tion.jda.api.JDA#getUserById(long)} might return null
 * for arbitrary user IDs.
 *
 * <p>If you wish to be sure that the {@link net.dv8tion.jda.api.JDA} information is correct, please use
 * {@link net.dv8tion.jda.api.JDA#awaitReady() JDA.awaitReady()} or register an
 * {@link net.dv8tion.jda.api.hooks.EventListener EventListener} to listen for the
 * {@link net.dv8tion.jda.api.events.ReadyEvent ReadyEvent}.
 *
 * @throws LoginException
 *         If the provided token is invalid.
 * @throws IllegalArgumentException
 *         If the provided token is empty or null. Or the provided intents/cache configuration is not possible.
 *
 * @return A {@link net.dv8tion.jda.api.JDA} instance that has started the login process. It is unknown as
 *         to whether or not loading has finished when this returns.
 *
 * @see    net.dv8tion.jda.api.JDA#awaitReady()
 */
@Nonnull
public JDA build() throws LoginException
{
    checkIntents();
    OkHttpClient httpClient = this.httpClient;
    if (httpClient == null)
    {
        if (this.httpClientBuilder == null)
            this.httpClientBuilder = IOUtil.newHttpClientBuilder();
        httpClient = this.httpClientBuilder.build();
    }

    WebSocketFactory wsFactory = this.wsFactory == null ? new WebSocketFactory() : this.wsFactory;

    if (controller == null && shardInfo != null)
        controller = new ConcurrentSessionController();

    AuthorizationConfig authConfig = new AuthorizationConfig(token);
    ThreadingConfig threadingConfig = new ThreadingConfig();
    threadingConfig.setCallbackPool(callbackPool, shutdownCallbackPool);
    threadingConfig.setGatewayPool(mainWsPool, shutdownMainWsPool);
    threadingConfig.setRateLimitPool(rateLimitPool, shutdownRateLimitPool);
    threadingConfig.setEventPool(eventPool, shutdownEventPool);
    SessionConfig sessionConfig = new SessionConfig(controller, httpClient, wsFactory, voiceDispatchInterceptor, flags, maxReconnectDelay, largeThreshold);
    MetaConfig metaConfig = new MetaConfig(maxBufferSize, contextMap, cacheFlags, flags);

    JDAImpl jda = new JDAImpl(authConfig, sessionConfig, threadingConfig, metaConfig);
    jda.setMemberCachePolicy(memberCachePolicy);
    // We can only do member chunking with the GUILD_MEMBERS intent
    if ((intents & GatewayIntent.GUILD_MEMBERS.getRawValue()) == 0)
        jda.setChunkingFilter(ChunkingFilter.NONE);
    else
        jda.setChunkingFilter(chunkingFilter);

    if (eventManager != null)
        jda.setEventManager(eventManager);

    if (audioSendFactory != null)
        jda.setAudioSendFactory(audioSendFactory);

    listeners.forEach(jda::addEventListener);
    jda.setStatus(JDA.Status.INITIALIZED);  //This is already set by JDA internally, but this is to make sure the listeners catch it.

    // Set the presence information before connecting to have the correct information ready when sending IDENTIFY
    ((PresenceImpl) jda.getPresence())
            .setCacheActivity(activity)
            .setCacheIdle(idle)
            .setCacheStatus(status);
    jda.login(shardInfo, compression, true, intents);
    return jda;
}
 
Example #12
Source File: JDAImpl.java    From JDA with Apache License 2.0 4 votes vote down vote up
public WebSocketFactory getWebSocketFactory()
{
    return sessionConfig.getWebSocketFactory();
}
 
Example #13
Source File: SessionConfig.java    From JDA with Apache License 2.0 4 votes vote down vote up
private static WebSocketFactory newWebSocketFactory()
{
    return new WebSocketFactory().setConnectionTimeout(10000);
}
 
Example #14
Source File: SessionConfig.java    From JDA with Apache License 2.0 4 votes vote down vote up
@Nonnull
public WebSocketFactory getWebSocketFactory()
{
    return webSocketFactory;
}
 
Example #15
Source File: DefaultShardManagerBuilder.java    From JDA with Apache License 2.0 3 votes vote down vote up
/**
 * Sets the {@link com.neovisionaries.ws.client.WebSocketFactory WebSocketFactory} that will be used by JDA's websocket client.
 * This can be used to set things such as connection timeout and proxy.
 *
 * @param  factory
 *         The new {@link com.neovisionaries.ws.client.WebSocketFactory WebSocketFactory} to use.
 *
 * @return The DefaultShardManagerBuilder instance. Useful for chaining.
 */
@Nonnull
public DefaultShardManagerBuilder setWebsocketFactory(@Nullable WebSocketFactory factory)
{
    this.wsFactory = factory;
    return this;
}
 
Example #16
Source File: JDABuilder.java    From JDA with Apache License 2.0 3 votes vote down vote up
/**
 * Sets the {@link com.neovisionaries.ws.client.WebSocketFactory WebSocketFactory} that will be used by JDA's websocket client.
 * This can be used to set things such as connection timeout and proxy.
 *
 * @param  factory
 *         The new {@link com.neovisionaries.ws.client.WebSocketFactory WebSocketFactory} to use.
 *
 * @return The JDABuilder instance. Useful for chaining.
 */
@Nonnull
public JDABuilder setWebsocketFactory(@Nullable WebSocketFactory factory)
{
    this.wsFactory = factory;
    return this;
}