JsonWebSocketSession.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace SuperWebSocket
  6. {
  7. /// <summary>
  8. /// Json websocket session
  9. /// </summary>
  10. public class JsonWebSocketSession : JsonWebSocketSession<JsonWebSocketSession>
  11. {
  12. }
  13. /// <summary>
  14. /// Json websocket session
  15. /// </summary>
  16. /// <typeparam name="TWebSocketSession">The type of the web socket session.</typeparam>
  17. public class JsonWebSocketSession<TWebSocketSession> : WebSocketSession<TWebSocketSession>
  18. where TWebSocketSession : JsonWebSocketSession<TWebSocketSession>, new()
  19. {
  20. private const string m_QueryTemplate = "{0} {1}";
  21. private string GetJsonMessage(string name, object content)
  22. {
  23. if(content.GetType().IsSimpleType())
  24. return string.Format(m_QueryTemplate, name, content);
  25. else
  26. return string.Format(m_QueryTemplate, name, AppServer.JsonSerialize(content));
  27. }
  28. /// <summary>
  29. /// Sends the json message.
  30. /// </summary>
  31. /// <param name="name">The name.</param>
  32. /// <param name="content">The content.</param>
  33. public void SendJsonMessage(string name, object content)
  34. {
  35. this.Send(GetJsonMessage(name, content));
  36. }
  37. }
  38. }