chore(csms): try WebSocket support
This commit is contained in:
@@ -8,6 +8,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@hono/node-server": "^1.19.6",
|
"@hono/node-server": "^1.19.6",
|
||||||
|
"@hono/node-ws": "^1.2.0",
|
||||||
"hono": "^4.10.6"
|
"hono": "^4.10.6"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -1,15 +1,65 @@
|
|||||||
|
import pkg from '../../../package.json' with { type: 'json' }
|
||||||
import { serve } from '@hono/node-server'
|
import { serve } from '@hono/node-server'
|
||||||
import { Hono } from 'hono'
|
import { Hono } from 'hono'
|
||||||
|
import { createNodeWebSocket } from '@hono/node-ws'
|
||||||
|
import { getConnInfo } from '@hono/node-server/conninfo'
|
||||||
|
import { cors } from 'hono/cors'
|
||||||
|
import { logger } from 'hono/logger'
|
||||||
|
import { showRoutes } from 'hono/dev'
|
||||||
|
|
||||||
const app = new Hono()
|
const app = new Hono()
|
||||||
|
|
||||||
|
const { injectWebSocket, upgradeWebSocket } = createNodeWebSocket({ app })
|
||||||
|
|
||||||
|
app.use(logger())
|
||||||
|
|
||||||
|
app.use('/ocpp', cors({
|
||||||
|
origin: '*',
|
||||||
|
allowMethods: ['GET', 'POST', 'OPTIONS'],
|
||||||
|
allowHeaders: ['Content-Type', 'Authorization'],
|
||||||
|
exposeHeaders: ['Content-Length'],
|
||||||
|
credentials: true,
|
||||||
|
}))
|
||||||
|
|
||||||
app.get('/', (c) => {
|
app.get('/', (c) => {
|
||||||
return c.text('Hello Hono!')
|
return c.json({
|
||||||
|
platform: 'Helios CSMS',
|
||||||
|
version: pkg.version,
|
||||||
|
message: 'ok',
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
serve({
|
app.get(
|
||||||
fetch: app.fetch,
|
'/ocpp',
|
||||||
port: 3001
|
upgradeWebSocket((c) => {
|
||||||
}, (info) => {
|
return {
|
||||||
console.log(`Server is running on http://localhost:${info.port}`)
|
onOpen(evt, ws) {
|
||||||
|
const connInfo = getConnInfo(c)
|
||||||
|
console.log(`New connection from ${connInfo.remote.address}:${connInfo.remote.port}`)
|
||||||
|
},
|
||||||
|
onMessage(evt, ws) {
|
||||||
|
console.log(`Received message: ${evt.data}`)
|
||||||
|
ws.send(`Echo: ${evt.data}`)
|
||||||
|
},
|
||||||
|
onClose(evt, ws) {
|
||||||
|
console.log('Connection closed: ', evt.code, evt.reason)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
showRoutes(app, {
|
||||||
|
verbose: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const server = serve(
|
||||||
|
{
|
||||||
|
fetch: app.fetch,
|
||||||
|
port: 3001,
|
||||||
|
},
|
||||||
|
(info) => {
|
||||||
|
console.log(`Server is running on http://localhost:${info.port}`)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
injectWebSocket(server)
|
||||||
|
|||||||
Reference in New Issue
Block a user