/// <summary> /// Sends a DT message to the client /// </summary> /// <typeparam name="T"></typeparam> /// <param name="opcode"></param> /// <param name="payload"></param> public void SendDTMessage <T>(DTCMessageOpcode opcode, T payload) { //Create the message and send it DTCMessage <T> msg = new DTCMessage <T> { opcode = opcode, payload = payload }; //Send SendMessage(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(msg))); }
/// <summary> /// Called when we download a message. We'll need to decode it. /// </summary> /// <param name="data"></param> /// <param name="length"></param> public override void OnMessageReceived(byte[] data, int length) { try { //Get this as a string string content = Encoding.UTF8.GetString(data, 0, length); //First, decode as a basic message so that we can get the opcode DTCMessageOpcode opcode = JsonConvert.DeserializeObject <DTCMessage <DTCMessagePayload> >(content).opcode; //Choose outcome of this switch (opcode) { case DTCMessageOpcode.SetServer: HandleSetServerMsg(GetMessagePayload <DTCPayloadSetServer>(content)); break; case DTCMessageOpcode.AddTileSubscription: HandleAddTileSubscription(GetMessagePayload <DTCPayloadAddTileSubscription>(content)); break; case DTCMessageOpcode.RemoveTileSubscription: HandleRemoveTileSubscription(GetMessagePayload <DTCPayloadRemoveTileSubscription>(content)); break; } } catch (Exception ex) { Console.WriteLine(ex.Message + ex.StackTrace); } }