Skip to content

Commit

Permalink
Web: Socket.IO: Refactoring work with socket.io
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeySafronov committed Dec 20, 2021
1 parent b2cb05b commit 99e37d9
Show file tree
Hide file tree
Showing 26 changed files with 5,170 additions and 1,622 deletions.
145 changes: 0 additions & 145 deletions common/ASC.Socket.IO/ASC.Socket.IO.njsproj

This file was deleted.

149 changes: 81 additions & 68 deletions common/ASC.Socket.IO/app.js
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;
23 changes: 0 additions & 23 deletions common/ASC.Socket.IO/app/controllers/chat.js

This file was deleted.

15 changes: 0 additions & 15 deletions common/ASC.Socket.IO/app/controllers/counters.js

This file was deleted.

Loading

0 comments on commit 99e37d9

Please sign in to comment.