0

这是我的 app.js 的以下代码:

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

我研究过不能使用 WebSockets 并且必须以不同的方式配置它。我不确定我是否正确。任何人都可以发现代码中的任何错误吗?请记住,我现在正在学习这些东西。

4

1 回答 1

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,而 Express 在版本 3 发布时更改了其 API。注意这些错误:

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,但后来发生了变化。是从 Express 2 更改为 3 的内容列表。这是直接引用:

请记住 express() 的返回值不再是 http.Server 实例。

该方法在提交中已弃用。您也可能会发现这个感兴趣的提交。除此之外,websockets 确实无法在 Heroku 上运行,因此您的设置是正确的。

于 2013-05-17T19:02:22.147 回答