Skip to content

Commit 59b99ad

Browse files
committed
Added user message targeting to SignalR components
1 parent ce7ba17 commit 59b99ad

File tree

5 files changed

+181
-160
lines changed

5 files changed

+181
-160
lines changed

dotnet/ServerlessMicroservices.FunctionApp.Trips/TripFunctions.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ public static async Task<IActionResult> AssignTripDriver([HttpTrigger(Authorizat
165165

166166
/*** SignalR Info or Negotiate Function ****/
167167
[FunctionName("GetSignalRInfo")]
168-
public static IActionResult GetSignalRInfo([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "signalrinfo")] HttpRequest req,
169-
[SignalRConnectionInfo(HubName = "trips")] SignalRConnectionInfo info,
168+
public static async Task<IActionResult> GetSignalRInfo([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "signalrinfo")] HttpRequest req,
169+
[SignalRConnectionInfo(HubName = "trips", UserId = "{headers.x-ms-signalr-userid}")] SignalRConnectionInfo info,
170170
ILogger log)
171171
{
172172
log.LogInformation("GetSignalRInfo triggered....");
@@ -176,6 +176,8 @@ public static IActionResult GetSignalRInfo([HttpTrigger(AuthorizationLevel.Anony
176176
if (info == null)
177177
throw new Exception("SignalR Info is null!");
178178

179+
await Utilities.ValidateToken(req);
180+
179181
return (ActionResult)new OkObjectResult(info);
180182
}
181183
catch (Exception e)
@@ -223,8 +225,9 @@ public static async Task ProcessTripExternalizations2SignalR([EventGridTrigger]
223225
clientMethod = "tripAborted";
224226

225227
log.LogInformation($"ProcessTripExternalizations2SignalR firing SignalR `{clientMethod}` client method!");
226-
await signalRMessages.AddAsync(new SignalRMessage()
228+
await signalRMessages.AddAsync(new SignalRMessage
227229
{
230+
UserId = trip.Passenger.Code,
228231
Target = clientMethod,
229232
Arguments = new object[] { trip }
230233
});

web/serverless-microservices-web/src/api/trips.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ const baseUrl = window.apiTripsBaseUrl;
44
const apiKey = window.apiKey;
55

66
// GET methods
7-
export function getSignalRInfo() {
8-
return get(`${baseUrl}/signalrinfo`, {}, apiKey).then(checkResponse);
7+
export function getSignalRInfo(username) {
8+
let customHeader = null;
9+
if (username) {
10+
customHeader = { 'x-ms-signalr-userid': username };
11+
}
12+
return get(`${baseUrl}/signalrinfo`, {}, apiKey, customHeader).then(checkResponse);
913
}
1014

1115
// POST methods

web/serverless-microservices-web/src/components/SignalRTrips.vue

Lines changed: 121 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
<template>
2-
</template>
1+
<template></template>
32

43
<script>
5-
import * as signalR from '@aspnet/signalr';
6-
import { createNamespacedHelpers } from 'vuex';
7-
const { mapGetters: commonGetters } = createNamespacedHelpers('common');
4+
import * as signalR from "@aspnet/signalr";
5+
import { createNamespacedHelpers } from "vuex";
6+
import { getPassenger } from "@/api/passengers";
7+
const { mapGetters: commonGetters } = createNamespacedHelpers("common");
88
const {
99
mapGetters: tripGetters,
1010
mapActions: tripActions
11-
} = createNamespacedHelpers('trips');
11+
} = createNamespacedHelpers("trips");
1212
1313
export default {
14-
name: 'SignalRTrips',
14+
name: "SignalRTrips",
1515
data() {
1616
return {};
1717
},
1818
computed: {
19-
...commonGetters(['notificationSystem', 'user']),
20-
...tripGetters(['trip', 'currentStep', 'contentLoading'])
19+
...commonGetters(["notificationSystem", "user"]),
20+
...tripGetters(["trip", "currentStep", "contentLoading"])
2121
},
2222
watch: {
2323
user(val, old) {
@@ -30,123 +30,133 @@ export default {
3030
}
3131
},
3232
methods: {
33-
...tripActions(['setTrip', 'setCurrentStep', 'createTrip', 'getSignalRInfo']),
33+
...tripActions([
34+
"setTrip",
35+
"setCurrentStep",
36+
"createTrip",
37+
"getSignalRInfo"
38+
]),
3439
async getSignalRInformation() {
35-
let rawResponse = await this.getSignalRInfo(); // {url: '', status: 201};//await this.getSignalRInfo();
36-
if (rawResponse.status === 200) {
37-
let signalRInfo = rawResponse.data;
38-
console.log(`Connection Endpoint: ${signalRInfo.url}`);
39-
return signalRInfo;
40-
} else {
41-
console.log(`getSignalRInfo Response status: ${rawResponse.status}`);
42-
throw `Could not obtain SignalR info. Response was ${
43-
rawResponse.status
44-
}`;
45-
return null;
40+
let passengerInfo = await getPassenger(this.user.idToken.oid);
41+
if (passengerInfo && passengerInfo.data) {
42+
// Pass in the current user email so messages can be sent to just the user.
43+
let rawResponse = await this.getSignalRInfo(passengerInfo.data.email); // {url: '', status: 201};//await this.getSignalRInfo();
44+
if (rawResponse.status === 200) {
45+
let signalRInfo = rawResponse.data;
46+
console.log(`Connection Endpoint: ${signalRInfo.url}`);
47+
return signalRInfo;
48+
} else {
49+
console.log(`getSignalRInfo Response status: ${rawResponse.status}`);
50+
throw `Could not obtain SignalR info. Response was ${
51+
rawResponse.status
52+
}`;
53+
return null;
54+
}
4655
}
4756
},
4857
connectToSignalR() {
4958
if (this.user !== null) {
50-
this.getSignalRInformation()
51-
.then(signalrInfo => {
52-
if (signalrInfo !== null && signalrInfo !== undefined) {
53-
let options = {
54-
accessTokenFactory: () => signalrInfo.accessToken
55-
};
56-
57-
let hubConnection = new signalR.HubConnectionBuilder()
58-
.withUrl(signalrInfo.url, options)
59-
.configureLogging(signalR.LogLevel.Information)
60-
.build();
59+
this.getSignalRInformation()
60+
.then(signalrInfo => {
61+
if (signalrInfo !== null && signalrInfo !== undefined) {
62+
let options = {
63+
accessTokenFactory: () => signalrInfo.accessToken
64+
};
6165
62-
console.log('Connected to SignalR');
66+
let hubConnection = new signalR.HubConnectionBuilder()
67+
.withUrl(signalrInfo.url, options)
68+
.configureLogging(signalR.LogLevel.Information)
69+
.build();
6370
64-
hubConnection.on('tripUpdated', trip => {
65-
console.log(`tripUpdated Trip code: ${trip.code}`);
66-
this.$toast.success(
67-
`Trip Code: ${trip.code}. Message: tripUpdated.`,
68-
'Trip Updated',
69-
this.notificationSystem.options.success
70-
);
71-
});
71+
console.log("Connected to SignalR");
7272
73-
hubConnection.on('tripDriversNotified', trip => {
74-
console.log(`tripDriversNotified Trip code: ${trip.code}`);
75-
this.$toast.info(
76-
`Trip Code: ${trip.code}. Message: tripDriversNotified.`,
77-
'Drivers Notified',
78-
this.notificationSystem.options.info
79-
);
80-
});
73+
hubConnection.on("tripUpdated", trip => {
74+
console.log(`tripUpdated Trip code: ${trip.code}`);
75+
this.$toast.success(
76+
`Trip Code: ${trip.code}. Message: tripUpdated.`,
77+
"Trip Updated",
78+
this.notificationSystem.options.success
79+
);
80+
});
8181
82-
hubConnection.on('tripDriverPicked', trip => {
83-
console.log(`tripDriverPicked Trip code: ${trip.code}`);
84-
this.setCurrentStep(2);
85-
this.setTrip(trip);
86-
this.$toast.info(
87-
`Trip Code: ${trip.code}. Message: tripDriverPicked.`,
88-
'Driver Picked',
89-
this.notificationSystem.options.info
90-
);
91-
});
82+
hubConnection.on("tripDriversNotified", trip => {
83+
console.log(`tripDriversNotified Trip code: ${trip.code}`);
84+
this.$toast.info(
85+
`Trip Code: ${trip.code}. Message: tripDriversNotified.`,
86+
"Drivers Notified",
87+
this.notificationSystem.options.info
88+
);
89+
});
9290
93-
hubConnection.on('tripStarting', trip => {
94-
console.log(`tripStarting Trip code: ${trip.code}`);
95-
this.setCurrentStep(3);
96-
this.$toast.info(
97-
`Trip Code: ${trip.code}. Message: tripStarting.`,
98-
'Trip Starting',
99-
this.notificationSystem.options.info
100-
);
101-
});
91+
hubConnection.on("tripDriverPicked", trip => {
92+
console.log(`tripDriverPicked Trip code: ${trip.code}`);
93+
this.setCurrentStep(2);
94+
this.setTrip(trip);
95+
this.$toast.info(
96+
`Trip Code: ${trip.code}. Message: tripDriverPicked.`,
97+
"Driver Picked",
98+
this.notificationSystem.options.info
99+
);
100+
});
102101
103-
hubConnection.on('tripRunning', trip => {
104-
console.log(`tripRunning Trip code: ${trip.code}`);
105-
if (this.currentStep < 3) {
102+
hubConnection.on("tripStarting", trip => {
103+
console.log(`tripStarting Trip code: ${trip.code}`);
106104
this.setCurrentStep(3);
107-
}
108-
this.$toast.info(
109-
`Trip Code: ${trip.code}. Message: tripRunning.`,
110-
'Trip Running',
111-
this.notificationSystem.options.info
112-
);
113-
});
105+
this.$toast.info(
106+
`Trip Code: ${trip.code}. Message: tripStarting.`,
107+
"Trip Starting",
108+
this.notificationSystem.options.info
109+
);
110+
});
114111
115-
hubConnection.on('tripCompleted', trip => {
116-
console.log(`tripCompleted Trip code: ${trip.code}`);
117-
this.setCurrentStep(4);
118-
this.$toast.success(
119-
`Trip Code: ${trip.code}. Message: tripCompleted.`,
120-
'Trip Completed',
121-
this.notificationSystem.options.success
122-
);
123-
});
112+
hubConnection.on("tripRunning", trip => {
113+
console.log(`tripRunning Trip code: ${trip.code}`);
114+
if (this.currentStep < 3) {
115+
this.setCurrentStep(3);
116+
}
117+
this.$toast.info(
118+
`Trip Code: ${trip.code}. Message: tripRunning.`,
119+
"Trip Running",
120+
this.notificationSystem.options.info
121+
);
122+
});
124123
125-
hubConnection.on('tripAborted', trip => {
126-
console.log(`tripAborted Trip code: ${trip.code}`);
127-
this.setCurrentStep(0);
128-
this.$toast.warning(
129-
`Trip Code: ${trip.code}. Message: tripAborted.`,
130-
'Trip Aborted',
131-
this.notificationSystem.options.warning
132-
);
133-
});
124+
hubConnection.on("tripCompleted", trip => {
125+
console.log(`tripCompleted Trip code: ${trip.code}`);
126+
this.setCurrentStep(4);
127+
this.$toast.success(
128+
`Trip Code: ${trip.code}. Message: tripCompleted.`,
129+
"Trip Completed",
130+
this.notificationSystem.options.success
131+
);
132+
});
134133
135-
hubConnection.start().catch(err => console.log(err.toString()));
136-
} else {
137-
console.log('signalrInfo is null');
138-
}
139-
})
140-
.catch(err => {
141-
this.$toast.error(
142-
err.response ? err.response : err.message ? err.message : err,
143-
'Error',
144-
this.notificationSystem.options.error
145-
);
146-
});
147-
}
148-
else {
149-
console.log('Not connecting to SignalR because the user is not authenticated.')
134+
hubConnection.on("tripAborted", trip => {
135+
console.log(`tripAborted Trip code: ${trip.code}`);
136+
this.setCurrentStep(0);
137+
this.$toast.warning(
138+
`Trip Code: ${trip.code}. Message: tripAborted.`,
139+
"Trip Aborted",
140+
this.notificationSystem.options.warning
141+
);
142+
});
143+
144+
hubConnection.start().catch(err => console.log(err.toString()));
145+
} else {
146+
console.log("signalrInfo is null");
147+
}
148+
})
149+
.catch(err => {
150+
this.$toast.error(
151+
err.response ? err.response : err.message ? err.message : err,
152+
"Error",
153+
this.notificationSystem.options.error
154+
);
155+
});
156+
} else {
157+
console.log(
158+
"Not connecting to SignalR because the user is not authenticated."
159+
);
150160
}
151161
}
152162
},

web/serverless-microservices-web/src/store/trips.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ export default {
5151
}
5252
},
5353

54-
async getSignalRInfo({ commit }) {
54+
async getSignalRInfo({ commit }, username) {
5555
try {
56-
let signalRInfo = await getSignalRInfo();
56+
let signalRInfo = await getSignalRInfo(username);
5757
return signalRInfo;
5858
} catch (e) {
5959
throw e;

0 commit comments

Comments
 (0)