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

我使用NodeJS创build了一个应用程序,并使用了ws模块。 如果我在本地主机上testing应用程序,它的工作原理,并没有任何问题来连接websockets。 现在我已经把应用程序上传到了Openshift,当我试图从客户端访问时,它返回的是不可能build立到websocket的连接。

如果我在我的应用程序的腻子尾巴我有这个消息: DEBUG:这种types的回应不得有一个机构。 忽略传递给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); 

而在客户端:

 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

 var ws = new WebSocket("ws://YourApp-YourName.rhcloud.com:8000"); ws.onopen = function(){ console.log('opened') } 

更多信息在这里 。

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