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/inc/fftcgsocket.coffee

68 lines
1.5 KiB
CoffeeScript
Raw Normal View History

# node libraries
socketio = (require 'socket.io')
path = (require 'path')
# my libraries
FFTCGDB = (require './fftcgdb')
FFTCGSOCKET = (http, dbfile) ->
that = @
2018-12-14 22:41:43 +00:00
# create server socket, open fftcg.db
@io = socketio http
@db = new FFTCGDB dbfile
2018-12-14 22:41:43 +00:00
# on new connection
@io.on 'connection', (socket) ->
that.__connection socket
return
FFTCGSOCKET::__connection = (socket) ->
that = @
2018-12-14 22:41:43 +00:00
# offer server socket id
socket.emit 'serverid', socket.id
2018-12-14 22:41:43 +00:00
# accept client side id
socket.on 'clientid', (clientid) ->
@clientid = clientid
console.log "user '#{@clientid}' connected"
socket.on 'disconnect', ->
2018-12-14 22:41:43 +00:00
console.log "user '#{@clientid}' disconnected"
socket.on 'register', (login, password) ->
that.__register login, password
socket.on 'login', (login, password) ->
that.__login login, password
FFTCGSOCKET::__login = (login, password) ->
console.log '__login:', login, password
@db.login login, password
.then (login) ->
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