Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

javascript - WebSocket connection to OpenShift app failed

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.

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);

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.");
};
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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')
}

More information here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...