gpt4 book ai didi

node.js - socket.io net::ERR_CONNECTION_CLOSED

转载 作者:太空宇宙 更新时间:2023-11-03 22:30:48 28 4
gpt4 key购买 nike

我正在创建一个带有登录系统的多人游戏。但是,有一个错误,我不知道如何解决。当我运行我的应用程序时,出现以下错误:net::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>

当我开始工作时,我只是想控制台记录“新连接尝试”,但这不起作用。我将不胜感激任何帮助。预先非常感谢。

最佳答案

首先,我相信你可以更改为这个(删除http的额外要求并设置io来监听服务器):

// Init App
var app = express();
var server = require("http").Server(app);
var io = require("socket.io").listen(server);

如果您的客户端运行在与服务器不同的端口上,我认为您也应该更改导入。否则,那件作品可能还不错。我认为你不需要 var socket = io.connect(); 中的 io 上的 .connect无论如何,我的没有它也能工作。

<script src='http://localhost:3000/socket.io/socket.io.js'></script>

关于node.js - socket.io net::ERR_CONNECTION_CLOSED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37434982/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com