I have an utility class to handle the socket, and in the socket delegate methods i could know the current state of the socket, e.g. didConnected, didReadData and so on. I do not want to write an extra protocol with a delegate to send the socket state indeed.
Actually, there are 2 properties in my utility class, an enum to differentiate the current state of the socket between: didConnected, didReadData, didDisconnected, the other is a Data type value to store the received data from socket. It likes that:
public enum SocketState {
case unknown, didConnected, didReconnectedFailed, didSentHeartbeatPack,
didSentMessage, didReadData, didDisconnected
}
private var currentState: SocketState = .unknown
private var msgData = Data()
After become the delegate of the socket, I have implemented the several methods,
func socket(_ sock: GCDAsyncSocket, didConnectToHost host: String, port: UInt16) {}
func socket(_ sock: GCDAsyncSocket, didWriteDataWithTag tag: Int) {}
func socketDidDisconnect(_ sock: GCDAsyncSocket, withError err: Error?) {}
What I intend to do is while I change the value of the currentState, or I did set the received data to the msgData, in my controller I could capture the 2 values changed simultaneously.