Cloudfoundry上的非http TCP连接 [英] Non-http TCP connection on Cloudfoundry

查看:199
本文介绍了Cloudfoundry上的非http TCP连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个nooby移动开发者试图利用cloudfoundry的服务来运行我的服务器来处理一些聊天和字符移动。
我使用Noobhub来实现这个(服务器和客户端之间的TCP连接使用Node.js和Corona SDK的TCP连接API)。



尝试Cloudfoundry(Node.js)和我的机器(lua)之间的非http TCP连接。



链接到Noobhub(有一个github repo与服务器和客户端实现。



/ p>

客户

  ... 
socket.connect myappname.cloudfoundry.com,45234)
...

服务器的process.env.VCAP_APP_PORT值我从控制台输出中检索到我运行应用程序后通过vmc logs myappname。)



服务器

  ... 
server.listen(process.env.VCAP_APP_PORT)

当我尝试连接时,它只是超时。



在我的本地计算机上,执行
客户端

  ... 
socket.connect(localhost,8989)

服务器

  ... 
server.listen(8989)

按预期工作。它只是在cloudfoundry,它不工作。



我尝试了一堆其他的方法,如设置客户端的端口连接到80和一堆其他。我看到了一些资源,但没有一个解决它。



PS

p>在把这个链接放在我面前有一个愤怒的脸D: ,这里是一个问题,显示了另一个人张贴的类似问题。



无法连接到CloudFoundry上的TCP服务器(localhost node.js工作正常)



从这里,我可以看到,这个家伙正试图做一个类似的事情,我在做什么。
选择的答案是否意味着我必须使用主机头(即使用http协议)连接?这也意味着cloudfoundry不会支持一个TRUETCP套接字很像heroku或应用程序雾吗?

解决方案

c $ c> process.env.VCAP_APP_PORT 环境变量为您提供端口,您的HTTP流量将由Cloud Foundry L7路由器(nginx)基于您的应用程序路由重定向(例如nodejsapp.vcap .me:80重定向到虚拟机上的 process.env.VCAP_APP_PORT 端口),所以你绝对不应该将其用于TCP连接。此端口应用于侦听HTTP流量。这就是为什么你的例子在本地工作,不工作在Cloud Foundry。



对我有用的方法是使用HTTP服务器监听CF提供的端口然后将Websocket服务器(在我的示例中为websocket.io)附加到它。我创建了样例echo服务器,可以在本地和CF中工作。我的Node.js文件名为 example.js 的内容是

  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服务器运行在+ host +:+ port);

// Web Socket部分
webSocketServer.on(connection,function(socket){
console.log(Connection established。);

socket.send(from webSocketServer on connect);

socket.on(message,function(message){
console.log(Message to echo: + message);
//回显
socket.send(message);
});

socket.on ){
console.log(Error:+ error);
});

socket.on(close,function(){console.log Connection closed。);});
});

依赖项lib websocket.io 在同一目录中安装运行 npm install websocket.io 命令。还有一个描述CF部署参数的 manifest.yml 文件:

  --- 
应用程序:
- name:websocket
命令:node example.js
内存:128M
实例:1
主机:websocket
domain:vcap.me
path:。

因此,从此目录运行 cf push 将应用程式部署到我的本地CFv2执行个体(在 cf_nise_installer 的帮助下设定)
测试此功能echo websocket服务器,我使用简单的 index.html 文件,它连接到服务器并发送消息(一切都登录到控制台):

 <!DOCTYPE html> 
< head>
< script>
var socket = null;
var pingData = 1;
var prefix =ws://;

function connect(){
socket = new WebSocket(prefix + document.getElementById(websocket_url)。
socket.onopen = function(){
console.log(Connection established);
};
socket.onclose = function(event){
if(event.wasClean){
console.log(Connection closed clean);
} else {
console.log(Connection aborted(例如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 establishedli );
return;
}
socket.send(pingData ++);
}
< / script>
< / head>
< body>
ws://< input id =websocket_url>
< button onclick =connect()> connect< / button>
< button onclick =ping()> ping< / button>
< / body>
< / html>

只需要将服务器地址输入到索引页面(websocket.vcap.me在我的情况下),按连接按钮,我们有工作Websocket连接通过TCP,可以通过发送Ping和接收回声测试。这在Chrome中运行良好,但是在IE 10和Firefox有一些问题。



TRUETCP套接字,没有确切的信息:段落此处您不能使用任何端口,除了80和443(HTTP和HTTPS)从Cloud Foundry外部与您的应用程序通信,这让我认为TCP套接字不能实现。但是,根据 this 答案,你可以使用任何其他端口...似乎对这个问题的一些深入调查是必需的...


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 achieve this (TCP connection between server and client using Node.js and Corona SDK's TCP connection API)

So basically I'm trying a non-http TCP connection between Cloudfoundry(Node.js) and my machine(lua).

Link to Noobhub(There is a github repo with server AND client side implementation.

I am doing

Client

...
socket.connect("myappname.cloudfoundry.com", 45234)
...

(45234 is from server's process.env.VCAP_APP_PORT value I retrieved from console output I got through "vmc logs myappname" after running the application.)

Server

...
server.listen(process.env.VCAP_APP_PORT)

When I try to connect, it just times out.

On my local machine, doing Client

...
socket.connect("localhost",8989)

Server

...
server.listen(8989)

works as expected. It's just on cloudfoundry that it doesn't work.

I tried a bunch of other ways of doing this such as setting the client's port connection to 80 and a bunch of others. I saw a few resources but none of them solved it. I usually blow at asking questions so if you need more information, please ask me!

P.S.

Before you throw this link at me with an angry face D:< , here's a question that shows a similar problem that another person posted.

cannot connect to TCP server on CloudFoundry (localhost node.js works fine)

From here, I can see that this guy was trying to do a similar thing I was doing. Does the selected answer mean that I MUST use host header (i.e. use http protocol) to connect? Does that also mean cloudfoundry will not support a "TRUE" TCP socket much like heroku or app fog?

解决方案

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...

这篇关于Cloudfoundry上的非http TCP连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆