WebSocket protocol support long connection

For all based on the service side of the C / S application, http request supports only request - response pattern, too restrictive, the server can not send unsolicited messages to the client, WebSocket protocol can either initiate the message from the server, you can also initiate a request by the client .
HTTP protocol with the WebSocket protocol lifecycle comparison:
WebSocket protocol support long connection
the WebSocket is based on the expansion of the http protocol support long connection. Tomcat7 server requires more
protocol address test request public static final String WEBSOCKET = "ws : //qp.hystudio.cn: 9000";
page testing Address: http://qp.hystudio.cn:9000/service/service.htm
If multiple clients simultaneously using the server supports WebSocket connected as follows:
WebSocket protocol support long connection
WebSocket using procedure
1 / Create a websocket connection object
   mConnection = new new WebSocketConnection ();
2 / create listener inherited WebSocketConnectionHandler
public class WebSocketConnectListener the extends WebSocketConnectionHandler {}
. 3 / and the need to create a server message channel for data exchange and a server, can send and receive data
public void Connect () {
     IF (! isConnect ()) {
= new new WebSocketConnectListener connectListener (context, mConnection, Message, listener);
       the try {
           mConnection.connect (+ a WebSocket path, connectListener);
        } the catch (WebSocketException E) {
           ; e.printStackTrace ()
       }
    }
}
The first 4 / and the server sends a message determining whether the connection requires
public Boolean isConnect () {
        iF (mConnection == null) return to false;
        return mConnection.isConnected ();
    }
test message 5 transmitted / client and server interfaces successful connection
  public void sendTextMessage (sendText, String) {
        iF (mConnection == null) {
            return;
        }
        Connect ();
        mConnection.sendTextMessage (sendText,);
    } Br />. 6 / Because it is based on the http protocol. May also be used as an ordinary request Request
// connection is established
@Override
    public void the onOpen () {
        super.onOpen ();
        LogUtils.d ( "% S", "Connect WebSocketConnectListener");
        connection.sendTextMessage (Message);
}

// Disconnect (typically due to network disconnection, or the request parameter error)
    @Override
    public void the onClose (int code, String reason) {
        super.onClose (code, reason);
        LogUtils.d ( "% S" , "WebSocketConnectListener Lost");
        ! IF (this.listener = null) {
            this.listener.onClose (code, reason);
        }
    }

// receive the message server use
    @Override
    public void onTextMessage (String payload) {
        super.onTextMessage (payload);
        LogUtils.d ( "% S", "payload =" + payload);
        IF (this.listener = null! ) {
            this.listener.onSuccess (payload);
        }
    }

Request as an ordinary manner, http request similar manner. Here custom protocol objects, packages the request parameter request
IRequest tokenRequest new new TokenRequest = (context, App.getInstance (). GetDeviceID (), new new IRequestListener () {
br /> @Override
public void the onSuccess (String payload) {
     TokenResponse Response JsonUtils.fromJson = (payload, TokenResponse.class);
     IF (response.isRequestSuccssful ()) {
       DBAdapter.getInstance () addToken (response.toToken ());.
       . App.getInstance () setToken (response.toToken ()) ;
       setToken (response.toToken () getToken to ().);
       request.request ();
      } the else {
      SnackbarUtils.show ((the Activity) context, R.string.device_unauth);
      }
     }
    @Override
    public void onClose(int code, String reason) {
}
});
tokenRequest.request();

Here the object is only TokenRequest encapsulated protocol request parameters. Detail below br /> @ the Path (Constants.Path.TOKENPATH)
public class TokenRequest the extends IRequest {
    Private String deviceId;
  public TokenRequest (the Context context, String deviceID, IRequestListener listener) {
    Super (context, listener);
        this.deviceId = deviceID;
    }

    @Override
    protected String prapareMsg() {
        return "{" + "deviceId:" + deviceId + "}";
    }
}

Here is a complete protocol based WEBSOCKET normal http request. Its biggest advantage is and server establish a two-way channel, either initiated by a client request, the request can also be initiated by the server. Before self-developed basically push message by the client polling mode, inefficient and wasteful. HTTP request header request is very long, but the server and establish a connection is possible to send a small amount of data, which is taking up server resources. WEBSOCKET is to ensure that as long as the connection is established the server and the client either end of the initiation message, push message each other, but also greatly improves the efficiency and header information between the server and the client is very small, can reduce the server's resources waste.

Guess you like

Origin blog.51cto.com/14473726/2435316