简体   繁体   English

NodeJS,WebSockets(ws)的Openshift

[英]NodeJS, WebSockets(ws) an Openshift

I'm trying to create a simple websocket app with NodeJS, WS and Openshift 我正在尝试使用NodeJS,WS和Openshift创建一个简单的Websocket应用

This is my code: package.json: 这是我的代码:package.json:

{
  "name": "OpenShift-Sample-App",
  "version": "1.0.0",
  "description": "OpenShift Sample Application",
  "keywords": [
    "OpenShift",
    "Node.js",
    "application",
    "openshift"
  ],
  "author": {
    "name": "John Smith",
    "email": "example123@example.com",
    "url": "http://www.google.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": {
    "express": "^4.12.3",
    "socket.io" : "0.9.16",
    "ws" : "0.4.31"
  },

  "devDependencies": {},
  "bundleDependencies": [],

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

app.js: app.js:

#!/bin/env node
//  OpenShift sample Node application
var express = require('express');
var fs = require('fs');
var WebSocketServer = require('ws').Server;
var SampleApp = function () {

    //  Scope.
    var self = this;

    var url = '127.0.0.1:27017/' + process.env.OPENSHIFT_APP_NAME;

    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";
        }
        ;
    };

    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');
    };



    self.cache_get = function (key) {
        return self.zcache[key];
    };


    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()));
    };


    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);
                });
            });
    };


    self.createGetRoutes = function () {
        self.getRoutes = {};

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


    self.initializeServer = function () {
        self.createGetRoutes();

        self.app = express();
        //  Add handlers for the app (from the routes).
        for (var r in self.getRoutes) {
            self.app.get(r, self.getRoutes[r]);
        }

    }

    self.initialize = function () {
        self.setupVariables();
        self.populateCache();
        self.setupTerminationHandlers();

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



    self.start = function () {

        var wss = new WebSocketServer({ server: self.app
        })

        wss.on('connection', function connection(ws) {
            console.log(".....Connected");
            var location = url.parse(ws.upgradeReq.url, true);

            ws.on('message', function incoming(message) {
                console.log('received: %s', message);
            });

            ws.send('something');
        });

        self.app.listen(self.port, self.ipaddress, function () {
            console.log('%s: Node server started on %s:%d ...',
                Date(Date.now()), self.ipaddress, self.port);
        });
    };

};
var zapp = new SampleApp();
zapp.initialize();
zapp.start();

when I run: wscat --connect ws://something.rhcloud.com:8000 当我运行时:wscat --connect ws://something.rhcloud.com:8000

I got: 我有:

connected (press CTRL+C to quit)
disconnected

What is wrong in the source code? 源代码有什么问题?

Thanks 谢谢

Within (pre-docker) OpenShift, your application connects to the system load balancer by listening on process.env.OPENSHIFT_NODEJS_PORT . 在(泊坞窗前)OpenShift中,您的应用程序通过侦听process.env.OPENSHIFT_NODEJS_PORT连接到系统负载平衡器。

OpenShift's load balancer then exposes your application externally on four different ports: 然后,OpenShift的负载平衡器会在四个不同的端口上向外部公开您的应用程序:

  1. 80 - basic http connections available 80提供基本的HTTP连接
  2. 443 - basic https connections available 443提供基本的https连接
  3. 8080 - http and ws connections available 8080可用的HTTP和WS连接
  4. 8443 - https and ws connections available 8443 https和ws连接

Unfortunately, in the pre-docker versions of OpenShift, the ws connection upgrade (from http or https) will only work correctly on ports 8080 and 8443 . 不幸的是,在OpenShift的docker之前版本中,ws连接升级(来自http或https)仅可在端口80808443上正常工作。

You can work around this issue by including a client-side check to test if the server hostname includes the string "rhcloud.com". 您可以通过包括客户端检查以测试服务器主机名是否包含字符串“ rhcloud.com”来解决此问题。 If it does, you'll need to establish your client's socket connection using the alternate port number. 如果是这样,则需要使用备用端口号建立客户端的套接字连接。

Newer, docker-based releases of OpenShift include full websocket support on standard web ports. 较新的基于Docker的OpenShift版本包括对标准Web端口的完整WebSocket支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM