You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Instructions/20480C_MOD13_LAB_MANUAL.md
+47-47Lines changed: 47 additions & 47 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@
6
6
7
7
During conference sessions, attendees may wish to ask questions. Distributing microphones among members of the audience can be problematic and slow, so you have been asked to add a page to the website that enables attendees to submit questions. Speakers can either respond immediately or later, depending on the nature of the questions and the session.
8
8
9
-
On the website, all questions must be displayed in real-time without reloading the page, so that all attendees and the speaker can see what has been asked. To support this requirement, a web socket server has been created. You need to update the web application to send the details of questions to the socket server, and also to receive and display questions submitted by other attendees.
9
+
On the website, all questions must be displayed in real-time without reloading the page, so that all attendees and the speaker can see what has been asked. To support this requirement, a web socket server has been created. You need to update the web application to send the details of the questions to the socket server, and also to receive and display questions submitted by other attendees.
10
10
11
11
>**Note:** The web socket server is implemented by using ASP.NET and C#. The details of how this server works are outside the scope of this lab.
In this exercise, you will review the new **Live** page and JavaScript. You will write JavaScript that creates a web socket and connect the socket to the server. Then you will handle messages received from the web socket. You will parse the JSON serialized messages into objects that contain new questions to display on the page. Finally, you will run the application, view the **Live** page and verify that it displays the questions sent by the socket server.
30
+
In this exercise, you will review the new **Live** page and JavaScript. You will write JavaScript that creates a web socket and connects the socket to the server. Then you will handle messages received from the web socket. You will parse the JSON serialized messages into objects that contain new questions to display on the page. Finally, you will run the application, view the **Live** page and verify that it displays the questions sent by the socket server.
31
31
32
-
#### Task 1: Review the Live page.
32
+
#### Task 1: Review the Live page
33
33
34
-
1. Start Visual Studio and open the **ContosoConf.sln** solution from the **Allfiles\Mod13\Labfiles\Starter\Exercise 1** folder.
35
-
2. Start the application and view the **Live** page. Ignore the error that occurs (this error happens because the JavaScript code for the page is not yet complete).
34
+
1. Start Microsoft Visual Studio, and then from the **Allfiles\Mod13\Labfiles\Starter\Exercise 1** folder, open the **ContosoConf.sln** solution.
35
+
2. Start the application, and then view the **Live** page. Ignore the error that occurs (this error happens because the JavaScript code for the page is not yet complete).
36
36
37
37
Using this page, an attendee can type a question and click **Ask** to send it to the presenter. All questions asked by all attendees will appear on this page, underneath the **Ask a question** box.
38
38
@@ -51,30 +51,30 @@ Using this page, an attendee can type a question and click **Ask** to send it to
51
51
<!-- Questions will be displayed here when received by the web socket. -->
52
52
</ul>
53
53
```
54
-
6. Also, note the **<script>** element referencing the **live.js** file in the **/scripts/pages** folder:
54
+
6. Also, in the **/scripts/pages** folder, note the **<script>** element referencing the **live.js** file:
7. Open the **live.js**file that is in the **/scripts/pages**folder and review the code. The JavaScript code in this file defines a **LivePage** object, which controls the page. The code to manipulate the user interface and respond to DOM events has already been written:
58
+
7. From the **/scripts/pages**folder, open the **live.js**file, and then review the code. The JavaScript code in this file defines a **LivePage** object, which controls the page. The code to manipulate the user interface and respond to DOM events has already been written:
59
59
```javascript
60
60
var LivePage = Object.inherit({
61
61
...
62
62
})
63
63
```
64
64
65
-
#### Task 2: Receive messages by using a web socket.
65
+
#### Task 2: Receive messages by using a web socket
66
66
67
67
1. In the **live.js** file near the end of the file (just before the call to the **create()** method), find the following comment:
68
68
```javascript
69
69
// TODO: Create a web socket connection to ws://localhost:[port]/live/socket.ashx
70
70
```
71
-
- The web server that handles requests sent to the web socket is located at the URL localhost:[port]/live/socket.ashx.
71
+
- The web server that handles requests sent to the web socket is located at **localhost:[port]/live/socket.ashx**.
72
72
2. After this comment, add code to create a new **WebSocket** object that connects to the socket server at **localhost:[port]/live/socket.ashx**.
73
73
74
74
>**Note:** The socket is stored in the **LivePage** object as **this.socket**, so methods of **LivePage** can use it.
75
75
76
-
3. Find the **initializeSocket()**function in the **LivePage**object.
77
-
4. In the **initializeSocket()** function, after the comment // TODO: Assign a callback to handle messages from the socket, assign a callback function to the **onmessage** property of the socket. As follows:
76
+
3. In the **LivePage**object, find the **initializeSocket()**function.
77
+
4. In the **initializeSocket()** function, after the __// TODO: Assign a callback to handle messages from the socket__ comment, assign a callback function to the **onmessage** property of the socket as follows:
@@ -84,7 +84,7 @@ Using this page, an attendee can type a question and click **Ask** to send it to
84
84
>var o = {data :1}; // object
85
85
>
86
86
>...
87
-
>functionf(a) = {
87
+
>functionf(a) = {
88
88
>returnthis.data+ b;
89
89
> }; // function
90
90
>...
@@ -111,12 +111,12 @@ Using this page, an attendee can type a question and click **Ask** to send it to
111
111
112
112
>**Note:** You will implement the commented code for the **handleSocketMessage** method in the next step.
113
113
114
-
#### Task 3: Display questions received as messages.
114
+
#### Task 3: Display questions received as messages
115
115
116
-
1. The **handleSocketMessage** method receives incoming messages from the web socket. The message data is stored in the **event** parameter, but it is serialized as a JSON string. In this method, add JavaScript code to perform the following tasks:
117
-
- After the comment // TODO: Parse the event data into message object, parse the JSON message into a JavaScript object named **message**.
116
+
1. The **handleSocketMessage** method receives incoming messages from the web socket. The message data is stored in the **event** parameter, but it is serialized as a JSON string. In this method, add a JavaScript code to perform the following tasks:
117
+
- After the __// TODO: Parse the event data into message object__ comment, parse the JSON message into a JavaScript object named **message**.
118
118
- Modify the statement that calls the **handlQuestionsMessage()** function and only run this statement if the **message** variable contains a **questions** property.
119
-
2. Review the **displayQuestion** method:
119
+
2. Review the **displayQuestion** method.
120
120
```javascript
121
121
displayQuestion:function (question) {
122
122
var item =this.createQuestionItem(question);
@@ -136,26 +136,26 @@ This method adds the questions specified as the parameter to the list below the
136
136
137
137
#### Task 4: Test the application.
138
138
139
-
1. Run the application and view the **Live** page. The server-side web socket will send a series of test questions.
139
+
1. Run the application, and then view the **Live** page. The server-side web socket will send a series of test questions.
140
140
2. Verify that the following four questions appear on the page over a period of a few seconds.
141
-
- What are some good resources for getting started with HTML5?
142
-
- Can you explain more about the Web Socket API, please?
143
-
- This is an #&!%!* inappropriate message!!
144
-
- How much of CSS3 can I use right now?
141
+
- **What are some good resources for getting started with HTML5?**
142
+
- **Can you explain more about the Web Socket API, please?**
143
+
- **This is an #&!%!* inappropriate message!!**
144
+
- **How much of CSS3 can I use right now?**
145
145
3. Close Microsoft Edge.
146
146
147
-
>**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.
147
+
>**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.
148
148
149
-
### Exercise 2: Sending Messages to a Web Socket.
149
+
### Exercise 2: Sending Messages to a Web Socket
150
150
151
151
#### Scenario
152
152
153
-
In this exercise, you will create a message object that contains a question to ask. You will serialize this message and send it to the server by using the web socket. Then you will run the application, view two concurrent instances of the **Live** page, and verify that asking questions results in them being displayed on the page in both instances.
153
+
In this exercise, you will create a message object that contains a question. You will serialize this message and send it to the server by using the web socket. Then you will run the application, view two concurrent instances of the **Live** page and verify that asking questions results in them being displayed on the page in both instances.
154
154
155
-
#### Task 1: Format a question as a message.
155
+
#### Task 1: Format a question as a message
156
156
157
-
1. Open the **ContosoConf** solution in the **Allfiles\Mod13\Labfiles\Starter\Exercise 2** folder.
158
-
2. Open the **live.js** file in the **scripts/pages** folder. In this file, the **LivePage** object has methods that handle the **Ask a question** form submission. The question text is passed to the **askQuestion** method, which looks like this:
157
+
1. From the **Allfiles\Mod13\Labfiles\Starter\Exercise 2** folder, open the **ContosoConf** solution.
158
+
2. From the **scripts/pages** folder, open the **live.js** file. In this file, the **LivePage** object has methods that handle the **Ask a question** form submission. The question text is passed to the **askQuestion** method, which looks like this:
159
159
```javascript
160
160
askQuestion: function (text) {
161
161
// TODO: Create a message object with the format { ask: text }
@@ -168,41 +168,41 @@ In this exercise, you will create a message object that contains a question to a
168
168
this.questionInput.value="";
169
169
}
170
170
```
171
-
3. In the **askQuestion** method, after the comment // TODO: Create a message object with the format { ask: text }, create a variable named **message** that contains an object with the following format (this is the message structure expected by the socket server):
171
+
3. In the **askQuestion** method, after the __// TODO: Create a message object with the format { ask: text }__ comment, create a variable named **message** that contains an object with the following format (this is the message structure expected by the socket server):
172
172
```javascript
173
173
{ ask: text }
174
174
```
175
-
4. After the comment // TODO: Convert the message object into a JSON string, serialize the **message** variable as a JSON string.
175
+
4. After the __// TODO: Convert the message object into a JSON string__ comment, serialize the **message** variable as a JSON string.
176
176
- Use the **JSON.stringify()**function.
177
177
178
-
#### Task 2: Send the message to the web socket.
178
+
#### Task 2: Send the message to the web socket
179
179
180
-
1. In the **askQuestion** method, after the comment // TODO: Send the message to the socket, send the JSON serialized message to the web socket.
180
+
In the **askQuestion** method, after the __// TODO: Send the message to the socket__ comment, send the JSON serialized message to the web socket.
181
181
- Use the **send()** function of the **socket** variable.
182
182
183
-
#### Task 3: Test the application.
183
+
#### Task 3: Test the application
184
184
185
-
1. Run the application and view the **Live** page.
186
-
2. Open a second tab in Microsoft Edge and view the **Live** page again.
185
+
1. Run the application, and then view the **Live** page.
186
+
2. Open a second tab in Microsoft Edge, and then view the **Live** page again.
187
187
3. Use the **Ask a question** form to submit questions.
188
188
4. Verify that the questions are displayed on the pages in both tabs.
189
189
190
-
>**Note:** The message **This is an #&!%!* inappropriate message** might disappear from the second tab.
190
+
>**Note:** The **This is an #&!%!* inappropriate message** message might disappear from the second tab.
191
191
192
192
5. Close Microsoft Edge.
193
193
194
194
>**Result:** 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.
195
195
196
-
### Exercise 3: Handling Different Web Socket Message Types.
196
+
### Exercise 3: Handling Different Web Socket Message Types
197
197
198
198
#### Scenario
199
199
200
-
In this exercise, you will add a link next to each question to enable a student to report the question as inappropriate. Then you will handle messages from the server instructing the page to remove a question; this process will involve handling different types of messages. Finally, you will run the application, view the **Live** page, and verify that clicking the link causes the question to be removed.
200
+
In this exercise, you will add a link next to each question to enable a student to report the question as inappropriate. Then you will handle messages from the server instructing the page to remove a question; this process will involve handling different types of messages. Finally, you will run the application, view the **Live** page, and then verify that clicking the link causes the question to be removed.
201
201
202
-
#### Task 1: Display report links.
202
+
#### Task 1: Display report links
203
203
204
-
1. Open the **ContosoConf** solution in the **Allfiles\Mod13\Labfiles\Starter\Exercise 3** folder.
205
-
2. Open the **live.js** file in the **scripts/pages** folder, and review the **createReportLink** method:
204
+
1. From the **Allfiles\Mod13\Labfiles\Starter\Exercise 3** folder, open the **ContosoConf** solution.
205
+
2. From the **scripts/pages** folder, open the **live.js** file, and then review the **createReportLink** method:
206
206
```javascript
207
207
createReportLink: function () {
208
208
var report =document.createElement("a");
@@ -213,13 +213,13 @@ In this exercise, you will add a link next to each question to enable a student
213
213
}
214
214
```
215
215
This method creates a newlink element that enables a user to report a question to the moderator, and adds attributes that cause the link to be rendered appropriately.
216
-
3. Uncomment the following line of the **displayQuestion** method:
216
+
3. Uncomment the following line of the **displayQuestion** method:
217
217
```javascrit
218
218
//item.appendChild(this.createReportLink());
219
219
```
220
220
- This statement calls the **createReportLink()**function to add the **Report** link next to each question.
221
221
222
-
#### Task 2: Send the report message.
222
+
#### Task 2: Send the report message
223
223
224
224
1. When a **Report** link is clicked, the **handleQuestionsClick** event handler changes the text of the link to **Reported**, and then calls the **reportQuestion** method:
225
225
```javascript
@@ -234,13 +234,13 @@ This method creates a new link element that enables a user to report a question
234
234
}
235
235
}
236
236
```
237
-
In the **reportQuestion()**function, after the comment // TODO: Send socket message { report: questionId }, add code to send the socket a message with the following format (this message specifies the ID of the question being reported):
237
+
2.In the **reportQuestion()**function, after the __// TODO: Send socket message { report: questionId }__ comment, add code to send the socket a message with the following format (this message specifies the ID of the question being reported):
238
238
```javascript
239
239
{ report: questionId }
240
240
```
241
241
- Use the **JSON.stringify()** function to serialize the message.
242
242
243
-
#### Task 3: Handle Remove Question messages.
243
+
#### Task 3: Handle the Remove Question messages
244
244
245
245
1. Find the **handleRemoveMessage()** function:
246
246
```javascript
@@ -257,11 +257,11 @@ In the **reportQuestion()** function, after the comment // TODO: Send socket mes
257
257
This function removes all messages that contain the **remove** property from the displayed list of questions; the socket server attaches this property to all messages that should no longer be displayed.
258
258
2. Update the **handleSocketMessage()** function to call the **handleRemoveMessage()** function for all messages that have the **remove** property.
259
259
260
-
#### Task 4: Test the application.
260
+
#### Task 4: Test the application
261
261
262
-
1. Run the application and view the **Live** page.
262
+
1. Run the application, and then view the **Live** page.
263
263
2. Verify that **Report** links are displayed next to questions.
264
-
3. Click the **Report** link next to the question **What are some good resources for getting started with HTML5?**
264
+
3. Click the **Report** link next to the **What are some good resources for getting started with HTML5?** question.
265
265
4. Verify that the question disappears from the page (there will be a short delay while the question is assessed).
0 commit comments