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

aba*_*edo 10 javascript websocket node.js openshift

我用NodeJS创建了一个应用程序,我正在使用ws模块.如果我在localhost中测试应用程序它是有效的,连接websockets没有任何问题.现在我已将应用程序上传到Openshift,当我尝试从客户端访问时,它返回无法建立与websocket的连接.

如果我在我的应用程序中使用putty尾部,我会收到以下消息:DEBUG:这种类型的响应不能有一个正文.忽略传递给end()的数据.

我在服务器中的代码是:

#!/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);
Run Code Online (Sandbox Code Playgroud)

在客户端:

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

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

and*_*bas 18

对于OpenShift上的所有WebSocket连接,您需要使用端口8000 (对于安全会话,它将是8443).因此,您的服务器示例运行良好(我删除不必要的行后运行它们var Enum = require('enum');,您只需要将客户端上的端口硬编码为8000:

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

ws.onopen = function(){
  console.log('opened')
}
Run Code Online (Sandbox Code Playgroud)

更多信息在这里.