public WebSocket connect ( ) throws WebSocketException { // Change the state to CONNECTING. If the state before // the change is not CREATED, an exception is thrown. changeStateOnConnect ( ) ; // HTTP headers from the server. Map < String , List < String > > headers ; try { // Connect to the server. mSocketConnector . connect ( ) ; // Perform WebSocket handshake. headers = shakeHands ( ) ; } catch ( WebSocketException e ) { // Close the socket. mSocketConnector . closeSilently ( ) ; // Change the state to CLOSED. mStateManager . setState ( CLOSED ) ; // Notify the listener of the state change. mListenerManager . callOnStateChanged ( CLOSED ) ; // The handshake failed. throw e ; } // HTTP headers in the response from the server. mServerHeaders = headers ; // Extensions. mPerMessageCompressionExtension = findAgreedPerMessageCompressionExtension ( ) ; // Change the state to OPEN. mStateManager . setState ( OPEN ) ; // Notify the listener of the state change. mListenerManager . callOnStateChanged ( OPEN ) ; // Start threads that communicate with the server. startThreads ( ) ; return this ; }
Connect to the server send an opening handshake to the server receive the response and then start threads to communicate with the server .