/// <summary> /// Callback for the OnConnect method /// Recieves initial data about the names of the initial spreadsheets /// </summary> /// <param name="state"></param> private void ReceiveSpreadsheetNames(SocketState state) { // check to see if we still have a connection if (state.ErrorOccured) { // inform the view if we have an error Error("Lost connection to server in recieve spreadsheet names. Message: " + state.ErrorMessage); return; } // startup: show the client the different spreadsheets // get the Json information string jsonInfo = state.GetData(); // split it into actual messages string[] parts = Regex.Split(jsonInfo, @"(?<=[\n])"); //string test = "Broadcast\nBlouse\nBeachhouse\n\n"; //string[] tester = Regex.Split(test, @"(?<=[\n])"); foreach (string p in parts) { // ignore empty strings added by the regex splitter if (p.Length == 0) { continue; } // The regex splitter will include the last string even if it doesn't end with a '\n', // So we need to ignore it if this happens. if (p[p.Length - 1] != '\n') { break; } // if we have recieved all names if (p.Equals("\n")) { state.OnNetworkAction = ReceiveUpdates; state.RemoveData(0, p.Length); continue; } // remove the data we just processed from the state's buffer state.RemoveData(0, p.Length); Names(p); } //starting another recieve data event loop // if we have recieved all of the names, change the network callback //state.OnNetworkAction = ReceiveSpreadsheetData; Networking.GetData(state); }
/// <summary> /// Parses the Json information recieved from the server /// </summary> /// <param name="state"></param> private void ProcessUpdates(SocketState state) { // get the Json information string jsonInfo = state.GetData(); // split it into actual messages string[] parts = Regex.Split(jsonInfo, @"(?<=[\n])"); // loop until we have processed all received data. foreach (string p in parts) { // ignore empty strings added by the regex splitter if (p.Length == 0) { continue; } // The regex splitter will include the last string even if it doesn't end with a '\n', // So we need to ignore it if this happens. if (p[p.Length - 1] != '\n') { break; } // deserialize our message, and let the view know try { MessageType m = JsonConvert.DeserializeObject <MessageType>(p); Update(m); } catch (Newtonsoft.Json.JsonSerializationException) { char[] h = { '\n' }; if (Int32.TryParse(p.TrimEnd(h), out int id)) { // we have recieved our user ID. } else { Error("Invalid Message from server"); } } // remove the data we just processed from the state's buffer state.RemoveData(0, p.Length); } }