2018-12-14 12:31:07 +00:00
|
|
|
# node libraries
|
|
|
|
socketio = (require 'socket.io')
|
|
|
|
path = (require 'path')
|
|
|
|
|
|
|
|
# my libraries
|
|
|
|
FFTCGDB = (require './fftcgdb')
|
|
|
|
|
2018-12-15 20:09:01 +00:00
|
|
|
FFTCGSOCKET = (http, dbfile, session) ->
|
2018-12-14 12:31:07 +00:00
|
|
|
that = @
|
2018-12-15 20:09:01 +00:00
|
|
|
|
|
|
|
# create server socket
|
2018-12-14 12:31:07 +00:00
|
|
|
@io = socketio http
|
2018-12-15 20:09:01 +00:00
|
|
|
@io.use session
|
|
|
|
|
|
|
|
# open fftcg db
|
2018-12-14 12:31:07 +00:00
|
|
|
@db = new FFTCGDB dbfile
|
|
|
|
|
2018-12-14 22:41:43 +00:00
|
|
|
# on new connection
|
2018-12-14 12:31:07 +00:00
|
|
|
@io.on 'connection', (socket) ->
|
|
|
|
that.__connection socket
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
FFTCGSOCKET::__connection = (socket) ->
|
|
|
|
that = @
|
|
|
|
|
2018-12-15 20:09:01 +00:00
|
|
|
@session = socket.handshake.session
|
|
|
|
console.log "session '#{@session.id}' connected"
|
|
|
|
console.log "is user '#{@session.userID}'" if @session.userID
|
2018-12-14 12:31:07 +00:00
|
|
|
|
|
|
|
socket.on 'disconnect', ->
|
2018-12-15 20:09:01 +00:00
|
|
|
console.log "session '#{that.session.id}' disconnected"
|
|
|
|
console.log "is user '#{that.session.userID}'" if that.session.userID
|
2018-12-14 12:31:07 +00:00
|
|
|
|
|
|
|
socket.on 'register', (login, password) ->
|
|
|
|
that.__register login, password
|
|
|
|
|
|
|
|
socket.on 'login', (login, password) ->
|
|
|
|
that.__login login, password
|
|
|
|
|
|
|
|
FFTCGSOCKET::__login = (login, password) ->
|
2018-12-15 20:09:01 +00:00
|
|
|
that = @
|
|
|
|
|
2018-12-14 12:31:07 +00:00
|
|
|
console.log '__login:', login, password
|
|
|
|
@db.login login, password
|
|
|
|
.then (login) ->
|
2018-12-15 20:09:01 +00:00
|
|
|
that.session.userID = login
|
|
|
|
that.session.save()
|
2018-12-14 12:31:07 +00:00
|
|
|
console.log 'Login OK "%s"', login
|
|
|
|
.catch (err) ->
|
|
|
|
console.error 'error: "%s"', err
|
|
|
|
|
|
|
|
FFTCGSOCKET::__register = (login, password) ->
|
|
|
|
console.log '__register:', login, password
|
|
|
|
@db.register login, password
|
|
|
|
.then (login) ->
|
|
|
|
console.log 'registered "%s"', login
|
|
|
|
.catch (err) ->
|
|
|
|
console.error 'error: "%s"', err
|
|
|
|
|
|
|
|
FFTCGSOCKET::close = ->
|
|
|
|
console.log '[FFTCGSOCKET] shutting down'
|
|
|
|
if @db
|
|
|
|
@db.close()
|
|
|
|
.then (msg) ->
|
|
|
|
console.log msg
|
|
|
|
.catch (err) ->
|
|
|
|
console.error err
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = FFTCGSOCKET
|