sessions?

This commit is contained in:
Jörn-Michael Miehe 2018-12-14 23:41:43 +01:00
parent b24b05009f
commit 8d5a31a5dc
4 changed files with 25 additions and 13 deletions

View file

@ -7,9 +7,11 @@ FFTCGDB = (require './fftcgdb')
FFTCGSOCKET = (http, dbfile) -> FFTCGSOCKET = (http, dbfile) ->
that = @ that = @
# create server socket, open fftcg.db
@io = socketio http @io = socketio http
@db = new FFTCGDB dbfile @db = new FFTCGDB dbfile
# on new connection
@io.on 'connection', (socket) -> @io.on 'connection', (socket) ->
that.__connection socket that.__connection socket
@ -18,13 +20,16 @@ FFTCGSOCKET = (http, dbfile) ->
FFTCGSOCKET::__connection = (socket) -> FFTCGSOCKET::__connection = (socket) ->
that = @ that = @
console.log 'a user connected' # offer server socket id
socket.emit 'serverid', socket.id
console.log socket.handshake.headers['cookie'] # accept client side id
socket.emit 'id', socket.handshake.headers['cookie'] socket.on 'clientid', (clientid) ->
@clientid = clientid
console.log "user '#{@clientid}' connected"
socket.on 'disconnect', -> socket.on 'disconnect', ->
console.log 'a user disconnected' console.log "user '#{@clientid}' disconnected"
socket.on 'register', (login, password) -> socket.on 'register', (login, password) ->
that.__register login, password that.__register login, password

View file

@ -5,13 +5,10 @@ http = (require 'http')
path = (require 'path') path = (require 'path')
# my libraries # my libraries
FFTCGSOCKET = (require './inc/socket') FFTCGSOCKET = (require './inc/fftcgsocket')
# express + socket framework # express + socket framework
app = express() app = express()
web = http.Server app
socket = new FFTCGSOCKET(web, path.resolve(__dirname, 'fftcg.db'))
app.use helmet() app.use helmet()
# Static content # Static content
@ -23,9 +20,12 @@ app.get '/:template.html', (req, res) ->
res.render (req.params.template + '.pug') res.render (req.params.template + '.pug')
# Create server # Create server
web = http.Server app
web.listen 3000, -> web.listen 3000, ->
console.log '[FFTCG] Listening on port 3000 ...' console.log '[FFTCG] Listening on port 3000 ...'
socket = new FFTCGSOCKET web, path.resolve(__dirname, 'fftcg.db')
# Handle termination # Handle termination
process.on 'SIGINT', -> process.on 'SIGINT', ->
socket.close() socket.close()

View file

@ -11,11 +11,21 @@ $ ->
# init Socket.IO # init Socket.IO
socket = io() socket = io()
console.log socket
require './index/localStorage.coffee' require './index/localStorage.coffee'
socket.on 'id', (session) ->
console.log session
socket.on 'id', (socketid) ->
if storageAvailable 'localStorage'
myid = localStorage.getItem 'myid'
if not myid
localStorage.setItem 'myid', socketid
myid = socketid
console.log "id is '#{myid}'"
socket.emit 'id', myid
# login form # login form
$('form[name="login"]').submit -> $('form[name="login"]').submit ->

View file

@ -19,6 +19,3 @@ window.storageAvailable = (type) ->
e.name == 'NS_ERROR_DOM_QUOTA_REACHED') and e.name == 'NS_ERROR_DOM_QUOTA_REACHED') and
# acknowledge QuotaExceededError only if there's something already stored # acknowledge QuotaExceededError only if there's something already stored
storage.length != 0 storage.length != 0
if storageAvailable 'sessionStorage'
console.log "yay"