Skip to content

Commit 49b6dde

Browse files
authored
Fix SignalR JS client error handling code snippet line numbers (#17834)
* Fix SignalR JS client error handling code snippet line numbers * more edits * Simplify
1 parent eebbd91 commit 49b6dde

3 files changed

Lines changed: 11 additions & 15 deletions

File tree

aspnetcore/signalr/javascript-client.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ SignalR determines which client method to call by matching the method name and a
138138

139139
Chain a `catch` method to the end of the `start` method to handle client-side errors. Use `console.error` to output errors to the browser's console.
140140

141-
[!code-javascript[Error handling](javascript-client/sample/wwwroot/js/chat.js?range=49-51)]
141+
[!code-javascript[Error handling](javascript-client/sample/wwwroot/js/chat.js?range=50)]
142142

143-
Setup client-side log tracing by passing a logger and type of event to log when the connection is made. Messages are logged with the specified log level and higher. Available log levels are as follows:
143+
Set up client-side log tracing by passing a logger and type of event to log when the connection is made. Messages are logged with the specified log level and higher. Available log levels are as follows:
144144

145145
* `signalR.LogLevel.Error` – Error messages. Logs `Error` messages only.
146146
* `signalR.LogLevel.Warning` – Warning messages about potential errors. Logs `Warning`, and `Error` messages.
@@ -171,7 +171,7 @@ Without any parameters, `withAutomaticReconnect()` configures the client to wait
171171
Before starting any reconnect attempts, the `HubConnection` will transition to the `HubConnectionState.Reconnecting` state and fire its `onreconnecting` callbacks instead of transitioning to the `Disconnected` state and triggering its `onclose` callbacks like a `HubConnection` without automatic reconnect configured. This provides an opportunity to warn users that the connection has been lost and to disable UI elements.
172172

173173
```javascript
174-
connection.onreconnecting((error) => {
174+
connection.onreconnecting(error => {
175175
console.assert(connection.state === signalR.HubConnectionState.Reconnecting);
176176

177177
document.getElementById("messageInput").disabled = true;
@@ -190,7 +190,7 @@ Since the connection looks entirely new to the server, a new `connectionId` will
190190
> The `onreconnected` callback's `connectionId` parameter will be undefined if the `HubConnection` was configured to [skip negotiation](xref:signalr/configuration#configure-client-options).
191191
192192
```javascript
193-
connection.onreconnected((connectionId) => {
193+
connection.onreconnected(connectionId => {
194194
console.assert(connection.state === signalR.HubConnectionState.Connected);
195195

196196
document.getElementById("messageInput").disabled = false;
@@ -220,7 +220,7 @@ async function start() {
220220
If the client doesn't successfully reconnect within its first four attempts, the `HubConnection` will transition to the `Disconnected` state and fire its [onclose](/javascript/api/%40aspnet/signalr/hubconnection#onclose) callbacks. This provides an opportunity to inform users the connection has been permanently lost and recommend refreshing the page:
221221

222222
```javascript
223-
connection.onclose((error) => {
223+
connection.onclose(error => {
224224
console.assert(connection.state === signalR.HubConnectionState.Disconnected);
225225

226226
document.getElementById("messageInput").disabled = true;

aspnetcore/signalr/javascript-client/sample/wwwroot/js/chat.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
// such as Internet Explorer 11, use a transpiler such as
55
// Babel at http://babeljs.io/.
66
//
7-
// See Es5-chat.js for a Babel transpiled version of the following code:
7+
// See Es5-chat.js for a Babel-transpiled version of the following code:
88

99
const connection = new signalR.HubConnectionBuilder()
1010
.withUrl("/chatHub")
1111
.configureLogging(signalR.LogLevel.Information)
1212
.build();
1313

1414
connection.on("ReceiveMessage", (user, message) => {
15-
const encodedMsg = user + " says " + message;
15+
const encodedMsg = `${user} says ${message}`;
1616
const li = document.createElement("li");
1717
li.textContent = encodedMsg;
1818
document.getElementById("messagesList").appendChild(li);
@@ -21,7 +21,7 @@ connection.on("ReceiveMessage", (user, message) => {
2121
document.getElementById("sendButton").addEventListener("click", event => {
2222
const user = document.getElementById("userInput").value;
2323
const message = document.getElementById("messageInput").value;
24-
connection.invoke("SendMessage", user, message).catch(err => console.error(err.toString()));
24+
connection.invoke("SendMessage", user, message).catch(err => console.error(err));
2525
event.preventDefault();
2626
});
2727

@@ -43,13 +43,9 @@ connection.onclose(async () => {
4343
start();
4444

4545
/* this is here to show an alternative to start, with a then
46-
connection.start().then(function () {
47-
console.log("connected");
48-
});
46+
connection.start().then(() => console.log("connected"));
4947
*/
5048

5149
/* this is here to show another alternative to start, with a catch
52-
connection.start().catch(function (err) {
53-
return console.error(err.toString());
54-
});
50+
connection.start().catch(err => console.error(err));
5551
*/

aspnetcore/signalr/javascript-client/sample/wwwroot/js/es5-chat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ document.getElementById("sendButton").addEventListener("click", function (event)
1919
});
2020

2121
connection.start().catch(function (err) {
22-
return console.error(err.toString());
22+
return console.error(err);
2323
});

0 commit comments

Comments
 (0)