我在将socketIO 1.1设置为Heroku时遇到问题。我正在运行Express v4.2。 socketio在localhost上完美运行。但是,在Heroku上,它会出现此错误:
来自Heroku日志:
2014-09-15T09:40:14.578660+00:00 app[web.1]: GET /socket.io/?EIO=3&transport=polling&t=1410774014583-86 404 6ms - 744b
来自浏览器:
GET http://[mydomainname]/socket.io/?EIO=3&transport=polling&t=1410774080589-97 404 (Not Found) socket.io.js:2680Request.create socket.io.js:2680Request socket.io.js:2614XHR.request socket.io.js:2555XHR.doPoll socket.io.js:2585Polling.poll socket.io.js:2951Polling.doOpen socket.io.js:2895Transport.open socket.io.js:2106Socket.open socket.io.js:1580Socket socket.io.js:1467Socket socket.io.js:1419Manager.open.Manager.connect socket.io.js:272(anonymous function)
我已按照Socket IO文档中的建议配置进行操作。相关代码如下:
var express = require('express');
/* routes */
var routes = require('./routes/index');
var users = require('./routes/users');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var cons = require('consolidate');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.engine('dust', cons.dust);
app.set('view engine', 'dust');
app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser('hashionhashion'));
app.use(require('less-middleware')(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
//routing
app.use('/', routes);
app.use('/users', users);
/// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
/// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
//start the server
var server = require('http').Server(app);
var io = require('socket.io').listen(server);
var ioSockets = {};
io.on('connection', function(socket) {
ioSockets[socket.id] = socket;
socket.emit('welcome', {message: socket.id});
socket.on('disconnect', function(socket) {
delete ioSockets[socket.id];
});
});
exports.ioSockets = ioSockets;
module.exports = app;
server.listen(3000, function() {
console.log('Congrats, nothing broke!! Listening on port %d', server.address().port);
});
在我的客户端文件中,脚本如下:
<script src="my_path_to/socket.io.js"></script>
<script>
var socket = io.connect();
var socketId = '';
socket.on('welcome', function (data) {
socketId = data.message;
});
</script>
我还尝试过io.connect到"http://localhost"
,"http://localhost:3000"
和我的Heroku应用域。所有这些都会产生同样的错误。
任何帮助将不胜感激!
答案 0 :(得分:1)
问题最终是一个明确的问题,此后在4.10.0
之后的版本中得到纠正。
为了您自己的阅读: https://github.com/strongloop/express/issues/2406 https://github.com/jshttp/on-finished/issues/10
答案 1 :(得分:0)
看起来你改变了socket.io路径。因为我看到了src="my_path_to/socket.io.js"
。 socket.io尝试连接但得到了404。
是否有快递处理的路线'/socket.io /'?
即使您解决了404错误。你会遇到另一个错误。因为目前在heroku上,您必须将socket.io连接到https地址。否则socket.io将在轮询下工作。