private static void Send(Socket handler, Client.Client.MatchDetails content) { using (MemoryStream ms = new MemoryStream()) { BinaryWriter bw = new BinaryWriter(ms); string jsonData = JsonUtility.ToJson(content); bw.Write("CREATEMATCH||" + jsonData); Debug.Log(jsonData); byte[] byteData = ms.ToArray(); handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), handler); } }
private static void ReadCallback(IAsyncResult ar) { ObjectState state = (ObjectState)ar.AsyncState; Socket handler = state.wSocket; int bytesRead = handler.EndReceive(ar); if (bytesRead > 0) { byte[] byteData = state.buffer; string data = null; using (MemoryStream ms = new MemoryStream(byteData)) { BinaryReader br = new BinaryReader(ms); data = br.ReadString(); } string[] dataParts = data.Split(new string[] { "||" }, StringSplitOptions.None); string command = dataParts[0]; string jsonData = dataParts[1]; Debug.Log("Command: " + command + ", Data: " + jsonData); Client.Client.MatchDetails matchDetails = JsonUtility.FromJson <Client.Client.MatchDetails>(jsonData); Debug.Log(matchDetails.usedWords); if (command.IndexOf("CREATEMATCH", StringComparison.Ordinal) > -1) { int id = Database.Database.Instance.ReceiveLatestID() + 1; Debug.Log("ID" + id); if (id == 0) { id = 1; } Database.Database.Instance.PassMatchDetailsToDatabase(id, matchDetails.ConsUsed, matchDetails.Score, matchDetails.AmountWordsFound, matchDetails.Accepted, matchDetails.usedWords); Debug.Log("Read: " + jsonData.Length + "bytes from \n socket Data: " + jsonData); Send(handler, matchDetails); } else { handler.BeginReceive(state.buffer, 0, ObjectState.bufferSize, 0, new AsyncCallback(ReadCallback), state); } } }