简体   繁体   English

WebSocket与OpenShift应用程序的连接失败

[英]WebSocket connection to OpenShift app failed

I created an app with NodeJS and I'm using ws module. 我用NodeJS创建了一个应用程序,我正在使用ws模块。 If I test the app in localhost it works and there isn't any problem to connect websockets. 如果我在localhost中测试应用程序它是有效的,连接websockets没有任何问题。 Now I've upload the app to Openshift and when I try to access from the client it returns that is not possible to stablish a connection to the websocket. 现在我已将应用程序上传到Openshift,当我尝试从客户端访问时,它返回无法建立与websocket的连接。

If I do a tail in putty to my app I have this message: DEBUG: This type of response MUST NOT have a body. 如果我在我的应用程序中使用putty尾部,我会收到以下消息: DEBUG:这种类型的响应不能有一个正文。 Ignoring data passed to end(). 忽略传递给end()的数据。

The code that I have in the server is: 我在服务器中的代码是:

#!/bin/env node

//Openshift variables
var ipaddress = process.env.OPENSHIFT_NODEJS_IP || "192.168.69.42";
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;


//NodeJS require modules
var Enum = require('enum');
var WebSocketServer = require('ws').Server
    wss = new WebSocketServer({host:ipaddress, port:port});
var fs = require('fs');


wss.on('connection', function(ws) {
    console.log((new Date()) + ' Connection from origin: ' + ws._socket.remoteAddress);
});

console.log((new Date()) + " Server is listening on: " + ipaddress + ':' port);

And in the client: 在客户端:

var ws = new WebSocket("ws://192.168.69.42:8080/");

ws.onopen = function() {
    console.log("Connected.");
    ws.send("This is the client speaking.");
};

For all WebSocket connections on OpenShift you need to use port 8000 (for Secured sessions it would be 8443 ). 对于OpenShift上的所有WebSocket连接,您需要使用端口8000 (对于安全会话,它将是8443 )。 So, your server example works well (I run them after removing the unnecessary line var Enum = require('enum'); , you just need to hardcode the port on client to 8000 : 因此,您的服务器示例运行良好(我在删除不必要的行后运行它们var Enum = require('enum'); ;,您只需将客户端上的端口硬编码为8000

var ws = new WebSocket("ws://YourApp-YourName.rhcloud.com:8000"); 

ws.onopen = function(){
  console.log('opened')
}

More information here . 更多信息在这里

这是github上的一个例子,可以查看: https//github.com/developercorey/openshift-nodejs-http-and-websocket-example

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

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