2013-05-17 93 views
0

超時,這是我app.js的下面的代碼:Socket.io保持在Heroku

var port = process.env.PORT || 3000; 

var app = require('express').createServer(); 
var io = require('socket.io').listen(app); 

app.listen(port); 

io.configure(function() { 
    io.set("transports", ["xhr-polling"]); 
    io.set("polling duration", 10); 
}); 

var spaces = {}; 

app.get('/', function (req, res) { 
    res.sendfile(__dirname + '/view.html'); 
}); 

app.get('/img/rocket.svg', function (req, res) { 
    res.sendfile(__dirname + '/img/rocket.svg'); 
}); 

app.get('/:spaceid', function (req, res) { 
    res.sendfile(__dirname + '/control.html'); 
}); 

io.sockets.on('connection', function (socket) { 

    socket.on('serve', function(data) { 
    spaces[data.spaceId] = socket; 
    }); 

    socket.on('control', function (data) { 
    var spaceSocket = spaces[data.spaceId]; 
    if (spaceSocket) { 
     spaceSocket.emit('control', data); 
    } 
    }); 

}); 

這是我所得到的,當我做heroku logs

2013-05-17T18:18:06.873629+00:00 heroku[web.1]: Starting process with command `node app.js` 
2013-05-17T18:18:07+00:00 app[web.1]: Warning: express.createServer() is deprecated, express 
2013-05-17T18:18:08+00:00 app[web.1]: applications no longer inherit from http.Server, 
2013-05-17T18:18:08+00:00 app[web.1]: please use: 
2013-05-17T18:18:08+00:00 app[web.1]: 
2013-05-17T18:18:08+00:00 app[web.1]: var express = require("express"); 
2013-05-17T18:18:08+00:00 app[web.1]: var app = express(); 
2013-05-17T18:18:08+00:00 app[web.1]: 
2013-05-17T18:18:09+00:00 app[web.1]: Socket.IO's `listen()` method expects an `http.Server` instance 
2013-05-17T18:18:09+00:00 app[web.1]: as its first parameter. Are you migrating from Express 2.x to 3.x? 
2013-05-17T18:18:08.076300+00:00 heroku[web.1]: State changed from starting to up 
2013-05-17T18:18:09+00:00 app[web.1]: If so, check out the "Socket.IO compatibility" section at: 
2013-05-17T18:18:09+00:00 app[web.1]: https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x 
2013-05-17T18:18:09+00:00 app[web.1]: info: socket.io started 
2013-05-17T18:18:09.745293+00:00 heroku[router]: at=error code=H18 desc="Request Interrupted" method=GET path=/ host=myapp123.herokuapp.com fwd="80.47.193.237" dyno=web.1 connect=1ms service=0ms status=503 bytes=0 sock=client 
2013-05-17T18:18:11.428152+00:00 heroku[router]: at=error code=H18 desc="Request Interrupted" method=GET path=/ host=myapp123.herokuapp.com fwd="80.47.193.237" dyno=web.1 connect=1ms service=0ms status=503 bytes=0 sock=client 

我研究的是不能使用WebSocket並必須以不同的方式進行配置。我不確定我是否正確。任何人都可以在代碼中發現任何錯誤?請記住我目前正在學習這些東西。

回答

0

您的問題正是錯誤狀態。使用這個來代替:

var express = require('express'); 
var app = express(); 
var http = require('http') 
var server = http.createServer(app) 
var io = require('socket.io').listen(server); 
server.listen(80); 

與應用程序的問題是,socket.io正在尋找的http.Server一個實例,並快速改變了它的API版本3發佈的時候。注意這些錯誤:

Warning: express.createServer() is deprecated, express 
applications no longer inherit from http.Server 

Socket.IO's `listen()` method expects an `http.Server` instance 
as its first parameter. Are you migrating from Express 2.x to 3.x? 

啓動一個應用程序,你表現出本來返回http.Server實例的方式,但已經改變了。 Here是事物從快2改爲3名單這裏的直引號:

請記住,明確的返回值()不再是 http.Server實例。

該方法在this提交中已棄用。您也可能會發現this感興趣。除此之外,websockets確實無法在Heroku上運行,所以你的設置是正確的。

+0

找到電腦有什麼好運氣? – orange

+0

已更新的答案。這實際上是不正確的 - 我懶得去看我自己的來源。 – hexacyanide