我之前设置了我的应用程序以便它正常工作。我最近改变了几件事的顺序,现在它已经坏了。我希望有人知道使用socket.io(在heroku上)设置节点/表达应用程序的最佳方法。我想我错过了一些东西,因为我似乎无法让它发挥作用。
这是我当前打破的app.js:
var express = require('express'),
app = express.createServer(express.logger()),
io = require('socket.io'),
routes = require('./routes'),
chatroom = require('./routes/chatroom'),
http = require('http'),
path = require('path');
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
var socket = io.listen(app);
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.get('/', routes.index);
app.get('/room/:chatroom', chatroom.create_room);
app.get('/privateroom/:chatroom', chatroom.create_private_room);
var port = process.env.PORT || 3000;
app.listen(port);
答案 0 :(得分:1)
这就是我所拥有的
/**
* Module dependencies.
*/
var express = require('express')
, http = require('http')
, path = require('path')
, io = require('socket.io');
var app = express()
, server = http.createServer(app)
, io = io.listen(server);
var db = require('./server/db');
/**
* Config.
*/
app.configure(function() {
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/public/views');
app.set('view engine', 'ejs');
app.use(express.favicon(__dirname + '/public/images/favicon.ico'));
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('any-text'));
app.use(express.session({
secret: 'any-text'
}));
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function() {
app.use(function(req, res, next){
res.send(404, 'Sorry, page not found!');
});
app.use(express.errorHandler());
});
/**
* App.
*/
require('./server/routes')(app, db);
server.listen(app.get('port'), function() {
console.log("Server listening on port " + app.get('port'));
});
require('./server/sockets')(io, db);
有用的链接 https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x http://socket.io/#how-to-use