我正在使用登录系统创建一个多人游戏。但是,有一个错误,我不知道如何解决。当我运行我的应用程序时,我收到以下错误: 净:: ERR_CONNECTION_CLOSED。
这是我的 app.js
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var exphbs = require('express-handlebars');
var expressValidator = require('express-validator');
var flash = require('connect-flash');
var session = require('express-session');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var mongo = require('mongodb');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/loginapp');
var db = mongoose.connection;
var routes = require('./routes/index');
var users = require('./routes/users');
// Init App
var app = express();
var http = require('http');
var server = require("http").Server(app);
var io = require("socket.io")(server);
// View Engine
app.set('views', path.join(__dirname, 'views'));
app.engine('handlebars', exphbs({defaultLayout:'layout'}));
app.set('view engine', 'handlebars');
// BodyParser Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
// Set Static Folder
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(__dirname + 'views'));
// Express Session
app.use(session({
secret: 'secret',
saveUninitialized: true,
resave: true
}));
// Passport init
app.use(passport.initialize());
app.use(passport.session());
// Express Validator
app.use(expressValidator({
errorFormatter: function(param, msg, value) {
var namespace = param.split('.')
, root = namespace.shift()
, formParam = root;
while(namespace.length) {
formParam += '[' + namespace.shift() + ']';
}
return {
param : formParam,
msg : msg,
value : value
};
}
}));
// Connect Flash
app.use(flash());
// Global Vars
app.use(function (req, res, next) {
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
res.locals.error = req.flash('error');
res.locals.user = req.user || null;
next();
});
app.use('/', routes);
app.use('/users', users);
app.set('port', (process.env.PORT || 3000));
io.on('connection', function(socket)
{
console.log('New connection attempt');
});
server.listen(app.get('port'), function(){
console.log('Server started on port '+app.get('port'));
});
我的 layout.handlebar
<!DOCTYPE html>
<html>
<head>
<link rel='stylesheet' href='/bootstrap/css/bootstrap1.css' />
<link rel = "stylesheet" href="/css/style.css" />
<title>IKTSL</title>
<style>
#question_options div button:nth-child(1){
visibility: visible;
}
</style>
<script src='/socket.io/socket.io.js'></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
</head>
<body>
<script>
var socket = io.connect('https://localhost:3000');
</script>
<div class="container">
<div class="row">
<div class="col-lg-12">
{{#if success_msg}}
<div class="alert alert-success">{{success_msg}}</div>
{{/if}}
{{#if error_msg}}
<div class="alert alert-danger">{{error_msg}}</div>
{{/if}}
{{#if error}}
<div class="alert alert-danger">{{error}}</div>
{{/if}}
</div>
</div>
</div> <!-- /container -->
<div class='container-fluid'>
<div class='row'>
<div class='span12'>
<br>
<br>
<h1>I Know This Song Lyrics!</h1>
<br>
</div>
</div>
<br>
<br>
<div class='row'>
<div class='span4'>
<div class='well'>
<div class="header clearfix">
<nav>
<ul class="nav nav-pills pull-right">
{{#if user}}
<li role="presentation"><a href="/">Dashboard</a></li>
<li role="presentation"><a href="/users/logout">Logout</a></li>
{{else}}
<li role="presentation"><a href="/users/login">Login</a></li>
<li role="presentation"><a href="/users/register">Register</a></li>
{{/if}}
</ul>
</nav>
<h3 class="text-muted">Log in</h3>
</div>
</div>
</div>
<div class='span8'>
<div class='hero-unit'>
{{{body}}}
</div>
</div>
<div class='span5'>
<div class='users'>
<h3>Players</h3>
<div class="connectedUsers"></div>
</div>
</div>
</div>
</body>
</html>
我只是试图控制日志&#34;新的连接尝试&#34;当我开始工作但这不起作用。 我会感激任何帮助。非常感谢。
答案 0 :(得分:0)
首先,我相信您可以更改为此(删除http的额外要求并设置io以收听服务器):
// Init App
var app = express();
var server = require("http").Server(app);
var io = require("socket.io").listen(server);
如果您的客户端运行在与服务器不同的端口上,我认为您也应该更改导入。否则,那件作品可能没问题。我认为你在var socket = io.connect()中不需要.o on io。无论如何,我的工作没有它。
<script src='http://localhost:3000/socket.io/socket.io.js'></script>