0

I cannot connect to the socket.io using cloud9 for some reason.

node app.js

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

app.configure(function(){
    app.set('domain', process.env.IP);
    app.set('port', process.env.PORT);
    app.use(express.urlencoded());
    app.use(express.json());
    app.use(express.methodOverride());
    app.use(express.compress());
    app.use(express.responseTime());
//    app.use(cors({credentials: false}));
    app.use(function(req, res, next) {
        res.header("Access-Control-Allow-Origin", "http://run.plnkr.co");
        res.header("Access-Control-Allow-Headers", "X-Requested-With");
        res.header("Access-Control-Allow-Credentials", true);
        next();
    });
    app.use(express.static(path.join(__dirname, '/html')));
});


app.use(function(err, req, res, next){
    console.error(err.stack);
    res.send(500, 'Something broke!');
});

// routes
app.use(app.router);

//sockets
io.sockets.on('connection', function(sock){
    console.log('connected!', sock);
    setInterval(function () {
        sock.emit('send:time', {
            time: (new Date()).toString()
        });
    }, 1000);
});

io.sockets.on('error', function(data) {
    console.log('error', data, arguments);
});

// start app
app.listen(app.get('port'));

Client side app.js (angular)

var app = angular.module('plunker', []);

app.controller('MainCtrl', ['$scope', 'socket', function($scope, socket) {
        socket.on('send:version', function (data) {
            $log.log('send:version', data);
            $scope.version = data.version;
        });

        socket.on('send:time', function (data) {
            $log.log('send:time', data);
            $scope.time = data.time;
        });
}]);

app.factory('socket', [
    '$rootScope'
    , '$log'
    , function (
        $rootScope
        , $log
        ) {

        var socket = io.connect('https://demo-project-c9-chovy.c9.io');

        $log.log('socket', io, socket);

        socket.on('connect', function() {
            $log.log('Connected!');
        });

        socket.on('error', function() {
            $log.log('error!', arguments);
        });

        return {
            on: function (eventName, callback) {
                socket.on(eventName, function () {
                    var args = arguments;
                    $rootScope.$apply(function () {
                        callback.apply(socket, args);
                    });
                });
            },
            emit: function (eventName, data, callback) {
                socket.emit(eventName, data, function () {
                    var args = arguments;
                    $rootScope.$apply(function () {
                        if (callback) {
                            callback.apply(socket, args);
                        }
                    });
                })
            }
        };
}]);

I suspect the problem is that cloud9 uses a proxy and can't server the socket.io/1 resource.

Here is how I'm connecting from angular:

    var socket = io.connect('https://demo-project-c9-chovy.c9.io');

If I do this, I get a 404.

If I use port 8080 I eventually get a socket error event on the client side.

chovy
  • 72,281
  • 52
  • 227
  • 295

1 Answers1

0

I needed server.listen(port, cb) instead of app.listen(port) in the node app.js file.

chovy
  • 72,281
  • 52
  • 227
  • 295