fix: register @fastify/websocket globally so /ws/agent route works
setupAgentWsRoute was called outside the encapsulated wsRoutes plugin scope where @fastify/websocket was registered. Register the plugin at the root level and convert setupAgentWsRoute to FastifyPluginAsync. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -3,7 +3,7 @@
|
|||||||
* Агенты подключаются по wss://api.hotelsync.ru/ws/agent
|
* Агенты подключаются по wss://api.hotelsync.ru/ws/agent
|
||||||
* и остаются подключёнными для получения команд
|
* и остаются подключёнными для получения команд
|
||||||
*/
|
*/
|
||||||
import { FastifyInstance } from 'fastify'
|
import { FastifyInstance, FastifyPluginAsync } from 'fastify'
|
||||||
import type { SocketStream } from '@fastify/websocket'
|
import type { SocketStream } from '@fastify/websocket'
|
||||||
import { WebSocket } from 'ws'
|
import { WebSocket } from 'ws'
|
||||||
import { db } from './db'
|
import { db } from './db'
|
||||||
@@ -71,7 +71,7 @@ async function markOnline(workstationId: string, ip?: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Регистрируем route для WebSocket агентов
|
// Регистрируем route для WebSocket агентов
|
||||||
export function setupAgentWsRoute(fastify: FastifyInstance) {
|
export const setupAgentWsRoute: FastifyPluginAsync = async (fastify) => {
|
||||||
fastify.get('/ws/agent', { websocket: true }, async (connection: SocketStream, req) => {
|
fastify.get('/ws/agent', { websocket: true }, async (connection: SocketStream, req) => {
|
||||||
const socket = connection.socket as AgentSocket
|
const socket = connection.socket as AgentSocket
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import { join } from 'path'
|
|||||||
import { config } from './config'
|
import { config } from './config'
|
||||||
import './types' // side-effect: augments fastify types
|
import './types' // side-effect: augments fastify types
|
||||||
|
|
||||||
|
import fastifyWebsocket from '@fastify/websocket'
|
||||||
import wsRoutes from './routes/ws'
|
import wsRoutes from './routes/ws'
|
||||||
import authRoutes from './routes/auth'
|
import authRoutes from './routes/auth'
|
||||||
import hotelsRoutes from './routes/hotels'
|
import hotelsRoutes from './routes/hotels'
|
||||||
@@ -53,6 +54,9 @@ export async function buildApp() {
|
|||||||
const uploadsDir = process.env.UPLOADS_DIR ?? join(process.cwd(), 'uploads')
|
const uploadsDir = process.env.UPLOADS_DIR ?? join(process.cwd(), 'uploads')
|
||||||
await fastify.register(staticFiles, { root: uploadsDir, prefix: '/uploads/' })
|
await fastify.register(staticFiles, { root: uploadsDir, prefix: '/uploads/' })
|
||||||
|
|
||||||
|
// ── WebSocket ──────────────────────────────────────────────────────────────
|
||||||
|
await fastify.register(fastifyWebsocket)
|
||||||
|
|
||||||
// ── Security ───────────────────────────────────────────────────────────────
|
// ── Security ───────────────────────────────────────────────────────────────
|
||||||
await fastify.register(helmet, { contentSecurityPolicy: false, crossOriginResourcePolicy: { policy: 'cross-origin' } })
|
await fastify.register(helmet, { contentSecurityPolicy: false, crossOriginResourcePolicy: { policy: 'cross-origin' } })
|
||||||
|
|
||||||
@@ -114,7 +118,7 @@ export async function buildApp() {
|
|||||||
await fastify.register(loyaltyRoutes)
|
await fastify.register(loyaltyRoutes)
|
||||||
await fastify.register(chatRoutes)
|
await fastify.register(chatRoutes)
|
||||||
await fastify.register(workstationRoutes)
|
await fastify.register(workstationRoutes)
|
||||||
setupAgentWsRoute(fastify)
|
await fastify.register(setupAgentWsRoute)
|
||||||
|
|
||||||
startJobs()
|
startJobs()
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { FastifyPluginAsync } from 'fastify'
|
import { FastifyPluginAsync } from 'fastify'
|
||||||
import fastifyWebsocket from '@fastify/websocket'
|
|
||||||
import type { SocketStream } from '@fastify/websocket'
|
import type { SocketStream } from '@fastify/websocket'
|
||||||
import type { RawData } from 'ws'
|
import type { RawData } from 'ws'
|
||||||
|
|
||||||
@@ -18,8 +17,6 @@ export function broadcast(hotelSlug: string, message: object) {
|
|||||||
const PING_INTERVAL_MS = 25_000 // ping every 25s — keeps nginx proxy_read_timeout alive
|
const PING_INTERVAL_MS = 25_000 // ping every 25s — keeps nginx proxy_read_timeout alive
|
||||||
|
|
||||||
const ws: FastifyPluginAsync = async (fastify) => {
|
const ws: FastifyPluginAsync = async (fastify) => {
|
||||||
await fastify.register(fastifyWebsocket)
|
|
||||||
|
|
||||||
fastify.get<{ Querystring: { hotel?: string } }>(
|
fastify.get<{ Querystring: { hotel?: string } }>(
|
||||||
'/ws',
|
'/ws',
|
||||||
{ websocket: true },
|
{ websocket: true },
|
||||||
|
|||||||
Reference in New Issue
Block a user