You want to do handshaking with a socket server and need to know what the received data’s context is to know how to process it.
Create different constant variables to represent states of the
protocol. Use the constants to map particular processing functions
with the corresponding state. In a
socketData event handler, call the
appropriate function by invoking it through the state map.
A common scenario when connecting to a socket is going through a handshake process. Typically, the server initially sends data to the client. The client then responds to the data in a particular manner, and the server responds again accordingly. This entire process repeats until the handshaking is complete and a “normal” connection is established.
It gets difficult to process the response from the server
socketData event handler does not keep track
of context. That is, there is no “why” sent along with the server
response, or no “this data is in response to” processing directive.
Knowing how to process the response from the server is not usually
something that can be gathered through the response itself, especially
when the response varies. Perhaps one response returns two bytes and
another returns an integer followed by a double. You can begin to see
how this presents itself as a problem.
The solution is to create various state constants to represent the different contexts in which the server sends data to the client. By associating ...