-
Notifications
You must be signed in to change notification settings - Fork 12
/
sample-server.js
141 lines (130 loc) · 4.66 KB
/
sample-server.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
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
const Boom = require("@hapi/boom")
const HAPI = require("@hapi/hapi")
const HAPIAuthBasic = require("@hapi/basic")
const HAPIWebSocket = require("./hapi-plugin-websocket")
const WebSocket = require("ws")
;(async () => {
/* create new HAPI service */
const server = new HAPI.Server({ address: "127.0.0.1", port: 12345 })
/* register HAPI plugins */
await server.register(HAPIWebSocket)
await server.register(HAPIAuthBasic)
/* register Basic authentication strategy */
server.auth.strategy("basic", "basic", {
validate: async (request, username, password, h) => {
let isValid = false
let credentials = null
if (username === "foo" && password === "bar") {
isValid = true
credentials = { username }
}
return { isValid, credentials }
}
})
/* provide plain REST route */
server.route({
method: "POST", path: "/foo",
options: {
payload: { output: "data", parse: true, allow: "application/json" }
},
handler: (request, h) => {
return { at: "foo", seen: request.payload }
}
})
/* provide combined REST/WebSocket route */
server.route({
method: "POST", path: "/bar",
options: {
payload: { output: "data", parse: true, allow: "application/json" },
plugins: { websocket: true }
},
handler: (request, h) => {
const { mode } = request.websocket()
return { at: "bar", mode: mode, seen: request.payload }
}
})
/* provide exclusive WebSocket route */
server.route({
method: "POST", path: "/baz",
options: {
plugins: { websocket: { only: true, autoping: 30 * 1000 } }
},
handler: (request, h) => {
return { at: "baz", seen: request.payload }
}
})
/* provide full-featured exclusive WebSocket route */
server.route({
method: "POST", path: "/quux",
options: {
response: { emptyStatusCode: 204 },
payload: { output: "data", parse: true, allow: "application/json" },
auth: { mode: "required", strategy: "basic" },
plugins: {
websocket: {
only: true,
initially: true,
subprotocol: "quux.example.com",
connect: ({ ctx, ws }) => {
ctx.to = setInterval(() => {
if (ws.readyState === WebSocket.OPEN)
ws.send(JSON.stringify({ cmd: "PING" }))
}, 5000)
},
disconnect: ({ ctx }) => {
if (ctx.to !== null) {
clearTimeout(this.ctx)
ctx.to = null
}
}
}
}
},
handler: (request, h) => {
const { initially, ws } = request.websocket()
if (initially) {
ws.send(JSON.stringify({ cmd: "HELLO", arg: request.auth.credentials.username }))
return ""
}
if (typeof request.payload !== "object" || request.payload === null)
return Boom.badRequest("invalid request")
if (typeof request.payload.cmd !== "string")
return Boom.badRequest("invalid request")
if (request.payload.cmd === "PING")
return { result: "PONG" }
else if (request.payload.cmd === "AWAKE-ALL") {
const peers = request.websocket().peers
peers.forEach((peer) => {
peer.send(JSON.stringify({ cmd: "AWAKE" }))
})
return ""
}
else
return Boom.badRequest("unknown command")
}
})
/* provide exclusive framed WebSocket route */
server.route({
method: "POST", path: "/framed",
options: {
plugins: {
websocket: {
only: true,
autoping: 30 * 1000,
frame: true,
frameEncoding: "json",
frameRequest: "REQUEST",
frameResponse: "RESPONSE"
}
}
},
handler: (request, h) => {
return { at: "framed", seen: request.payload }
}
})
/* start the HAPI service */
await server.start()
})().catch((err) => {
/* eslint no-console: off */
console.log(`ERROR: ${err}`)
})