与 OpenShift 应用程序的 WebSocket 连接失败 [英] WebSocket connection to OpenShift app failed

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

问题描述

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

如果我在我的应用程序中添加了一个尾巴,我会收到以下消息:调试:这种类型的响应不能有正文.忽略传递给 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(对于 Secured 会话,它将是 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')
}

更多信息此处.

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

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