org.eclipse.jetty.websocket
类 WebSocketClient

java.lang.Object
  继承者 org.eclipse.jetty.websocket.WebSocketClient

public class WebSocketClient
extends Object

WebSocketClient allows to create multiple connections to multiple destinations that can speak the websocket protocol.

When creating websocket connections, WebSocketClient accepts a WebSocket object (to receive events from the server), and returns a WebSocket.Connection to send data to the server.

Example usage is as follows:

   WebSocketClientFactory factory = new WebSocketClientFactory();
   factory.start();

   WebSocketClient client = factory.newWebSocketClient();
   // Configure the client

   WebSocket.Connection connection = client.open(new URI("ws://127.0.0.1:8080/"), new WebSocket.OnTextMessage()
   {
     public void onOpen(Connection connection)
     {
       // open notification
     }

     public void onClose(int closeCode, String message)
     {
       // close notification
     }

     public void onMessage(String data)
     {
       // handle incoming message
     }
   }).get(5, TimeUnit.SECONDS);

   connection.sendMessage("Hello World");
 


构造方法摘要
WebSocketClient()
          已过时。 Use WebSocketClientFactory.newWebSocketClient()
WebSocketClient(WebSocketClientFactory factory)
          Creates a WebSocketClient with shared WebSocketClientFactory.
 
方法摘要
 SocketAddress getBindAddress()
           
 Map<String,String> getCookies()
          Returns the map of the cookies that are sent during the initial HTTP handshake that upgrades to the websocket protocol.
 List<String> getExtensions()
           
 WebSocketClientFactory getFactory()
           
 MaskGen getMaskGen()
           
 int getMaxBinaryMessageSize()
           
 int getMaxIdleTime()
           
 int getMaxTextMessageSize()
           
 String getOrigin()
           
 String getProtocol()
           
 Future<WebSocket.Connection> open(URI uri, WebSocket websocket)
          Asynchronously opens a websocket connection and returns a Future to obtain the connection.
 WebSocket.Connection open(URI uri, WebSocket websocket, long maxConnectTime, TimeUnit units)
          Opens a websocket connection to the URI and blocks until the connection is accepted or there is an error.
 void setBindAddress(SocketAddress bindAddress)
           
 void setMaskGen(MaskGen maskGen)
           
 void setMaxBinaryMessageSize(int maxBinaryMessageSize)
          Set the initial maximum binary message size for a connection.
 void setMaxIdleTime(int maxIdleTime)
           
 void setMaxTextMessageSize(int maxTextMessageSize)
          Set the initial maximum text message size for a connection.
 void setOrigin(String origin)
           
 void setProtocol(String protocol)
           
static InetSocketAddress toSocketAddress(URI uri)
           
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

构造方法详细信息

WebSocketClient

@Deprecated
public WebSocketClient()
                throws Exception
已过时。 Use WebSocketClientFactory.newWebSocketClient()

Creates a WebSocketClient from a private WebSocketClientFactory.

This can be wasteful of resources if many clients are created.

抛出:
Exception - if the private WebSocketClientFactory fails to start

WebSocketClient

public WebSocketClient(WebSocketClientFactory factory)

Creates a WebSocketClient with shared WebSocketClientFactory.

参数:
factory - the shared WebSocketClientFactory
方法详细信息

getFactory

public WebSocketClientFactory getFactory()
返回:
The WebSocketClientFactory this client was created with.

getBindAddress

public SocketAddress getBindAddress()
返回:
the address to bind the socket channel to
另请参见:
setBindAddress(SocketAddress)

setBindAddress

public void setBindAddress(SocketAddress bindAddress)
参数:
bindAddress - the address to bind the socket channel to
另请参见:
getBindAddress()

getMaxIdleTime

public int getMaxIdleTime()
返回:
The maxIdleTime in ms for connections opened by this client, or -1 if the default from WebSocketClientFactory.getSelectorManager() is used.
另请参见:
setMaxIdleTime(int)

setMaxIdleTime

public void setMaxIdleTime(int maxIdleTime)
参数:
maxIdleTime - The max idle time in ms for connections opened by this client
另请参见:
getMaxIdleTime()

getProtocol

public String getProtocol()
返回:
The subprotocol string for connections opened by this client.
另请参见:
setProtocol(String)

setProtocol

public void setProtocol(String protocol)
参数:
protocol - The subprotocol string for connections opened by this client.
另请参见:
getProtocol()

getOrigin

public String getOrigin()
返回:
The origin URI of the client
另请参见:
setOrigin(String)

setOrigin

public void setOrigin(String origin)
参数:
origin - The origin URI of the client (eg "http://example.com")
另请参见:
getOrigin()

getCookies

public Map<String,String> getCookies()

Returns the map of the cookies that are sent during the initial HTTP handshake that upgrades to the websocket protocol.

返回:
The read-write cookie map

getExtensions

public List<String> getExtensions()
返回:
The list of websocket protocol extensions

getMaskGen

public MaskGen getMaskGen()
返回:
the mask generator to use, or null if not mask generator should be used
另请参见:
setMaskGen(MaskGen)

setMaskGen

public void setMaskGen(MaskGen maskGen)
参数:
maskGen - the mask generator to use, or null if not mask generator should be used
另请参见:
getMaskGen()

getMaxTextMessageSize

public int getMaxTextMessageSize()
返回:
The initial maximum text message size (in characters) for a connection

setMaxTextMessageSize

public void setMaxTextMessageSize(int maxTextMessageSize)
Set the initial maximum text message size for a connection. This can be changed by the application calling WebSocket.Connection.setMaxTextMessageSize(int).

参数:
maxTextMessageSize - The default maximum text message size (in characters) for a connection

getMaxBinaryMessageSize

public int getMaxBinaryMessageSize()
返回:
The initial maximum binary message size (in bytes) for a connection

setMaxBinaryMessageSize

public void setMaxBinaryMessageSize(int maxBinaryMessageSize)
Set the initial maximum binary message size for a connection. This can be changed by the application calling WebSocket.Connection.setMaxBinaryMessageSize(int).

参数:
maxBinaryMessageSize - The default maximum binary message size (in bytes) for a connection

open

public WebSocket.Connection open(URI uri,
                                 WebSocket websocket,
                                 long maxConnectTime,
                                 TimeUnit units)
                          throws IOException,
                                 InterruptedException,
                                 TimeoutException

Opens a websocket connection to the URI and blocks until the connection is accepted or there is an error.

参数:
uri - The URI to connect to.
websocket - The WebSocket instance to handle incoming events.
maxConnectTime - The interval to wait for a successful connection
units - the units of the maxConnectTime
返回:
A WebSocket.Connection
抛出:
IOException - if the connection fails
InterruptedException - if the thread is interrupted
TimeoutException - if the timeout elapses before the connection is completed
另请参见:
open(URI, WebSocket)

open

public Future<WebSocket.Connection> open(URI uri,
                                         WebSocket websocket)
                                  throws IOException

Asynchronously opens a websocket connection and returns a Future to obtain the connection.

The caller must call Future.get(long, TimeUnit) if they wish to impose a connect timeout on the open.

参数:
uri - The URI to connect to.
websocket - The WebSocket instance to handle incoming events.
返回:
A Future to the WebSocket.Connection
抛出:
IOException - if the connection fails
另请参见:
open(URI, WebSocket, long, TimeUnit)

toSocketAddress

public static InetSocketAddress toSocketAddress(URI uri)


Copyright © 2013. All Rights Reserved.