|
1 | | -# Module 13: Implementing Real-time Communication by Using Web Sockets. |
| 1 | +# Module 13: Implementing Real-time Communication by Using Web Sockets |
2 | 2 |
|
3 | | -# Lab: Performing Real-time Communication by Using Web Sockets. |
| 3 | +# Lab: Performing Real-time Communication by Using Web Sockets |
4 | 4 |
|
5 | 5 | ### Lab Setup |
6 | 6 |
|
7 | 7 | ### Preparation Steps |
8 | 8 |
|
9 | | -1. Ensure that you have cloned the 20480C directory from GitHub. It contains the code segments for this course's labs and demos. https://github.com/MicrosoftLearning/20480-Programming-in-HTML5-with-JavaScript-and-CSS3/tree/master/Allfiles. |
| 9 | +Ensure that you have cloned the 20480C directory from GitHub (https://github.com/MicrosoftLearning/20480-Programming-in-HTML5-with-JavaScript-and-CSS3/tree/master/Allfiles). It contains the code segments for the labs and demos in this course. |
10 | 10 |
|
11 | | -### Exercise 1: Receiving Messages from a Web Socket. |
| 11 | +### Exercise 1: Receiving Messages from a Web Socket |
12 | 12 |
|
13 | | -#### Task 1: Review the Live page. |
| 13 | +#### Task 1: Review the Live page |
14 | 14 |
|
15 | | -1. Open **Visual Studio 2017**. |
| 15 | +1. Open **Microsoft Visual Studio 2017**. |
16 | 16 | 2. In Visual Studio, on the **File** menu, point to **Open**, and then click **Project/Solution**. |
17 | 17 | 3. In the **Open Project** dialog box, browse to **Allfiles\Mod13\Labfiles\Starter\Exercise 1**, click **ContosoConf.sln**, and then click **Open**. |
18 | 18 | 4. On the **Debug** menu, click **Start Without Debugging**. |
19 | | -5. In Microsoft Edge, if the message **Intranet settings are turned off by default** appears, click **Don’t show this message again**. |
| 19 | +5. In Microsoft Edge, if the **Intranet settings are turned off by default** message appears, click **Don’t show this message again**. |
20 | 20 | 6. Click **Live**. |
21 | 21 | 7. In the **Webpage Error** dialog box, click **No**. |
22 | 22 | 8. Verify that the **Live** page looks like the following image: |
23 | 23 |
|
24 | 24 |  |
25 | 25 |
|
26 | 26 | 9. Close Microsoft Edge and return to Visual Studio. |
27 | | -10. In Solution Explorer, expand the **ContosoConf** project, and then double-click **live.htm**. |
| 27 | +10. In **Solution Explorer**, expand the **ContosoConf** project, and then double-click **live.htm**. |
28 | 28 | 11. Verify that this page contains the following HTML markup: |
29 | 29 | ```html |
30 | 30 | <form action="#"> |
|
40 | 40 | ```html |
41 | 41 | <script src="/scripts/pages/live.js" type="text/javascript"></script> |
42 | 42 | ``` |
43 | | -13. In Solution Explorer, expand the **scripts** folder, expand the **pages** folder, and then double-click **live.js**. |
| 43 | +13. In **Solution Explorer**, expand the **scripts** folder, expand the **pages** folder, and then double-click **live.js**. |
44 | 44 | 14. Verify that the file contains the following JavaScript code: |
45 | | - ```javascript |
| 45 | + ``` |
| 46 | + javascript |
46 | 47 | var LivePage = Object.inherit({ |
47 | 48 | ``` |
48 | 49 |
|
49 | | -#### Task 2: Receive messages by using a web socket. |
| 50 | +#### Task 2: Receive messages by using a web socket |
50 | 51 |
|
51 | 52 | 1. In **live.js**, find the following comment near the end of the file: |
52 | 53 | ```javascript |
|
56 | 57 | ```javascript |
57 | 58 | var socket = new WebSocket("ws://localhost:[port]/live/socket.ashx"); |
58 | 59 | ``` |
59 | | -3. Scroll to the top of the file and find the following comment: |
| 60 | +3. Scroll to the top of the file and find the following comment: |
60 | 61 | ```javascript |
61 | 62 | // TODO: Assign a callback to handle messages from the socket. |
62 | 63 | ``` |
|
65 | 66 | this.socket.onmessage = this.handleSocketMessage.bind(this); |
66 | 67 | ``` |
67 | 68 |
|
68 | | -#### Task 3: Display questions received as messages. |
| 69 | +#### Task 3: Display questions received as messages |
69 | 70 |
|
70 | 71 | 1. In **live.js**, find the following comment: |
71 | 72 | ```javascript |
|
79 | 80 | ```javascript |
80 | 81 | // TODO: Check if message has a `questions` property, before calling handleQuestionsMessage |
81 | 82 | ``` |
82 | | -4. Modify the statement following this comment, as shown below in bold: |
| 83 | +4. Modify the statement following this comment, as shown below: |
83 | 84 | ```javascript |
84 | 85 | if (message.questions) { |
85 | 86 | this.handleQuestionsMessage(message); |
|
93 | 94 | this.questionListElement.appendChild(item); |
94 | 95 | } |
95 | 96 | ``` |
96 | | -6. Find the following comment in the **handleQuestionsMessage()** function: |
| 97 | +6. Find the following comment in the **handleQuestionsMessage()** function: |
97 | 98 | ```javascript |
98 | 99 | // TODO: Display each question in the page, using the displayQuestion function. |
99 | 100 | ``` |
|
104 | 105 | } |
105 | 106 | ``` |
106 | 107 |
|
107 | | -#### Task 4: Test the application. |
| 108 | +#### Task 4: Test the application |
108 | 109 |
|
109 | 110 | 1. In Solution Explorer, double-click **live.htm**. |
110 | 111 | 2. On the **Debug** menu, click **Start Without Debugging**. |
111 | 112 | 3. In Microsoft Edge, once the page has loaded, wait five seconds. |
112 | 113 | 4. Verify that the page displays the following four questions: |
113 | | -- What are some good resources for getting started with HTML5? |
114 | | -- Can you explain more about the Web Socket API, please? |
115 | | -- This is an #&!%!* inappropriate message!! |
116 | | -- How much of CSS3 can I use right now? |
| 114 | +- **What are some good resources for getting started with HTML5?** |
| 115 | +- **Can you explain more about the Web Socket API, please?** |
| 116 | +- **This is an #&!%!* inappropriate message!!** |
| 117 | +- **How much of CSS3 can I use right now?** |
117 | 118 | 5. Close Microsoft Edge. |
118 | 119 |
|
119 | | ->**Results:** After completing this exercise, you will have added JavaScript code to the **Live** web page to receive questions from a web socket and to display them. |
| 120 | +>**Results:** After completing this exercise, you will have added a JavaScript code to the **Live** web page to receive questions from a web socket and to display them. |
120 | 121 |
|
121 | | -### Exercise 2: Sending Messages to a Web Socket. |
| 122 | +### Exercise 2: Sending Messages to a Web Socket |
122 | 123 |
|
123 | | -#### Task 1: Format a question as a message. |
| 124 | +#### Task 1: Format a question as a message |
124 | 125 |
|
125 | 126 | 1. In Visual Studio, on the **File** menu, point to **Open**, and then click **Project/Solution**. |
126 | 127 | 2. In the **Open Project** dialog box, browse to **Allfiles\Mod13\Labfiles\Starter\Exercise 2**, click **ContosoConf.sln**, and then click **Open**. |
127 | | -3. In Solution Explorer, expand the **ContosoConf** project, expand the **scripts** folder, expand the **pages** folder, and then double-click **live.js**. |
| 128 | +3. In **Solution Explorer**, expand the **ContosoConf** project, expand the **scripts** folder, expand the **pages** folder, and then double-click **live.js**. |
128 | 129 | 4. Verify that the **live.js** file contains the following function: |
129 | 130 | ```javascript |
130 | 131 | askQuestion: function (text) { |
|
157 | 158 | var json = JSON.stringify(message); |
158 | 159 | ``` |
159 | 160 |
|
160 | | -#### Task 2: Send the message to the web socket. |
| 161 | +#### Task 2: Send the message to the web socket |
161 | 162 |
|
162 | 163 | 1. In **live.js**, find the following comment: |
163 | 164 | ```javascript |
|
168 | 169 | this.socket.send(json); |
169 | 170 | ``` |
170 | 171 |
|
171 | | -#### Task 3: Test the application. |
| 172 | +#### Task 3: Test the application |
172 | 173 |
|
173 | 174 | 1. In Solution Explorer, double-click **live.htm**. |
174 | 175 | 2. On the **Debug** menu, click **Start Without Debugging**. |
|
178 | 179 | 6. Verify that the list of questions displays **This is a test.** |
179 | 180 | 7. Click the first Microsoft Edge tab. |
180 | 181 | 8. Verify that the list of questions includes **This is a test.** |
181 | | -9. Close Microsoft Edge. |
| 182 | +9. Close Microsoft Edge. |
182 | 183 |
|
183 | 184 | >**Results:** After completing this exercise, you will have modified the **Live** question page to enable users to ask questions by sending messages to the server by using the web socket. |
184 | 185 |
|
185 | | -### Exercise 2: Handling Different Web Socket Message Types. |
| 186 | +### Exercise 2: Handling Different Web Socket Message Types |
186 | 187 |
|
187 | | -#### Task 1: Display report links. |
| 188 | +#### Task 1: Display report links |
188 | 189 |
|
189 | 190 | 1. In Visual Studio, on the **File** menu, point to **Open**, and then click **Project/Solution**. |
190 | 191 | 2. In the **Open Project** dialog box, browse to **Allfiles\Mod13\Labfiles\Starter\Exercise 3**, click **ContosoConf.sln**, and then click **Open**. |
191 | | -3. In Solution Explorer, expand the **ContosoConf** project, expand the **scripts** folder, expand the **pages** folder, and then double-click **live.js**. |
| 192 | +3. In **Solution Explorer**, expand the **ContosoConf** project, expand the **scripts** folder, expand the **pages** folder, and then double-click **live.js**. |
192 | 193 | 4. In **live.js**, find the following code: |
193 | 194 | ```javascript |
194 | 195 | createReportLink: function () { |
|
203 | 204 | ```javascript |
204 | 205 | //item.appendChild(this.createReportLink()); |
205 | 206 | ``` |
206 | | -6. Modify this statement and remove the // characters, as shown in the following JavaScript code: |
| 207 | +6. Modify this statement and remove the __//__ characters, as shown in the following JavaScript code: |
207 | 208 | ```javascript |
208 | 209 | item.appendChild(this.createReportLink()); |
209 | 210 | ``` |
210 | 211 |
|
211 | | -#### Task 2: Send the report message. |
| 212 | +#### Task 2: Send the report message |
212 | 213 |
|
213 | 214 | 1. In **live.js**, find the following function: |
214 | 215 | ```javascript |
|
232 | 233 | this.socket.send(JSON.stringify({ report: questionId })); |
233 | 234 | ``` |
234 | 235 |
|
235 | | -#### Task 3: Handle Remove Question messages. |
| 236 | +#### Task 3: Handle Remove Question messages |
236 | 237 |
|
237 | 238 | 1. In **live.js**, find the **handleRemoveMessage()** function: |
238 | 239 | ```javascript |
|
258 | 259 | } |
259 | 260 | } |
260 | 261 | ``` |
261 | | -3. Modify the JavaScript code in the **handleSocketMessage** method as shown below in bold: |
| 262 | +3. Modify the JavaScript code in the **handleSocketMessage** method as shown below: |
262 | 263 | ```javascript |
263 | 264 | handleSocketMessage: function (event) { |
264 | 265 | // TODO: Parse the event data into message object. |
|
271 | 272 | } |
272 | 273 | ``` |
273 | 274 |
|
274 | | -#### Task 4: Test the application. |
| 275 | +#### Task 4: Test the application |
275 | 276 |
|
276 | 277 | 1. In Solution Explorer, double-click **live.htm**. |
277 | 278 | 2. On the **Debug** menu, click **Start Without Debugging**. |
|
0 commit comments