Non-http TCP connection on Cloudfoundry

后端 未结 2 464
温柔的废话
温柔的废话 2021-01-14 11:27

I\'m a nooby mobile developer trying to take advantage of cloudfoundry\'s service to run my server to handle some chats and character movements. I\'m using Noobhub to achiev

相关标签:
2条回答
  • 2021-01-14 11:44

    Actually, process.env.VCAP_APP_PORT environment variable provides you the port, to which your HTTP traffic is redirected by the Cloud Foundry L7 router (nginx) based on the your apps route (e.g. nodejsapp.vcap.me:80 is redirected to the process.env.VCAP_APP_PORT port on the virtual machine), so you definitely should not use it for the TCP connection. This port should be used to listen HTTP traffic. That is why you example do work locally and do not work on Cloud Foundry.

    The approach that worked for me is to listen to the port provided by CF with an HTTP server and then attach Websocket server (websocket.io in my example below) to it. I've created sample echo server that works both locally and in the CF. The content of my Node.js file named example.js is

    var host = process.env.VCAP_APP_HOST || "localhost";
    var port = process.env.VCAP_APP_PORT || 1245;
    
    var webServerApp = require("http").createServer(webServerHandler);  
    var websocket = require("websocket.io");
    var http = webServerApp.listen(port, host);
    var webSocketServer = websocket.attach(http);
    
    function webServerHandler (req, res) {
        res.writeHead(200);
        res.end("Node.js websockets.");
    }
    console.log("Web server running at " + host + ":" + port);
    
    //Web Socket part
    webSocketServer.on("connection", function (socket) {
        console.log("Connection established."); 
    
        socket.send("Hi from webSocketServer on connect");
    
        socket.on("message", function (message) { 
            console.log("Message to echo: " + message);
            //Echo back
            socket.send(message);
        });
    
        socket.on("error", function(error){
            console.log("Error: " + error);
        });
    
        socket.on("close", function () { console.log("Connection closed."); });
    });
    

    The dependency lib websocket.io could be installed running npm install websocket.io command in the same directory. Also there is a manifest.yml file which describes CF deploy arguments:

    ---
    applications:
    - name: websocket
      command: node example.js
      memory: 128M
      instances: 1
      host: websocket
      domain: vcap.me
      path: .
    

    So, running cf push from this directory deployed app to my local CFv2 instance (set up with the help of cf_nise_installer) To test this echo websocket server, I used simple index.html file, which connects to server and sends messages (everything is logged into the console):

    <!DOCTYPE html>
        <head>
            <script>        
            var socket = null;
            var pingData = 1;
            var prefix = "ws://";
    
            function connect(){
                socket = new WebSocket(prefix + document.getElementById("websocket_url").value);
                socket.onopen = function() { 
                    console.log("Connection established"); 
                };
                socket.onclose = function(event) { 
                    if (event.wasClean) {
                        console.log("Connection closed clean");
                    } else {
                        console.log("Connection aborted (e.g. server process killed)");
                    }
                        console.log("Code: " + event.code + " reason: " + event.reason);
                    };             
                socket.onmessage = function(event) { 
                    console.log("Data received:  " + event.data);
                };
                socket.onerror = function(error) { 
                    console.log("Error: " + error.message);               
                };
            }        
            function ping(){
                if( !socket || (socket.readyState != WebSocket.OPEN)){
                    console.log("Websocket connection not establihed");
                    return;
                }            
                socket.send(pingData++);
            }        
            </script>
        </head>
        <body>
            ws://<input id="websocket_url">
            <button onclick="connect()">connect</button>
            <button onclick="ping()">ping</button>
        </body>
    </html>
    

    Only thing to do left is to enter server address into the textbox of the Index page (websocket.vcap.me in my case), press Connect button and we have working Websocket connection over TCP which could be tested by sending Ping and receiving echo. That worked well in Chrome, however there were some issues with IE 10 and Firefox.

    What about "TRUE" TCP socket, there is no exact info: according to the last paragraph here you cannot use any port except 80 and 443 (HTTP and HTTPS) to communicate with your app from outside of Cloud Foundry, which makes me think TCP socket cannot be implemented. However, according to this answer, you can actually use any other port... It seems that some deep investigation on this question is required...

    0 讨论(0)
  • 2021-01-14 11:50

    "Cloud Foundry uses an L7 router (ngnix) between clients and apps. The router needs to parse HTTP before it can route requests to apps. This approach does not work for non-HTTP protocols like WebSockets. Folks running node.js are going to run into this issue but there are no easy fixes in the current architecture of Cloud Foundry." - http://www.subbu.org/blog/2012/03/my-gripes-with-cloud-foundry

    I decided to go with pubnub for all my messaging needs.

    0 讨论(0)
提交回复
热议问题