如何在OpenShift中部署Nodejs -Socket IO项目

时间:2014-10-14 12:10:09

标签: node.js socket.io openshift

我在openshift中部署了nodejs项目。 请在下面找到我的package.json和server,js

的package.json

{
  "name": "OpenShift-Sample-App",
  "version": "1.0.0",
  "description": "OpenShift Sample Application",
  "keywords": [
    "OpenShift",
    "Node.js",
    "application",
    "openshift"
  ],
  "author": {
    "name": "OpenShift",
    "email": "ramr@example.org",
    "url": "http://www.openshift.com/"
  },
  "homepage": "http://www.openshift.com/",
  "repository": {
    "type": "git",
    "url": "https://github.com/openshift/origin-server"
  },

  "engines": {
    "node": ">= 0.6.0",
    "npm": ">= 1.0.0"
  },

  "dependencies": {
    "socket.io" : "1.0.6",
   "express" : "4.7.2"
  },
  "devDependencies": {},
  "bundleDependencies": [],

  "private": true,
  "main": "server.js"
}

Server.js

var express = require('express');
var fs      = require('fs');


/**
 *  Define the sample application.
 */
var SampleApp = function() {

    //  Scope.
    var self = this;


    /*  ================================================================  */
    /*  Helper functions.                                                 */
    /*  ================================================================  */

    /**
     *  Set up server IP address and port # using env variables/defaults.
     */
    self.setupVariables = function() {
        //  Set the environment variables we need.
        self.ipaddress = process.env.OPENSHIFT_NODEJS_IP;
        self.port      = process.env.OPENSHIFT_NODEJS_PORT || 8080;

        if (typeof self.ipaddress === "undefined") {
            //  Log errors on OpenShift but continue w/ 127.0.0.1 - this
            //  allows us to run/test the app locally.
            console.warn('No OPENSHIFT_NODEJS_IP var, using 127.0.0.1');
            self.ipaddress = "127.0.0.1";
        };
    };


    /**
     *  Populate the cache.
     */
    self.populateCache = function() {
        if (typeof self.zcache === "undefined") {
            self.zcache = { 'index.html': '' };
        }

        //  Local cache for static content.
        self.zcache['index.html'] = fs.readFileSync('./index.html');
    };


    /**
     *  Retrieve entry (content) from cache.
     *  @param {string} key  Key identifying content to retrieve from cache.
     */
    self.cache_get = function(key) { return self.zcache[key]; };


    /**
     *  terminator === the termination handler
     *  Terminate server on receipt of the specified signal.
     *  @param {string} sig  Signal to terminate on.
     */
    self.terminator = function(sig){
        if (typeof sig === "string") {
           console.log('%s: Received %s - terminating sample app ...',
                       Date(Date.now()), sig);
           process.exit(1);
        }
        console.log('%s: Node server stopped.', Date(Date.now()) );
    };


    /**
     *  Setup termination handlers (for exit and a list of signals).
     */
    self.setupTerminationHandlers = function(){
        //  Process on exit and signals.
        process.on('exit', function() { self.terminator(); });

        // Removed 'SIGPIPE' from the list - bugz 852598.
        ['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT',
         'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGTERM'
        ].forEach(function(element, index, array) {
            process.on(element, function() { self.terminator(element); });
        });
    };


    /*  ================================================================  */
    /*  App server functions (main app logic here).                       */
    /*  ================================================================  */

    /**
     *  Create the routing table entries + handlers for the application.
     */
    self.createRoutes = function() {
        self.routes = { };

        self.routes['/asciimo'] = function(req, res) {
            var link = "http://i.imgur.com/kmbjB.png";
            res.send("<html><body><img src='" + link + "'></body></html>");
        };

        self.routes['/'] = function(req, res) {
            res.setHeader('Content-Type', 'text/html');
            res.send(self.cache_get('index.html') );
        };
    };


    /**
     *  Initialize the server (express) and create the routes and register
     *  the handlers.
     */
    self.initializeServer = function() {
        self.createRoutes();
        self.app = express.createServer();
      self.app.use(express.static(__dirname + '/public'));
        //  Add handlers for the app (from the routes).
        for (var r in self.routes) {
            self.app.get(r, self.routes[r]);
        }
    };


    /**
     *  Initializes the sample application.
     */
    self.initialize = function() {
        self.setupVariables();
        self.populateCache();
        self.setupTerminationHandlers();

        // Create the express server and routes.
        self.initializeServer();

         self.initializeSocketIO().addSocketIOEvents();
    };
// socket.io initialization on the server side
    self.initializeSocketIO = function() {
            self.server = require('http').createServer(self.app);
            self.io = require('socket.io').listen(self.server);
            self.io.enable('browser client minification');  // send minified client
            self.io.enable('browser client etag');          // apply etag caching logic based on version number
            self.io.enable('browser client gzip');          // gzip the file
            self.io.set('log level', 1);                    // reduce logging

            self.io.set('transports', [
                    'websocket'
                ]);
            return this;
        }

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




                   socket.on('onmouse move',function(data){
                            socket.broadcast.emit('mouse move',data);
                    });

                    socket.on('onmouse down',function(data){
                            socket.broadcast.emit('mouse down',data);
                    });

                    socket.on('onmouse wheel',function(data){
                            socket.broadcast.emit('mouse wheel',data);
    });

        });
}



    /**
     *  Start the server (starts up the sample application).
     */
    self.start = function() {
        //  Start the app on the specific interface (and port).
        self.app.listen(self.port, self.ipaddress, function() {
            console.log('%s: Node server started on %s:%d ...',
                        Date(Date.now() ), self.ipaddress, self.port);
        });
    };

};   /*  Sample Application.  */



/**
 *  main():  Main code.
 */
var zapp = new SampleApp();
zapp.initialize();
zapp.start();

git推后。我得到了以下信息。

remote: npm info install json3@3.2.6
remote: npm info postinstall json3@3.2.6
remote: npm info build /var/lib/openshift/543d046250044666dc0002f6/app-root/runt
ime/repo/node_modules/socket.io/node_modules/socket.io-adapter/node_modules/sock
et.io-parser
remote: npm info linkStuff socket.io-parser@2.1.2
remote: npm info install socket.io-parser@2.1.2
remote: npm info postinstall socket.io-parser@2.1.2
remote: npm info build /var/lib/openshift/543d046250044666dc0002f6/app-root/runt
ime/repo/node_modules/socket.io/node_modules/socket.io-adapter
remote: npm info linkStuff socket.io-adapter@0.2.0
remote: npm info install socket.io-adapter@0.2.0
remote: npm info postinstall socket.io-adapter@0.2.0
remote: npm info preinstall base64-arraybuffer@0.1.2
remote: npm info build /var/lib/openshift/543d046250044666dc0002f6/app-root/runt
ime/repo/node_modules/socket.io/node_modules/socket.io-client/node_modules/engin
e.io-client/node_modules/engine.io-parser/node_modules/base64-arraybuffer
remote: npm info linkStuff base64-arraybuffer@0.1.2
remote: npm info install base64-arraybuffer@0.1.2
remote: npm info postinstall base64-arraybuffer@0.1.2
remote: npm info install tinycolor@0.0.1 into /var/lib/openshift/543d04625004466
6dc0002f6/app-root/runtime/repo/node_modules/socket.io/node_modules/engine.io/no
de_modules/ws
remote: npm info install options@0.0.6 into /var/lib/openshift/543d046250044666d
c0002f6/app-root/runtime/repo/node_modules/socket.io/node_modules/engine.io/node
_modules/ws
remote: npm info install commander@0.6.1 into /var/lib/openshift/543d04625004466
6dc0002f6/app-root/runtime/repo/node_modules/socket.io/node_modules/engine.io/no
de_modules/ws
remote: npm info install nan@0.3.2 into /var/lib/openshift/543d046250044666dc000
2f6/app-root/runtime/repo/node_modules/socket.io/node_modules/engine.io/node_mod
ules/ws
remote: npm info installOne tinycolor@0.0.1
remote: npm info installOne options@0.0.6
remote: npm info installOne commander@0.6.1
remote: npm info installOne nan@0.3.2
remote: npm info preinstall global@2.0.1
remote: npm info /var/lib/openshift/543d046250044666dc0002f6/app-root/runtime/re
po/node_modules/socket.io/node_modules/engine.io/node_modules/ws/node_modules/ti
nycolor unbuild
remote: npm info /var/lib/openshift/543d046250044666dc0002f6/app-root/runtime/re
po/node_modules/socket.io/node_modules/engine.io/node_modules/ws/node_modules/op
tions unbuild
remote: npm info /var/lib/openshift/543d046250044666dc0002f6/app-root/runtime/re
po/node_modules/socket.io/node_modules/engine.io/node_modules/ws/node_modules/co
mmander unbuild
remote: npm info /var/lib/openshift/543d046250044666dc0002f6/app-root/runtime/re
po/node_modules/socket.io/node_modules/engine.io/node_modules/ws/node_modules/na
n unbuild
remote: npm info build /var/lib/openshift/543d046250044666dc0002f6/app-root/runt
ime/repo/node_modules/socket.io/node_modules/socket.io-client/node_modules/engin
e.io-client/node_modules/has-cors/node_modules/global
remote: npm info linkStuff global@2.0.1
remote: npm info install global@2.0.1
remote: npm info postinstall global@2.0.1
remote: npm info build /var/lib/openshift/543d046250044666dc0002f6/app-root/runt
ime/repo/node_modules/socket.io/node_modules/socket.io-client/node_modules/engin
e.io-client/node_modules/has-cors
remote: npm info linkStuff has-cors@1.0.3
remote: npm info install has-cors@1.0.3
remote: npm info postinstall has-cors@1.0.3
remote: npm info preinstall utf8@2.0.0
remote: npm info build /var/lib/openshift/543d046250044666dc0002f6/app-root/runt
ime/repo/node_modules/socket.io/node_modules/engine.io/node_modules/engine.io-pa
rser/node_modules/utf8
remote: npm info linkStuff utf8@2.0.0
remote: npm info install utf8@2.0.0
remote: npm info postinstall utf8@2.0.0
remote: npm info preinstall tinycolor@0.0.1
remote: npm info build /var/lib/openshift/543d046250044666dc0002f6/app-root/runt
ime/repo/node_modules/socket.io/node_modules/engine.io/node_modules/engine.io-pa
rser
remote: npm info linkStuff engine.io-parser@1.0.6
remote: npm info install engine.io-parser@1.0.6
remote: npm info build /var/lib/openshift/543d046250044666dc0002f6/app-root/runt
ime/repo/node_modules/socket.io/node_modules/engine.io/node_modules/ws/node_modu
les/tinycolor
remote: npm info linkStuff tinycolor@0.0.1
remote: npm info postinstall engine.io-parser@1.0.6
remote: npm info install tinycolor@0.0.1
remote: npm info postinstall tinycolor@0.0.1
remote: npm info preinstall options@0.0.6
remote: npm info build /var/lib/openshift/543d046250044666dc0002f6/app-root/runt
ime/repo/node_modules/socket.io/node_modules/engine.io/node_modules/ws/node_modu
les/options
remote: npm info linkStuff options@0.0.6
remote: npm info install options@0.0.6
remote: npm info postinstall options@0.0.6
remote: npm info preinstall commander@0.6.1
remote: npm info build /var/lib/openshift/543d046250044666dc0002f6/app-root/runt
ime/repo/node_modules/socket.io/node_modules/engine.io/node_modules/ws/node_modu
les/commander
remote: npm info linkStuff commander@0.6.1
remote: npm info install commander@0.6.1
remote: npm info postinstall commander@0.6.1
remote: npm info preinstall nan@0.3.2
remote: npm info build /var/lib/openshift/543d046250044666dc0002f6/app-root/runt
ime/repo/node_modules/socket.io/node_modules/engine.io/node_modules/ws/node_modu
les/nan
remote: npm info linkStuff nan@0.3.2
remote: npm info install nan@0.3.2
remote: npm info postinstall nan@0.3.2
remote: npm info build /var/lib/openshift/543d046250044666dc0002f6/app-root/runt
ime/repo/node_modules/socket.io/node_modules/engine.io/node_modules/ws
remote: npm info linkStuff ws@0.4.31
remote: npm info install ws@0.4.31
remote:
remote: > ws@0.4.31 install /var/lib/openshift/543d046250044666dc0002f6/app-root
/runtime/repo/node_modules/socket.io/node_modules/engine.io/node_modules/ws
remote: > (node-gyp rebuild 2> builderror.log) || (exit 0)
remote:
remote: make: Entering directory `/var/lib/openshift/543d046250044666dc0002f6/ap
p-root/runtime/repo/node_modules/socket.io/node_modules/engine.io/node_modules/w
s/build'
remote:   CXX(target) Release/obj.target/bufferutil/src/bufferutil.o
remote:   SOLINK_MODULE(target) Release/obj.target/bufferutil.node
remote:   SOLINK_MODULE(target) Release/obj.target/bufferutil.node: Finished
remote:   COPY Release/bufferutil.node
remote:   CXX(target) Release/obj.target/validation/src/validation.o
remote:   SOLINK_MODULE(target) Release/obj.target/validation.node
remote:   SOLINK_MODULE(target) Release/obj.target/validation.node: Finished
remote:   COPY Release/validation.node
remote: make: Leaving directory `/var/lib/openshift/543d046250044666dc0002f6/app
-root/runtime/repo/node_modules/socket.io/node_modules/engine.io/node_modules/ws
/build'
remote: npm info preinstall ws@0.4.31
remote: npm info install commander@0.6.1 into /var/lib/openshift/543d04625004466
6dc0002f6/app-root/runtime/repo/node_modules/socket.io/node_modules/socket.io-cl
ient/node_modules/engine.io-client/node_modules/ws
remote: npm info install nan@0.3.2 into /var/lib/openshift/543d046250044666dc000
2f6/app-root/runtime/repo/node_modules/socket.io/node_modules/socket.io-client/n
ode_modules/engine.io-client/node_modules/ws
remote: npm info install options@0.0.6 into /var/lib/openshift/543d046250044666d
c0002f6/app-root/runtime/repo/node_modules/socket.io/node_modules/socket.io-clie
nt/node_modules/engine.io-client/node_modules/ws
remote: npm info install tinycolor@0.0.1 into /var/lib/openshift/543d04625004466
6dc0002f6/app-root/runtime/repo/node_modules/socket.io/node_modules/socket.io-cl
ient/node_modules/engine.io-client/node_modules/ws
remote: npm info installOne commander@0.6.1
remote: npm info installOne nan@0.3.2
remote: npm info installOne options@0.0.6
remote: npm info installOne tinycolor@0.0.1
remote: npm info /var/lib/openshift/543d046250044666dc0002f6/app-root/runtime/re
po/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-c
lient/node_modules/ws/node_modules/commander unbuild
remote: npm info /var/lib/openshift/543d046250044666dc0002f6/app-root/runtime/re
po/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-c
lient/node_modules/ws/node_modules/nan unbuild
remote: npm info /var/lib/openshift/543d046250044666dc0002f6/app-root/runtime/re
po/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-c
lient/node_modules/ws/node_modules/options unbuild
remote: npm info /var/lib/openshift/543d046250044666dc0002f6/app-root/runtime/re
po/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-c
lient/node_modules/ws/node_modules/tinycolor unbuild
remote: npm info preinstall utf8@2.0.0
remote: npm info preinstall tinycolor@0.0.1
remote: npm info build /var/lib/openshift/543d046250044666dc0002f6/app-root/runt
ime/repo/node_modules/socket.io/node_modules/socket.io-client/node_modules/engin
e.io-client/node_modules/engine.io-parser/node_modules/utf8
remote: npm info linkStuff utf8@2.0.0
remote: npm info install utf8@2.0.0
remote: npm info build /var/lib/openshift/543d046250044666dc0002f6/app-root/runt
ime/repo/node_modules/socket.io/node_modules/socket.io-client/node_modules/engin
e.io-client/node_modules/ws/node_modules/tinycolor
remote: npm info linkStuff tinycolor@0.0.1
remote: npm info install tinycolor@0.0.1
remote: npm info postinstall utf8@2.0.0
remote: npm info postinstall tinycolor@0.0.1
remote: npm info build /var/lib/openshift/543d046250044666dc0002f6/app-root/runt
ime/repo/node_modules/socket.io/node_modules/socket.io-client/node_modules/engin
e.io-client/node_modules/engine.io-parser
remote: npm info linkStuff engine.io-parser@1.0.6
remote: npm info install engine.io-parser@1.0.6
remote: npm info preinstall options@0.0.6
remote: npm info postinstall engine.io-parser@1.0.6
remote: npm info build /var/lib/openshift/543d046250044666dc0002f6/app-root/runt
ime/repo/node_modules/socket.io/node_modules/socket.io-client/node_modules/engin
e.io-client/node_modules/ws/node_modules/options
remote: npm info linkStuff options@0.0.6
remote: npm info install options@0.0.6
remote: npm info postinstall options@0.0.6
remote: npm info preinstall commander@0.6.1
remote: npm info build /var/lib/openshift/543d046250044666dc0002f6/app-root/runt
ime/repo/node_modules/socket.io/node_modules/socket.io-client/node_modules/engin
e.io-client/node_modules/ws/node_modules/commander
remote: npm info linkStuff commander@0.6.1
remote: npm info install commander@0.6.1
remote: npm info postinstall commander@0.6.1
remote: npm info preinstall nan@0.3.2
remote: npm info build /var/lib/openshift/543d046250044666dc0002f6/app-root/runt
ime/repo/node_modules/socket.io/node_modules/socket.io-client/node_modules/engin
e.io-client/node_modules/ws/node_modules/nan
remote: npm info linkStuff nan@0.3.2
remote: npm info install nan@0.3.2
remote: npm info postinstall nan@0.3.2
remote: npm info build /var/lib/openshift/543d046250044666dc0002f6/app-root/runt
ime/repo/node_modules/socket.io/node_modules/socket.io-client/node_modules/engin
e.io-client/node_modules/ws
remote: npm info linkStuff ws@0.4.31
remote: npm info install ws@0.4.31
remote:
remote: > ws@0.4.31 install /var/lib/openshift/543d046250044666dc0002f6/app-root
/runtime/repo/node_modules/socket.io/node_modules/socket.io-client/node_modules/
engine.io-client/node_modules/ws
remote: > (node-gyp rebuild 2> builderror.log) || (exit 0)
remote:
remote: make: Entering directory `/var/lib/openshift/543d046250044666dc0002f6/ap
p-root/runtime/repo/node_modules/socket.io/node_modules/socket.io-client/node_mo
dules/engine.io-client/node_modules/ws/build'
remote:   CXX(target) Release/obj.target/bufferutil/src/bufferutil.o
remote:   SOLINK_MODULE(target) Release/obj.target/bufferutil.node
remote:   SOLINK_MODULE(target) Release/obj.target/bufferutil.node: Finished
remote:   COPY Release/bufferutil.node
remote:   CXX(target) Release/obj.target/validation/src/validation.o
remote:   SOLINK_MODULE(target) Release/obj.target/validation.node
remote:   SOLINK_MODULE(target) Release/obj.target/validation.node: Finished
remote:   COPY Release/validation.node
remote: make: Leaving directory `/var/lib/openshift/543d046250044666dc0002f6/app
-root/runtime/repo/node_modules/socket.io/node_modules/socket.io-client/node_mod
ules/engine.io-client/node_modules/ws/build'
remote: npm info postinstall ws@0.4.31
remote: npm info build /var/lib/openshift/543d046250044666dc0002f6/app-root/runt
ime/repo/node_modules/socket.io/node_modules/engine.io
remote: npm info linkStuff engine.io@1.3.1
remote: npm info install engine.io@1.3.1
remote: npm info postinstall engine.io@1.3.1
remote: npm info postinstall ws@0.4.31
remote: npm info build /var/lib/openshift/543d046250044666dc0002f6/app-root/runt
ime/repo/node_modules/socket.io/node_modules/socket.io-client/node_modules/engin
e.io-client
remote: npm info linkStuff engine.io-client@1.3.1
remote: npm info install engine.io-client@1.3.1
remote: npm info postinstall engine.io-client@1.3.1
remote: npm info build /var/lib/openshift/543d046250044666dc0002f6/app-root/runt
ime/repo/node_modules/socket.io/node_modules/socket.io-client
remote: npm info linkStuff socket.io-client@1.0.6
remote: npm info install socket.io-client@1.0.6
remote: npm info postinstall socket.io-client@1.0.6
remote: npm info build /var/lib/openshift/543d046250044666dc0002f6/app-root/runt
ime/repo/node_modules/socket.io
remote: npm info linkStuff socket.io@1.0.6
remote: npm info install socket.io@1.0.6
remote: npm info postinstall socket.io@1.0.6
remote: npm info build /var/lib/openshift/543d046250044666dc0002f6/app-root/runt
ime/repo
remote: npm info linkStuff OpenShift-Sample-App@1.0.0
remote: npm info install OpenShift-Sample-App@1.0.0
remote: npm info postinstall OpenShift-Sample-App@1.0.0
remote: npm info prepublish OpenShift-Sample-App@1.0.0
remote: express@4.7.2 node_modules/express
remote: ├── merge-descriptors@0.0.2
remote: ├── utils-merge@1.0.0
remote: ├── cookie@0.1.2
remote: ├── escape-html@1.0.1
remote: ├── cookie-signature@1.0.4
remote: ├── finalhandler@0.1.0
remote: ├── vary@0.1.0
remote: ├── range-parser@1.0.0
remote: ├── fresh@0.2.2
remote: ├── qs@0.6.6
remote: ├── media-typer@0.2.0
remote: ├── parseurl@1.2.0
remote: ├── methods@1.1.0
remote: ├── buffer-crc32@0.2.3
remote: ├── depd@0.4.4
remote: ├── path-to-regexp@0.1.3
remote: ├── debug@1.0.4 (ms@0.6.2)
remote: ├── proxy-addr@1.0.1 (ipaddr.js@0.1.2)
remote: ├── accepts@1.0.7 (negotiator@0.4.7, mime-types@1.0.2)
remote: ├── type-is@1.3.2 (mime-types@1.0.2)
remote: ├── send@0.7.2 (ms@0.6.2, mime@1.2.11, finished@1.2.2)
remote: └── serve-static@1.4.4 (send@0.7.4)
remote:
remote: socket.io@1.0.6 node_modules/socket.io
remote: ├── debug@0.7.4
remote: ├── has-binary-data@0.1.1 (isarray@0.0.1)
remote: ├── socket.io-parser@2.2.0 (isarray@0.0.1, emitter@1.0.1, json3@3.2.6)
remote: ├── socket.io-adapter@0.2.0 (socket.io-parser@2.1.2)
remote: ├── engine.io@1.3.1 (base64id@0.1.0, debug@0.6.0, engine.io-parser@1.0.6
, ws@0.4.31)
remote: └── socket.io-client@1.0.6 (to-array@0.1.3, indexof@0.0.1, component-bin
d@1.0.0, object-component@0.0.3, component-emitter@1.1.2, parseuri@0.0.2, engine
.io-client@1.3.1)
remote: npm info ok
remote: Preparing build for deployment
remote: Deployment id is f00577e5
remote: Activating deployment
remote: Starting NodeJS cartridge
remote: Tue Oct 14 2014 07:31:26 GMT-0400 (EDT): Starting application 'nodejs' .
..
remote: -------------------------
remote: Git Post-Receive Result: success
remote: Activation status: success
remote: Deployment completed with status: success
To ssh://543d0@odejs-nuvent.rhcloud.com/~/git/nodejs.git/

   ac66919..d5382a5  master -> master

在客户端,我已经使用过。

 var socket = io.connect("http://cube-nuventech.rhcloud.com:8000");



**Service Temporarily Unavailable

The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.

Apache/2.2.15 (Red Hat) Server at node-nuven.rhcloud.com Port 80**

但我还是无法访问我的应用程序网址。它告诉我上面的消息。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

在服务器端,尝试将self.io = require('socket.io').listen(self.server);更改为self.io = require('socket.io').listen(8000);