From 8d5a31a5dc2b41b7b640949c239750a0037cfe03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Fri, 14 Dec 2018 23:41:43 +0100 Subject: [PATCH] sessions? --- inc/{socket.coffee => fftcgsocket.coffee} | 13 +++++++++---- server.coffee | 8 ++++---- src/index.coffee | 14 ++++++++++++-- src/index/localStorage.coffee | 3 --- 4 files changed, 25 insertions(+), 13 deletions(-) rename inc/{socket.coffee => fftcgsocket.coffee} (79%) diff --git a/inc/socket.coffee b/inc/fftcgsocket.coffee similarity index 79% rename from inc/socket.coffee rename to inc/fftcgsocket.coffee index c38b177..66653f6 100644 --- a/inc/socket.coffee +++ b/inc/fftcgsocket.coffee @@ -7,9 +7,11 @@ FFTCGDB = (require './fftcgdb') FFTCGSOCKET = (http, dbfile) -> that = @ + # create server socket, open fftcg.db @io = socketio http @db = new FFTCGDB dbfile + # on new connection @io.on 'connection', (socket) -> that.__connection socket @@ -18,13 +20,16 @@ FFTCGSOCKET = (http, dbfile) -> FFTCGSOCKET::__connection = (socket) -> that = @ - console.log 'a user connected' + # offer server socket id + socket.emit 'serverid', socket.id - console.log socket.handshake.headers['cookie'] - socket.emit 'id', socket.handshake.headers['cookie'] + # accept client side id + socket.on 'clientid', (clientid) -> + @clientid = clientid + console.log "user '#{@clientid}' connected" socket.on 'disconnect', -> - console.log 'a user disconnected' + console.log "user '#{@clientid}' disconnected" socket.on 'register', (login, password) -> that.__register login, password diff --git a/server.coffee b/server.coffee index df7c9af..9dd2a39 100644 --- a/server.coffee +++ b/server.coffee @@ -5,13 +5,10 @@ http = (require 'http') path = (require 'path') # my libraries -FFTCGSOCKET = (require './inc/socket') +FFTCGSOCKET = (require './inc/fftcgsocket') # express + socket framework app = express() -web = http.Server app -socket = new FFTCGSOCKET(web, path.resolve(__dirname, 'fftcg.db')) - app.use helmet() # Static content @@ -23,9 +20,12 @@ app.get '/:template.html', (req, res) -> res.render (req.params.template + '.pug') # Create server +web = http.Server app web.listen 3000, -> console.log '[FFTCG] Listening on port 3000 ...' +socket = new FFTCGSOCKET web, path.resolve(__dirname, 'fftcg.db') + # Handle termination process.on 'SIGINT', -> socket.close() diff --git a/src/index.coffee b/src/index.coffee index 1aa5aa5..7c39523 100644 --- a/src/index.coffee +++ b/src/index.coffee @@ -11,11 +11,21 @@ $ -> # init Socket.IO socket = io() + console.log socket 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 $('form[name="login"]').submit -> diff --git a/src/index/localStorage.coffee b/src/index/localStorage.coffee index 13a9c60..ee3d3e2 100644 --- a/src/index/localStorage.coffee +++ b/src/index/localStorage.coffee @@ -19,6 +19,3 @@ window.storageAvailable = (type) -> e.name == 'NS_ERROR_DOM_QUOTA_REACHED') and # acknowledge QuotaExceededError only if there's something already stored storage.length != 0 - -if storageAvailable 'sessionStorage' - console.log "yay"