Use the Shared Whiteboard implemented by Net. tcp of Silverlight4 and Wcf4

Source: Internet
Author: User
Tags object serialization

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.