2018-12-07 09:38:46 +00:00
|
|
|
# node libraries
|
2019-02-13 16:25:35 +00:00
|
|
|
fastify = (require 'fastify')()# logger: true
|
2019-02-07 16:03:20 +00:00
|
|
|
logger = (require 'logging').default 'FFTCG'
|
2019-02-13 16:25:35 +00:00
|
|
|
path = (require 'path')
|
2018-12-07 09:38:46 +00:00
|
|
|
|
|
|
|
# my libraries
|
2019-02-13 16:25:35 +00:00
|
|
|
# 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
|
|
|
|
|
2018-11-29 13:14:53 +00:00
|
|
|
|
2018-12-07 09:38:46 +00:00
|
|
|
# Handle termination
|
|
|
|
process.on 'SIGINT', ->
|
2018-12-14 12:31:07 +00:00
|
|
|
socket.close()
|
2019-02-07 16:03:20 +00:00
|
|
|
logger.info 'shutting down after SIGINT'
|
2018-12-07 09:38:46 +00:00
|
|
|
process.exit()
|