javascript - Cloud 9 IDE 上的 Socket.io - 警告 : error raised: Error: listen EACCES

标签 javascript node.js sockets express socket.io

我正在 Cloud 9 IDE 中进行开发。

这是我收到的错误:

警告:引发错误:错误:收听 EACCES

我正在使用 Cloud 9 指定在我的代码中用于监听的端口:process.env.PORT

这是我的代码:

//app.js Socket IO Test
var express = require("express"),
redis = require('socket.io/node_modules/redis'),
io = require('socket.io').listen(app);

var app = express();

var redisPort = [CENSORED]
  , hostname = [CENSORED]
  , password = [CENSORED]
  , db = 1;

var pub = redis.createClient(redisPort, hostname);
var sub = redis.createClient(redisPort, hostname);
var store = redis.createClient(redisPort, hostname);
pub.auth(password, function(){console.log("adentro! pub")});
sub.auth(password, function(){console.log("adentro! sub")});
store.auth(password, function(){console.log("adentro! store")});

io.configure( function(){
io.enable('browser client minification'); // send minified client
io.enable('browser client etag'); // apply etag caching logic based on version number
io.enable('browser client gzip'); // gzip the file
io.set('log level', 1); // reduce logging
io.set('transports', [ // enable all transports (optional if you want flashsocket)
'websocket'
, 'flashsocket'
, 'htmlfile'
, 'xhr-polling'
, 'jsonp-polling'
]);
var RedisStore = require('socket.io/lib/stores/redis');
io.set('store', new RedisStore({redisPub:pub, redisSub:sub, redisClient:store}));
});

app.listen(process.env.PORT);

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

var buffer = [];
io.sockets.on('connection', function(client){
var Room = "";
client.on("setNickAndRoom", function(nick, fn){
fn({msg : "Hello " + nick.nick});
client.join(nick.room);
Room = nick.room;
client.broadcast.to(Room).json.send({ msg: "Se conecto al room: " + nick.room, nick : nick });
});

client.on('message', function(message, fn){
var msg = message; //{ message: [client.sessionId, message] };
buffer.push(msg);
if (buffer.length > 15)
buffer.shift();
client.broadcast.to(Room).json.send(msg);
fn(msg);
});

client.on('disconnect', function(){
client.broadcast.to(Room).json.send({ msg: "Se desconecto"});
});
});

我不确定为什么在使用 Cloud 9 建议的端口时会出现此错误。

提前致谢!

最佳答案

难道你不想在让套接字监听它之前创建 app = express() 吗?

关于javascript - Cloud 9 IDE 上的 Socket.io - 警告 : error raised: Error: listen EACCES,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15416363/

相关文章:

javascript - 上传之前先剪切图片吗?

javascript - 将缓冲区作为不带空字节的 Uint8Array 传递

node.js - Gradle NPM Node 错误(npm'未被识别为内部或外部命令)

javascript - 如何在没有 ng-repeat 的情况下访问数据

Javascript:在特定数组结构上使用 array.sort()

javascript - 为什么无法尝试/捕捉某些东西?

javascript - 持续集成、dist 文件夹和 node_modules

c - accept() 套接字系统调用中的 "lvalue required as unary ' & ' operand"

c - 是否可以将套接字绑定(bind)到 c 中的 2 个地址?

c - 为什么 getaddrinfo 返回 HTTP 端口号 20480?