/// <summary> /// Uses default TwinoModelWriter and WebSocketWriter classes. /// Creates websocket protocol byte array data of serialized model string. /// </summary> public static async Task <byte[]> ToWebSocketUTF8DataAsync(this ISerializableModel model) { WebSocketMessage message = new WebSocketMessage { OpCode = SocketOpCode.UTF8, Content = new MemoryStream(Encoding.UTF8.GetBytes(_twriter.Serialize(model))) }; return(await _writer.Create(message)); }
/// <summary> /// Uses default TwinoModelWriter and WebSocketWriter classes. /// Creates websocket protocol byte array data of serialized model string. /// </summary> public static byte[] ToWebSocketUTF8Data(this ISerializableModel model) { WebSocketMessage message = new WebSocketMessage { OpCode = SocketOpCode.UTF8, Content = new MemoryStream(Encoding.UTF8.GetBytes(_twriter.Serialize(model))) }; return(_writer.Create(message).Result); }
/// <summary> /// Creates serialized string message from T model /// </summary> public string Serialize(ISerializableModel model) { string body; if (model is IPerformanceCriticalModel critical) { LightJsonWriter writer = new LightJsonWriter(); body = writer.Serialize(critical); } else { body = JsonConvert.SerializeObject(model); } return($"[{model.Type},{body}]"); }
/// <summary> /// Reads the string message and if any method is subscribed the model event with On method, they will be called. /// </summary> public virtual void Read(SocketBase client, string message) { int type = Reader.ReadType(message); if (!_descriptors.ContainsKey(type)) { return; } PackageDescriptor descriptor = _descriptors[type]; ISerializableModel model = Reader.Read(descriptor.Type, message, true); if (model == null) { return; } foreach (var action in descriptor.Actions) { action.DynamicInvoke(client, model); } }
public byte[] Prepare(ISerializableModel model) { return(_writer.Create(Serialize(model)).Result); }
public string Serialize(ISerializableModel model) { return(model.Type + "=" + Newtonsoft.Json.JsonConvert.SerializeObject(model)); }