Socket.io连接连接很多次,我想应该是只有一次
我建立了一个Web应用程序,基于WES BOS课程:qazxsw POI
现在,我想添加一个简单的消息传递系统,所以我尝试添加socket.io
在他们的网站::
/
所以在我的start.js文件我想它上面适应代码,并部分工作,只有在其网站上说,它只是它应该为每个连接的用户登录一次。
而在我的情况下,它像记录每一帧。
任何想法有什么不好?
这是我的start.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
console.log('a user connected');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
编辑:这也是我ap.js:
const app = require('./app');
app.set('port', process.env.PORT || 7777);
const server = app.listen(app.get('port'), () => {
console.log(`Express running → PORT ${server.address().port}`);
});
// const http = require('http').Server(app);
const io = require('socket.io').listen(server);
io.on('connection', function(socket){
console.log('a user connected');
});
回答如下:
您需要删除以下行:
// create our Express app
const app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views')); // this is the folder where we keep our pug files
app.set('view engine', 'pug'); // we use the engine pug, mustache or EJS work great too
// serves up static files from the public folder. Anything in public/ will just be served up as the file it is
app.use(express.static(path.join(__dirname, 'public')));
// Takes the raw requests and turns them into usable properties on req.body
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Exposes a bunch of methods for validating data. Used heavily on userController.validateRegister
app.use(expressValidator());
// populates req.cookies with any cookies that came along with the request
app.use(cookieParser());
// Sessions allow us to store data on visitors from request to request
// This keeps users logged in and allows us to send flash messages
app.use(session({
secret: process.env.SECRET,
key: process.env.KEY,
resave: false,
saveUninitialized: false,
store: new MongoStore({ mongooseConnection: mongoose.connection })
}));
// // Passport JS is what we use to handle our logins
app.use(passport.initialize());
app.use(passport.session());
// // The flash middleware let's us use req.flash('error', 'Shit!'), which will then pass that message to the next page the user requests
app.use(flash());
// pass variables to our templates + all requests
app.use((req, res, next) => {
res.locals.h = helpers;
res.locals.flashes = req.flash();
res.locals.user = req.user || null;
res.locals.currentPath = req.path;
next();
});
// promisify some callback based APIs
app.use((req, res, next) => {
req.login = promisify(req.login, req);
next();
});
// After allllll that above middleware, we finally handle our own routes!
app.use('/', routes);
// If that above routes didnt work, we 404 them and forward to error handler
app.use(errorHandlers.notFound);
// One of our error handlers will see if these errors are just validation errors
app.use(errorHandlers.flashValidationErrors);
// Otherwise this was a really bad error we didn't expect! Shoot eh
if (app.get('env') === 'development') {
/* Development Error Handler - Prints stack trace */
app.use(errorHandlers.developmentErrors);
}
// production error handler
app.use(errorHandlers.productionErrors);
// done! we export it so we can start the site in start.js
module.exports = app;
您正在收听这里,但你应该这样做:
const server = app.listen(app.get('port'), () => {
console.log(`Express running → PORT ${server.address().port}`);
});
或与您通过快递设置端口的情况下:
const http = require('http').Server(app);
http.listen(3000, function(){
console.log('listening on *:3000');
});
更常见的语法来声明你端口作为一个const:
const http = require('http').Server(app);
http.listen(app.get('port'), function(){
console.log(`Express running → PORT ${server.address().port}`);
});
反过来,你的socket.io服务器将创建为:
const PORT = 7777
const http = require('http').Server(app);
http.listen(PORT, function(){
console.log(`Express running → PORT ${PORT}`);
});
因为它代表的是:(从我可以告诉)创建两个服务器。
编辑:
整个start.js应该是这样的:
const io = require('socket.io')(http);
Socket.io连接连接很多次,我想应该是只有一次
我建立了一个Web应用程序,基于WES BOS课程:qazxsw POI
现在,我想添加一个简单的消息传递系统,所以我尝试添加socket.io
在他们的网站::
/
所以在我的start.js文件我想它上面适应代码,并部分工作,只有在其网站上说,它只是它应该为每个连接的用户登录一次。
而在我的情况下,它像记录每一帧。
任何想法有什么不好?
这是我的start.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
console.log('a user connected');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
编辑:这也是我ap.js:
const app = require('./app');
app.set('port', process.env.PORT || 7777);
const server = app.listen(app.get('port'), () => {
console.log(`Express running → PORT ${server.address().port}`);
});
// const http = require('http').Server(app);
const io = require('socket.io').listen(server);
io.on('connection', function(socket){
console.log('a user connected');
});
回答如下:
您需要删除以下行:
// create our Express app
const app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views')); // this is the folder where we keep our pug files
app.set('view engine', 'pug'); // we use the engine pug, mustache or EJS work great too
// serves up static files from the public folder. Anything in public/ will just be served up as the file it is
app.use(express.static(path.join(__dirname, 'public')));
// Takes the raw requests and turns them into usable properties on req.body
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Exposes a bunch of methods for validating data. Used heavily on userController.validateRegister
app.use(expressValidator());
// populates req.cookies with any cookies that came along with the request
app.use(cookieParser());
// Sessions allow us to store data on visitors from request to request
// This keeps users logged in and allows us to send flash messages
app.use(session({
secret: process.env.SECRET,
key: process.env.KEY,
resave: false,
saveUninitialized: false,
store: new MongoStore({ mongooseConnection: mongoose.connection })
}));
// // Passport JS is what we use to handle our logins
app.use(passport.initialize());
app.use(passport.session());
// // The flash middleware let's us use req.flash('error', 'Shit!'), which will then pass that message to the next page the user requests
app.use(flash());
// pass variables to our templates + all requests
app.use((req, res, next) => {
res.locals.h = helpers;
res.locals.flashes = req.flash();
res.locals.user = req.user || null;
res.locals.currentPath = req.path;
next();
});
// promisify some callback based APIs
app.use((req, res, next) => {
req.login = promisify(req.login, req);
next();
});
// After allllll that above middleware, we finally handle our own routes!
app.use('/', routes);
// If that above routes didnt work, we 404 them and forward to error handler
app.use(errorHandlers.notFound);
// One of our error handlers will see if these errors are just validation errors
app.use(errorHandlers.flashValidationErrors);
// Otherwise this was a really bad error we didn't expect! Shoot eh
if (app.get('env') === 'development') {
/* Development Error Handler - Prints stack trace */
app.use(errorHandlers.developmentErrors);
}
// production error handler
app.use(errorHandlers.productionErrors);
// done! we export it so we can start the site in start.js
module.exports = app;
您正在收听这里,但你应该这样做:
const server = app.listen(app.get('port'), () => {
console.log(`Express running → PORT ${server.address().port}`);
});
或与您通过快递设置端口的情况下:
const http = require('http').Server(app);
http.listen(3000, function(){
console.log('listening on *:3000');
});
更常见的语法来声明你端口作为一个const:
const http = require('http').Server(app);
http.listen(app.get('port'), function(){
console.log(`Express running → PORT ${server.address().port}`);
});
反过来,你的socket.io服务器将创建为:
const PORT = 7777
const http = require('http').Server(app);
http.listen(PORT, function(){
console.log(`Express running → PORT ${PORT}`);
});
因为它代表的是:(从我可以告诉)创建两个服务器。
编辑:
整个start.js应该是这样的:
const io = require('socket.io')(http);