Closed piglovesyou closed 8 years ago
For flexible authentication, we need follows:
req
server
ws
verifyClient
This is a short example to use session in handlers with using npm Express. (I don't guarantee, I did't run)
const http = require('http'); const express = require('express'); const cookieParser = require('cookie-parser'); const session = require('express-session'); const MemoryPersistence = require('synceddb-persistence-memory'); const SyncedDBBackend = require('synceddb-server'); const app = express(); const server = http.createServer(app); const sessionParser = session({ secret: 'baaa', resave: false, saveUninitialized: true }); app.set('port', 3000); app.use(cookieParser()); MemoryPersistence.create().then((p) => { const ws = new SyncedDBBackend({ store: p, server: server, verifyClient: (info, done) => { const {req} = info; sessionParser(req, null, () => { console.log(req.session); // session available done(!!req.sesion.user); // If true, a user accepts a handshake. }); } }); ws.handlers.create = (clientData, store, msg, respond, broadcast, req) => { sessionParser(req, null, () => { console.log(req.session); // session available req.session.user.type; // We can change what we do in terms of a user state. }); }; server.listen(port); });
For flexible authentication, we need follows:
req
to deal with sessionserver
to a web socket server of syncedDB,ws
module associates a web socket request with a http requestverifyClient
to ws.ServerThis is a short example to use session in handlers with using npm Express. (I don't guarantee, I did't run)