This article works with the simple chat room implemented using Silverlight4 and Net. tcp of Wcf4. The same demo only adds a Shared Whiteboard function.
The Shared Whiteboard uses Json to serialize the properties of the control, and then uses net. the tcp method is sent to the server, and then the server uses the "push" method to broadcast the serialized attributes.
For the user "user-1", use the "pen" to draw a picture. At the same time, user-2 and user-3 receive the same information and display it in the drawing.
In this example, all attributes of an object are serialized and transmitted in json. I have written the serialization and deserialization methods as extension methods. As follows:
Json serialization
1 # region Json serialization
2 public static class JsonSerializerHelper
3 {
4 /// <summary>
5 // Add an extension method as a string
6 /// </summary>
7 // <typeparam name = "T"> expected type </typeparam>
8 /// <param name = "json"> JSON string data </param>
9 /// <returns> deserialized object diagram </returns>
10 public static T JsonDeserialize <T> (this string json)
11 {
12 using (var mstream = new MemoryStream (Encoding. Unicode. GetBytes (json )))
13 {
14 var serializable = new DataContractJsonSerializer (typeof (T ));
15 return (T) serializable. ReadObject (mstream );
16}
17}
18
19 /// <summary>
20 // serialize the JSON string of the object
21 /// </summary>
22 // <param name = "obj"> Object serialization </param>
23 // <returns> serialized string </returns>
24 public static string JsonSerialize (this object obj)
25 {
26 using (var mstream = new MemoryStream ())
27 {
28 var serializable = new DataContractJsonSerializer (obj. GetType ());
29 serializable. WriteObject (mstream, obj );
30 mstream. Position = 0;
31 using (var reader = new StreamReader (mstream ))
32 {
33 return reader. ReadToEnd ();
34}
35}
36}
37}
38 # endregion
Other functions are easy to implement. I will not explain them too much here. Please download the demo and study it yourself. If you have any questions, leave a message to me.
/Files/homezzm/demo and Shared Whiteboard .rar