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

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

本文介绍了WebSocket与OpenShift应用程序的连接失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created an app with NodeJS and I'm using ws module. If I test the app in localhost it works and there isn't any problem to connect 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.

如果我这样做我的应用程序中的putty尾部我有这样的消息: DEBUG:这种类型的响应不能有一个正文。忽略传递给end()的数据。

If I do a tail in putty to my app I have this message: DEBUG: This type of response MUST NOT have a body. Ignoring data passed to 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);

在客户端:

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

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


推荐答案

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

For all WebSocket connections on OpenShift you need to use port 8000 (for Secured sessions it would be 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 ws = new WebSocket("ws://YourApp-YourName.rhcloud.com:8000");

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

更多信息。

这篇关于WebSocket与OpenShift应用程序的连接失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 20:05