public void onConnect() { Console.WriteLine("Connected"); RougeObject rougeObject = new RougeObject(); rougeObject.putString("username", "bob"); rougeObject.putString("password", "password"); driver.send("login", rougeObject); }
public void onMessage(string command, RougeObject payload) { Console.WriteLine("Received {0} {1}", command, payload.toJson()); if (command.Equals("login")) { RougeObject rougeObject = new RougeObject(); rougeObject.putString("name", "csroom2"); driver.send("room.create", rougeObject); } else if (command.Equals("room.create")) { RougeObject rougeObject = new RougeObject(); rougeObject.putString("name", "csroom2"); rougeObject.putString("message", "hello"); driver.send("room.say", rougeObject); } }
//private RougeType type; public RougeDataWrapper(Object value) { if (value is IDictionary<object, object>) { this.value = new RougeObject((IDictionary<object, object>) value); } else if (value is IDictionary<string, object>) { this.value = new RougeObject((IDictionary<string, object>) value); } else if (value is List<object> || value is JArray) { this.value = new RougeArray((IList<object>)value); } else if (value is JObject) { RougeObject rougeObject = new RougeObject(); foreach ( KeyValuePair<string, JToken> keyPair in ((JObject)value) ) { rougeObject.put(keyPair.Key, keyPair.Value); } this.value = rougeObject; } else if (value is JArray) { RougeArray rougeArray = new RougeArray(); foreach (JToken jToken in ((JArray)value)) { rougeArray.add(value); } this.value = rougeArray; } else { this.value = value; } }
static void testJSonConversion() { RougeObject rougeObject = new RougeObject(); rougeObject.putString("username", "cs"); rougeObject.putString("password", "secret"); rougeObject.putInt("int", 1); rougeObject.putFloat("float", 0.1f); rougeObject.putBoolean("boolean", true); //string jSon = rougeObject.toJSon(); //Console.WriteLine("Rouge Object is {0}", jSon); //valueRougeObject jSonRouge = new RougeObject(jSon); //Console.WriteLine("After Transformation {0}", jSonRouge.toJSon()); }
public void putRougeObject(string key, RougeObject value) { this.content.Add(key, new RougeDataWrapper(value)); }
public void send(string command, RougeObject payload) { Dictionary<string, object> toSend = new Dictionary<string, object>(); toSend.Add("command", command); toSend.Add("payload", payload.toDictionary()); byte[] byteArray = null; if(bEncode) { byteArray = BEncode.Encode(toSend); } else { string stringToSend = JsonConvert.SerializeObject(toSend) + "|" + "\n"; byteArray = Encoding.ASCII.GetBytes(stringToSend); } if (byteArray != null) { try { socket.Send(byteArray); //Console.WriteLine("Sent {0}", System.Text.Encoding.ASCII.GetString(byteArray)); } catch (SocketException e) { Console.WriteLine("Problem sending data"); Console.WriteLine(e.ToString()); socket.Close(); return; } } }
protected void ThreadProc() { System.Text.Encoding enc = System.Text.Encoding.ASCII; while(true) { byte[] receiveBuffer = new byte[this.socket.Available]; int recv = this.socket.Receive(receiveBuffer); if(recv > 0) { if (bEncode) { //Console.WriteLine("Received {0}", enc.GetString(receiveBuffer)); List<IDictionary<string,object>> l = BEncode.Decode(receiveBuffer); foreach(IDictionary<string,object> d in l) { RougeObject rougeObject = new RougeObject(d); if (this.listener != null) { this.listener.onMessage(rougeObject.getString("command"), rougeObject.getRougeObject("payload")); } } } else { string stringBuffer = Regex.Replace( unProcessed + enc.GetString(receiveBuffer), @"\s", "" ); string[] splitBuffer = stringBuffer.Split('|'); for(int i = 0; i < splitBuffer.Length-1; i++) { RougeObject rougeObject = new RougeObject(splitBuffer[i]); if (this.listener != null) { this.listener.onMessage(rougeObject.getString("command"), rougeObject.getRougeObject("payload")); } } if (splitBuffer[splitBuffer.Length-1].Length > 0) { this.unProcessed = splitBuffer[splitBuffer.Length-1]; } else { this.unProcessed = ""; } } Thread.Sleep(50); } } }
public void addRougeObject(RougeObject value) { this.content.Add(new RougeDataWrapper(value)); }