com.neovisionaries.ws.client.WebSocket.connect()方法的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(5.7k)|赞(0)|评价(0)|浏览(343)

本文整理了Java中com.neovisionaries.ws.client.WebSocket.connect()方法的一些代码示例,展示了WebSocket.connect()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WebSocket.connect()方法的具体详情如下:
包路径:com.neovisionaries.ws.client.WebSocket
类名称:WebSocket
方法名:connect

WebSocket.connect介绍

[英]Connect to the server, send an opening handshake to the server, receive the response and then start threads to communicate with the server.

As necessary, #addProtocol(String), #addExtension(WebSocketExtension)#addHeader(String,String) should be called before you call this method. It is because the parameters set by these methods are used in the opening handshake.

Also, as necessary, #getSocket() should be used to set up socket parameters before you call this method. For example, you can set the socket timeout like the following.

WebSocket websocket = ......; 
websocket. 
#getSocket(). 
Socket#setSoTimeout(int)(5000);

If the WebSocket endpoint requires Basic Authentication, you can set credentials by #setUserInfo(String) or #setUserInfo(String,String) before you call this method. Note that if the URI passed to WebSocketFactory.createSocket method contains the user-info part, you don't have to call setUserInfo method.

Note that this method can be called at most only once regardless of whether this method succeeded or failed. If you want to re-connect to the WebSocket endpoint, you have to create a new WebSocketinstance again by calling one of createSocket methods of a WebSocketFactory. You may find #recreate() method useful if you want to create a new WebSocket instance that has the same settings as this instance. (But settings you made on the raw socket are not copied.)
[中]连接到服务器,向服务器发送开始握手,接收响应,然后启动线程与服务器通信。
如有必要,在调用此方法之前,应先调用#addProtocol(String)、#addExtension(WebSocketExtension)#addHeader(String,String)。这是因为这些方法设置的参数是在开始握手时使用的。
此外,根据需要,在调用此方法之前,应使用#getSocket()设置套接字参数。例如,可以按如下方式设置套接字超时。

WebSocket websocket = ......; 
websocket. 
#getSocket(). 
Socket#setSoTimeout(int)(5000);

如果WebSocket端点需要基本身份验证,则可以在调用此方法之前通过#setUserInfo(字符串)或#setUserInfo(字符串,字符串)设置凭据。请注意,如果URI传递给WebSocketFactory。createSocket方法包含用户信息部分,您不必调用setUserInfo方法。
请注意,无论此方法是否成功,此方法最多只能调用一次。如果要重新连接到WebSocket端点,必须通过调用WebSocketFactory的createSocket方法之一再次创建新的WebSocketinstance。如果要创建与此实例具有相同设置的新WebSocket实例,您可能会发现#recreate()方法很有用。(但不会复制您在原始套接字上所做的设置。)

代码示例

代码示例来源:origin: TakahikoKawasaki/nv-websocket-client

@Override
  public WebSocket call() throws WebSocketException
  {
    return mWebSocket.connect();
  }
}

代码示例来源:origin: TakahikoKawasaki/nv-websocket-client

@Override
public void runMain()
{
  try
  {
    mWebSocket.connect();
  }
  catch (WebSocketException e)
  {
    handleError(e);
  }
}

代码示例来源:origin: DV8FromTheWorld/JDA

protected synchronized void connect()
{
  if (api.getStatus() != JDA.Status.ATTEMPTING_TO_RECONNECT)
    api.setStatus(JDA.Status.CONNECTING_TO_WEBSOCKET);
  if (shutdown)
    throw new RejectedExecutionException("JDA is shutdown!");
  initiating = true;
  String url = api.getGatewayUrl() + "?encoding=json&v=" + DISCORD_GATEWAY_VERSION;
  if (compression)
  {
    url += "&compress=zlib-stream";
    decompressBuffer = newDecompressBuffer();
  }
  try
  {
    socket = api.getWebSocketFactory()
        .createSocket(url)
        .addHeader("Accept-Encoding", "gzip")
        .addListener(this);
    socket.connect();
  }
  catch (IOException | WebSocketException e)
  {
    api.resetGatewayUrl();
    //Completely fail here. We couldn't make the connection.
    throw new IllegalStateException(e);
  }
}

代码示例来源:origin: com.neovisionaries/nv-websocket-client

@Override
  public WebSocket call() throws WebSocketException
  {
    return mWebSocket.connect();
  }
}

代码示例来源:origin: com.neovisionaries/nv-websocket-client

@Override
public void runMain()
{
  try
  {
    mWebSocket.connect();
  }
  catch (WebSocketException e)
  {
    handleError(e);
  }
}

代码示例来源:origin: twitch4j/twitch4j

/**
 * Connecting to IRC-WS
 */
@Synchronized
public void connect() {
  if (connectionState.equals(TMIConnectionState.DISCONNECTED) || connectionState.equals(TMIConnectionState.RECONNECTING)) {
    try {
      // Change Connection State
      connectionState = TMIConnectionState.CONNECTING;
      // Recreate Socket if state does not equal CREATED
      createWebSocket();
      // Connect to IRC WebSocket
      this.webSocket.connect();
    } catch (Exception ex) {
      log.error("Connection to Twitch IRC failed: {} - Retrying ...", ex.getMessage());
      // Sleep 1 second before trying to reconnect
      try {
        Thread.sleep(1000);
      } catch (Exception et) {
      }
      // reconnect
      reconnect();
    }
  }
}

代码示例来源:origin: twitch4j/twitch4j

/**
 * Connecting to IRC-WS
 */
@Synchronized
public void connect() {
  if (connectionState.equals(TMIConnectionState.DISCONNECTED) || connectionState.equals(TMIConnectionState.RECONNECTING)) {
    try {
      // Change Connection State
      connectionState = TMIConnectionState.CONNECTING;
      // Recreate Socket if state does not equal CREATED
      createWebSocket();
      // Connect to IRC WebSocket
      this.webSocket.connect();
    } catch (Exception ex) {
      log.error("PubSub: Connection to Twitch PubSub failed: {} - Retrying ...", ex.getMessage());
      // Sleep 1 second before trying to reconnect
      try {
        Thread.sleep(1000);
      } catch (Exception et) {
      }
      // reconnect
      reconnect();
    }
  }
}

代码示例来源:origin: net.dv8tion/JDA

.connect();

代码示例来源:origin: io.github.sac/SocketclusterClientJava

ws.connect();
} catch (OpeningHandshakeException e) {

代码示例来源:origin: net.dv8tion/JDA

private void connect(String url)
{
  WebSocketFactory factory = new WebSocketFactory();
  if (proxy != null)
  {
    ProxySettings settings = factory.getProxySettings();
    settings.setHost(proxy.getHostName());
    settings.setPort(proxy.getPort());
  }
  try
  {
    socket = factory.createSocket(url)
        .addHeader("Accept-Encoding", "gzip")
        .addListener(this);
    socket.connect();
  }
  catch (IOException | WebSocketException e)
  {
    //Completely fail here. We couldn't make the connection.
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: blockchain/Android-Merchant-App

mConnection.connect();

代码示例来源:origin: Javacord/Javacord

websocket.addListener(new WebSocketLogger());
  waitForIdentifyRateLimit();
  websocket.connect();
} catch (Throwable t) {
  logger.warn("An error occurred while connecting to websocket", t);

代码示例来源:origin: stackoverflow.com

ws.addListener(new WSListener());
  ws.addExtension(WebSocketExtension.parse(WebSocketExtension.PERMESSAGE_DEFLATE));
  ws.connect();
} catch (Exception e) {
  e.printStackTrace();

相关文章