private void SocketReceiveLoop() { while (true) { if (!isConnected) { continue; } // receive data from server var recvData = new byte[MAX_DATA_SIZE]; var recvLen = socket.Receive(recvData); if (recvLen == 0) { // lost connection Debug.Log("lost connection!!!"); socket.Close(); socket = null; _isConnected = false; continue; } else { // write receive data to buffer var recvStr = Encoding.ASCII.GetString(recvData, 0, recvLen); // Debug.Log("[receive data]: " + recvStr); Message recvMessage = null; try { recvMessage = JsonUtility.FromJson <Message>(recvStr); } catch { Debug.Log("[Bad Mesaage]: " + recvStr); continue; } receiveBuffer.Write(recvMessage); } } }
private void SocketReceiveLoop() { Connect(); while (true) { try { // receive data from server var recvData = new byte[MAX_DATA_SIZE]; var recvLen = socket.Receive(recvData); if (recvLen == 0) { // lost connection _isConnected = false; Connect(); continue; } else { // write receive data to buffer var recvStr = Encoding.ASCII.GetString(recvData, 0, recvLen); foreach (var msg in PDUParser.ParsePDU(recvStr)) { receiveBuffer.Write(msg); } } } catch { _isConnected = false; Connect(); continue; } } }