-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Web: Socket.IO: Refactoring work with socket.io
- Loading branch information
1 parent
b2cb05b
commit 99e37d9
Showing
26 changed files
with
5,170 additions
and
1,622 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,105 @@ | ||
const express = require('express'); | ||
const logger = require('morgan'); | ||
const cookieParser = require('cookie-parser'); | ||
const express = require("express"); | ||
const { createServer } = require("http"); | ||
const logger = require("morgan"); | ||
const cookieParser = require("cookie-parser"); | ||
const expressSession = require("express-session"); | ||
const sharedsession = require("express-socket.io-session"); | ||
const RedisStore = require('connect-redis')(expressSession); | ||
const MemoryStore = require('memorystore')(expressSession); | ||
const config = require('./config'); | ||
const winston = require('./app/log.js'); | ||
const { Server } = require("socket.io"); | ||
//const RedisStore = require("connect-redis")(expressSession); | ||
const MemoryStore = require("memorystore")(expressSession); | ||
const config = require("./config"); | ||
const winston = require("./app/log.js"); | ||
const app = express(); | ||
|
||
const secret = config.get("core.machinekey") + (new Date()).getTime(); | ||
const secret = config.get("core.machinekey") + new Date().getTime(); | ||
const secretCookieParser = cookieParser(secret); | ||
const baseCookieParser = cookieParser(); | ||
|
||
const port = config.get("port") || 3000; | ||
|
||
winston.stream = { | ||
write: (message) => winston.info(message) | ||
write: (message) => winston.info(message), | ||
}; | ||
|
||
const redis = { | ||
host: config.get("redis:host"), | ||
port: config.get("redis:port"), | ||
ttl: 3600 | ||
} | ||
// const redis = { | ||
// host: config.get("redis:host"), | ||
// port: config.get("redis:port"), | ||
// ttl: 3600, | ||
// }; | ||
|
||
let store; | ||
if(redis.host && redis.port){ | ||
store = new RedisStore(redis); | ||
} else { | ||
store = new MemoryStore(); | ||
} | ||
// if (redis.host && redis.port) { | ||
// store = new RedisStore(redis); | ||
// } else { | ||
store = new MemoryStore(); | ||
//} | ||
|
||
const session = expressSession({ | ||
store: store, | ||
secret: secret, | ||
resave: true, | ||
saveUninitialized: true, | ||
cookie: { | ||
path: '/', | ||
httpOnly: true, | ||
secure: false, | ||
maxAge: null | ||
}, | ||
cookieParser: secretCookieParser, | ||
name: "socketio.sid" | ||
store: store, | ||
secret: secret, | ||
resave: true, | ||
saveUninitialized: true, | ||
cookie: { | ||
path: "/", | ||
httpOnly: true, | ||
secure: false, | ||
maxAge: null, | ||
}, | ||
cookieParser: secretCookieParser, | ||
name: "socketio.sid", | ||
}); | ||
|
||
app.set('port', config.get('port') || 3000); | ||
app.use(logger("dev", { "stream": winston.stream })); | ||
app.use(session); | ||
app.get('/', (req, res) => { res.send('<h1>Hello world</h1>'); }); | ||
console.log(session); | ||
|
||
const server = app.listen(app.get('port'), () => { | ||
//log.info('Express server listening on port ' + server.address().port); | ||
//app.set("port", config.get("port") || 3000); | ||
app.use(logger("dev", { stream: winston.stream })); | ||
app.use(session); | ||
app.get("/", (req, res) => { | ||
res.send("<h1>Hello world</h1>"); | ||
}); | ||
|
||
const io = require('socket.io')(server, { | ||
perMessageDeflate : false, | ||
cookie: false, | ||
handlePreflightRequest: function (req, res) { | ||
session(req, res, ()=>{}); | ||
res.writeHead(200, {'Content-Type': 'text/html'}); | ||
res.end(); | ||
}, | ||
allowRequest : function(req, fn){ | ||
var cookies = baseCookieParser(req, null, ()=>{}); | ||
if(!req.cookies || (!req.cookies['asc_auth_key'] && !req.cookies['authorization'])){ | ||
return fn('auth', false); | ||
} | ||
|
||
return io.checkRequest(req, fn); | ||
// const server = app.listen(app.get("port"), () => { | ||
// log.info("Express server listening on port " + server.address().port); | ||
// }); | ||
|
||
const options = { | ||
perMessageDeflate: false, | ||
cookie: false, | ||
handlePreflightRequest: function (req, res) { | ||
session(req, res, () => {}); | ||
res.writeHead(200, { "Content-Type": "text/html" }); | ||
res.end(); | ||
}, | ||
allowRequest: function (req, fn) { | ||
var cookies = baseCookieParser(req, null, () => {}); | ||
if ( | ||
!req.cookies || | ||
(!req.cookies["asc_auth_key"] && !req.cookies["authorization"]) | ||
) { | ||
return fn("auth", false); | ||
} | ||
}); | ||
const auth = require('./app/middleware/auth.js'); | ||
|
||
io | ||
.use(sharedsession(session, secretCookieParser, {autoSave: true})) | ||
.use((socket, next) => { | ||
baseCookieParser(socket.client.request, null, next); | ||
}) | ||
.use((socket, next) => { | ||
auth(socket, next); | ||
}); | ||
return io.checkRequest(req, fn); | ||
}, | ||
}; | ||
|
||
const httpServer = createServer(app); | ||
|
||
const io = new Server(httpServer, options); | ||
const auth = require("./app/middleware/auth.js"); | ||
|
||
io.use(sharedsession(session, secretCookieParser, { autoSave: true })) | ||
.use((socket, next) => { | ||
baseCookieParser(socket.client.request, null, next); | ||
}) | ||
.use((socket, next) => { | ||
auth(socket, next); | ||
}); | ||
|
||
const filesHub = require("./app/hubs/files.js")(io); | ||
|
||
const countersHub = require('./app/hubs/counters.js')(io); | ||
const voipHub = require('./app/hubs/voip.js')(io); | ||
const chatHub = require('./app/hubs/chat.js')(io); | ||
const filesHub = require('./app/hubs/files.js')(io); | ||
app.use("/controller", require("./app/controllers")(filesHub)); | ||
|
||
app.use("/controller", require('./app/controllers')(countersHub, chatHub, voipHub, filesHub)); | ||
httpServer.listen(port, () => console.log(`Server started on port: ${port}`)); | ||
|
||
module.exports = app; | ||
module.exports = io; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.