This repository has been archived on 2024-04-29. You can view files and clone it, but cannot push or open issues or pull requests.
node-fftcg/backend/server.coffee

55 lines
1.3 KiB
CoffeeScript

# node libraries
fastify = (require 'fastify')()# logger: true
logger = (require 'logging').default 'FFTCG'
path = (require 'path')
# my libraries
# FFTCGSOCKET = (require './socket')
# FFTCGSESSION = (require './session')
# FFTCGROUTER = (require './router')
# fastify framework
fastify.register (require 'fastify-cookie')
fastify.register (require 'fastify-ws'), library: 'uws'
fastify.register (require 'fastify-static'), root: path.join __dirname, 'tmpfront'
fastify.ready()
.then ->
fastify.ws.on 'connection', (socket) ->
logger.info 'Client connected.'
logger.info JSON.stringify socket.cookies
socket.on 'message', (msg) ->
# echo server
logger.info "Echo '#{msg}'."
socket.send msg
socket.on 'close', ->
logger.info 'Client disconnected.'
.catch (err) ->
logger.error err
process.exit 1
fastify.get '/api', (request, reply) ->
logger.info request.cookies
reply.setCookie 'foo', 'foo', path: '/'
.send hello: 'world'
fastify.listen 3001, '0.0.0.0'
.then ->
logger.info "Listening on port #{fastify.server.address().port} ..."
.catch (err) ->
logger.error err
# Handle termination
process.on 'SIGINT', ->
socket.close()
logger.info 'shutting down after SIGINT'
process.exit()