Through two previous articles
Study on the Java-websocket Project (i) description of Java-websocket class diagram
Research on the Java-websocket Project (II.) Try your skill: client connects to server and sends a message instance
Introduction We probably understand the whole project of the class structure, there is an important class: Websocketclient, let's look at the following details of this class
First look at our previous class diagram about websocketclient 's descriptive narrative, can see:
1. Inherit from Websocketadapter
2. Dependent on class Websocketimpl(in fact the WebSocket core code is in class Websocketimpl )
3. Implementation of the WebSocket interface (actually implemented through class Websocketimpl )
Very easy, from the literal meaning we can probably guess websocketadapter is the adapter class, the bridge between Websocketimpl and websocketclient , Websocketimpl is the abbreviation of Web implementation, which means to realize the basic function of websocket really.
Then let's take a look at some of the main methods of websocketclient :
The first is the Connect method
/** * Initiates the WebSocket connection. This method is does not block. */public void Connect () {if (writethread! = null) throw new IllegalStateException ("Websocketclient objects is not Reusea Ble "); writethread = new Thread (this); Writethread.start ();}
We can find:
He actually started a thread because the websocketclient class implements the Runnable interface, so he's going to call the Run method on his own, and then we go further into the run method to get to the bottom.
public void Run () {try {if (socket = = NULL) {socket = new socket (proxy);} else if (socket.isclosed ()) {throw new Ioexc Eption ();} SYSTEM.OUT.PRINTLN ("---->" +uri.tostring () + "Port:" +getport ()), if (!socket.isbound ()) Socket.connect (New INETSOC Ketaddress (Uri.gethost (), Getport ()), connecttimeout); istream = Socket.getinputstream (); ostream = Socket.getoutputstream (); Sendhandshake ();} catch (/*ioexception | SecurityException | unresolvedaddressexception | invalidhandshakeexception | closedbyinterruptexception | Sockettimeoutexception */exception e) {onwebsocketerror (engine, E); Engine.closeconnection (Closeframe.never_ CONNECTED, E.getmessage ()); return;} Writethread = new Thread (new Websocketwritethread ()); Writethread.start (); byte[] Rawbuffer = new byte[WEBSOCKETIMPL.RCV BUF];int readbytes;try {while (!isclosed () && (readbytes = Istream.read (rawbuffer))! =-1) {Engine.decode ( Bytebuffer.wrap (rawbuffer, 0, readbytes));} Engine.eot ();} catch (IOException E) {Engine.eot ();} catch (RuntimeException e) {//This catch case covers internal errors only and indicates a bug in this websocket impleme Ntationonerror (e); Engine.closeconnection (Closeframe.abnormal_close, E.getmessage ());} ASSERT (socket.isclosed ());}
The following is a detailed study of the code above:
Socket = new socket (proxy);
The obvious thing is that a socket socket was created.
Socket.connect (New Inetsocketaddress (Uri.gethost (), Getport ()), connecttimeout);
To create a connection, the number of references is the server address, the port number, and the time-out.
IStream = Socket.getinputstream ();
is to accept the server side of the data, about the specific process of acceptance, I will be in the post-blog to explain, please look forward to.
Ostream = Socket.getoutputstream ();
is used to send data.
Others do not explain, we continue the previous process------connect after the sending of information, that is, call the Send method, the Send method such as the following:
/** * Sends <var>text</var> to the connected WebSocket server. * * @param text * The string which'll be transmitted. */public void Send (String text) throws Notyetconnectedex ception {engine.send (text);}
It calls the engine's send method, then what the engine is, there is a sentence in the class declaration:
Private Websocketimpl engine = null;
Explains theWebsocketimpl classThe operation of the send is implemented.
Finish...
Study on the Java-websocket Project (iii) specific explanation of Websocketclient class