This repository has been archived by the owner on Nov 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
48 lines (42 loc) · 1.47 KB
/
test.js
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
const { WebPubSubServiceClient, WebPubSubCloudEventsHandler } = require('./dist/webpubsub')
const dotenv = require('dotenv');
const express = require("express");
dotenv.config();
const serviceClient = new WebPubSubServiceClient(process.env.WPS_CONNECTION_STRING, 'chat');
// sample of generating client token
console.log(serviceClient.getAuthenticationToken({
userId: "vicancy",
claims: {
hey: ["w"],
role: ["webpubsub.group.join"],
}
}));
const handler = new WebPubSubCloudEventsHandler('chat', ['*'],
{
//path: "/customUrl", // optional
handleConnect: async (req, res) => {
res.success({
userId: "vicancy"
});
},
onConnected: async connectedRequest => {
await serviceClient.sendToAll(connectedRequest.context.connectionId + " connected", { dataType: "text" });
},
handleUserEvent: async (userRequest, res) => {
console.log(`Received user request data: ${userRequest.data}`);
if (userRequest.data === 'abort') {
res.fail(400, "abort");
} else if (userRequest.data === 'error') {
res.fail(500, "error");
} else {
res.success("Hey " + userRequest.data, userRequest.dataType);
}
},
onDisconnected: async disconnectRequest => {
console.log(disconnectRequest.context.userId + " disconnected");
}
}
);
const app = express();
app.use(handler.getMiddleware())
app.listen(3000, () => console.log(`Azure WebPubSub Upstream ready at http://localhost:3000${handler.path}`));