-
Notifications
You must be signed in to change notification settings - Fork 260
/
Copy pathSignalRTrips.vue
165 lines (154 loc) · 5.61 KB
/
SignalRTrips.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<template>
<div></div>
</template>
<script>
import * as signalR from "@microsoft/signalr";
import { createNamespacedHelpers } from "vuex";
import { getPassenger } from "@/api/passengers";
const { mapGetters: commonGetters } = createNamespacedHelpers("common");
const {
mapGetters: tripGetters,
mapActions: tripActions
} = createNamespacedHelpers("trips");
export default {
name: "SignalRTrips",
data() {
return {};
},
computed: {
...commonGetters(["notificationSystem", "user"]),
...tripGetters(["trip", "currentStep", "contentLoading"])
},
watch: {
user(val, old) {
// We watch for the username to change due to logging in or out.
// If the username was previously null, but now isn't, then we
// connect to SignalR Service.
if (old === null && val !== null) {
this.connectToSignalR();
}
}
},
methods: {
...tripActions([
"setTrip",
"setCurrentStep",
"createTrip",
"getSignalRInfo"
]),
async getSignalRInformation() {
let passengerInfo = await getPassenger(this.user.idTokenClaims.oid);
if (passengerInfo && passengerInfo.data) {
// Pass in the current user email so messages can be sent to just the user.
let rawResponse = await this.getSignalRInfo(passengerInfo.data.email); // {url: '', status: 201};//await this.getSignalRInfo();
if (rawResponse.status === 200) {
let signalRInfo = rawResponse.data;
console.log(`Connection Endpoint: ${signalRInfo.url}`);
return signalRInfo;
} else {
console.log(`getSignalRInfo Response status: ${rawResponse.status}`);
return null;
}
}
},
connectToSignalR() {
if (this.user !== null) {
this.getSignalRInformation()
.then(signalrInfo => {
if (signalrInfo !== null && signalrInfo !== undefined) {
let options = {
accessTokenFactory: () => signalrInfo.accessToken
};
let hubConnection = new signalR.HubConnectionBuilder()
.withUrl(signalrInfo.url, options)
.configureLogging(signalR.LogLevel.Information)
.build();
console.log("Connected to SignalR");
hubConnection.on("tripUpdated", trip => {
console.log(`tripUpdated Trip code: ${trip.code}`);
this.$toast.success(
`Trip Code: ${trip.code}. Message: tripUpdated.`,
"Trip Updated",
this.notificationSystem.options.success
);
});
hubConnection.on("tripDriversNotified", trip => {
console.log(`tripDriversNotified Trip code: ${trip.code}`);
this.$toast.info(
`Trip Code: ${trip.code}. Message: tripDriversNotified.`,
"Drivers Notified",
this.notificationSystem.options.info
);
});
hubConnection.on("tripDriverPicked", trip => {
console.log(`tripDriverPicked Trip code: ${trip.code}`);
this.setCurrentStep(2);
this.setTrip(trip);
this.$toast.info(
`Trip Code: ${trip.code}. Message: tripDriverPicked.`,
"Driver Picked",
this.notificationSystem.options.info
);
});
hubConnection.on("tripStarting", trip => {
console.log(`tripStarting Trip code: ${trip.code}`);
this.setCurrentStep(3);
this.$toast.info(
`Trip Code: ${trip.code}. Message: tripStarting.`,
"Trip Starting",
this.notificationSystem.options.info
);
});
hubConnection.on("tripRunning", trip => {
console.log(`tripRunning Trip code: ${trip.code}`);
if (this.currentStep < 3) {
this.setCurrentStep(3);
}
this.$toast.info(
`Trip Code: ${trip.code}. Message: tripRunning.`,
"Trip Running",
this.notificationSystem.options.info
);
});
hubConnection.on("tripCompleted", trip => {
console.log(`tripCompleted Trip code: ${trip.code}`);
this.setCurrentStep(4);
this.$toast.success(
`Trip Code: ${trip.code}. Message: tripCompleted.`,
"Trip Completed",
this.notificationSystem.options.success
);
});
hubConnection.on("tripAborted", trip => {
console.log(`tripAborted Trip code: ${trip.code}`);
this.setCurrentStep(0);
this.$toast.warning(
`Trip Code: ${trip.code}. Message: tripAborted.`,
"Trip Aborted",
this.notificationSystem.options.warning
);
});
hubConnection.start().catch(err => console.log(err.toString()));
} else {
console.log("signalrInfo is null");
}
})
.catch(err => {
this.$toast.error(
err.response ? err.response : err.message ? err.message : err,
"Error",
this.notificationSystem.options.error
);
});
} else {
console.log(
"Not connecting to SignalR because the user is not authenticated."
);
}
}
},
mounted() {
this.connectToSignalR();
}
};
</script>