private void TestPost() { var jsonData = new Person() { name = "Hello", age = 1, }; Dictionary <string, string> header = new Dictionary <string, string>(); header["Content-Type"] = "application/json"; var postData = Encoding.UTF8.GetBytes(JsonUtility.ToJson(jsonData)); var request = new HTTPRequest(); request.Post("http://httpbin.org/", "post", (result, data) => { if (result == true) { ColoredLogger.Log(Encoding.ASCII.GetString(data), ColoredLogger.LogColor.Green); } else { ColoredLogger.Log("Request failed: " + request.LastError, ColoredLogger.LogColor.Red); } m_PendingRequests.Remove(request); }, postData, header); //add to pending list m_PendingRequests.Add(request); }
void Update() { if (m_ServerSession.GetRecvedData(m_RecvedData)) { while (m_RecvedData.Count != 0) { var data = m_RecvedData.Dequeue(); ColoredLogger.Log("Msg From User: [" + Encoding.ASCII.GetString(data) + "]", ColoredLogger.LogColor.Yellow); } } }
private void TestGetTimeout() { var request = new HTTPRequest(); request.Get("http://111.111.111.111/", "get", (result, data) => { if (result == false) { ColoredLogger.Log("Request failed: " + request.LastError, ColoredLogger.LogColor.Red); } m_PendingRequests.Remove(request); }); //add to pending list m_PendingRequests.Add(request); }
void Update() { if (m_ServerSession.GetRecvedData(m_RecvedData)) { while (m_RecvedData.Count != 0) { var data = XOR(m_RecvedData.Dequeue(), KEY); MoveToMsg msg = new MoveToMsg(); msg.Unserialize(data); ColoredLogger.Log( "Msg From User: [" + string.Format("PlayerID={0},TargetPosition={1},Speed={2}", msg.PlayerID, msg.TargetPosition.ToString(), msg.Speed) + "]", ColoredLogger.LogColor.Yellow); } } }
private void TestGet() { var request = new HTTPRequest(); request.Get("http://httpbin.org/", "get", (result, data) => { if (result == true) { ColoredLogger.Log(Encoding.ASCII.GetString(data), ColoredLogger.LogColor.Green); } else { ColoredLogger.Log("Request failed: " + request.LastError, ColoredLogger.LogColor.Red); } m_PendingRequests.Remove(request); }, "name", "Hello", "age", 1); //add to pending list m_PendingRequests.Add(request); }
void Start() { m_Request = new HTTPRequest(); m_Request.Get("http://m.ps123.net/sucai/UploadFiles/201103/2011032500295577.jpg", "", (result, data) => { if (result == true) { Texture2D tex = new Texture2D(512, 512); if (ImageConversion.LoadImage(tex, data)) { Renderer.material.mainTexture = tex; } m_IsLoaded = true; } else { ColoredLogger.Log("failed load image: " + m_Request.LastError, ColoredLogger.LogColor.Red); } m_Request = null; }); }
void Start() { if (m_ServerSession.Init("127.0.0.1", 30000)) { m_ServerSession.SetDataHandler((data, dataLen) => { string msg = Encoding.ASCII.GetString(data, 0, dataLen); m_PendingMsg += msg; while (true) { int endOfMsgPos = m_PendingMsg.IndexOf("!"); if (endOfMsgPos >= 0) { string helloMsg = m_PendingMsg.Substring(0, endOfMsgPos + 1); ColoredLogger.Log("Msg From User: [" + helloMsg + "]", ColoredLogger.LogColor.Yellow); m_PendingMsg = m_PendingMsg.Substring(endOfMsgPos + 1); } else { break; } } //ColoredLogger.Log(msg, ColoredLogger.LogColor.Green); }); m_ServerSession.Start(); } if (m_ClientSession.Init("127.0.0.1", 30000)) { m_ClientSession.SetEchoHandler(delegate() { string msg = "Hello, Server!"; ColoredLogger.Log(msg, ColoredLogger.LogColor.Green); return(Encoding.ASCII.GetBytes(msg)); }); m_ClientSession.Start(); } }
void Start() { if (m_ServerSession.Init("127.0.0.1", 30000)) { m_ServerSession.SetDataHandler((data, dataLen) => { MemoryStream recvStream = new MemoryStream(); if (m_PendingStream.Length > 0) { recvStream.Write(m_PendingStream.GetBuffer(), 0, (int)m_PendingStream.Length); m_PendingStream.SetLength(0); } recvStream.Write(data, 0, dataLen); int avaliableCount = (int)recvStream.Length; recvStream.Seek(0, SeekOrigin.Begin); BinaryReader reader = new BinaryReader(recvStream); int headerSize = 4; while (true) { if (avaliableCount < headerSize) { m_PendingStream.SetLength(avaliableCount); reader.Read(m_PendingStream.GetBuffer(), 0, avaliableCount); break; } int len = reader.ReadInt32(); avaliableCount -= headerSize; if (avaliableCount < len) { recvStream.Seek(-headerSize, SeekOrigin.Current); int remainingCount = avaliableCount + headerSize; m_PendingStream.SetLength(remainingCount); reader.Read(m_PendingStream.GetBuffer(), 0, remainingCount); break; } else { byte[] msgBytes = reader.ReadBytes(len); avaliableCount -= len; ColoredLogger.Log("Msg From User: [" + Encoding.ASCII.GetString(msgBytes, 0, len) + "]", ColoredLogger.LogColor.Yellow); } } }); m_ServerSession.Start(); } if (m_ClientSession.Init("127.0.0.1", 30000)) { m_ClientSession.SetEchoHandler(delegate() { string msg = "Hello, Server!"; MemoryStream stream = new MemoryStream(); BinaryWriter writer = new BinaryWriter(stream); byte[] msgBytes = Encoding.ASCII.GetBytes(msg); writer.Write(msgBytes.Length); writer.Write(msgBytes); ColoredLogger.Log(msg, ColoredLogger.LogColor.Green); return(stream.ToArray()); }); m_ClientSession.Start(); } }