WebSockets on OpenShift do not work with remote client

柔情痞子 提交于 2021-01-27 07:40:50

问题


I have an issue that I cannot solve. I implemented a node js WebSockets server on an openshift cartridge using socket.io or WebSockets node js libraries. With any of them the result is the same.

With a node js client running on the same openshift platform everything works ok.

When the client is moved on my local pc the client connects and suddenly disconnects giving a 1011 internal server error.

I tried using other well known clients like the echo service on WebSockets.Org or jsfiddle but the result is the same, connect and suddenly disconnect.

Do not know why. Have someone been able to connect to an openshift WebSockets server from remote? The server is minimal and simple, and locally it works ok.

This is the code I am using as the server:

var ipaddress = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port      = 8000;

var app = require("express");
var server = require("http").Server(app);
var io = require("socket.io")(server);

var handleClient = function (socket) {
    // we've got a client connection
    socket.sendUTF("hello");
    console.log("connect");
};

io.on("connection", handleClient);

server.listen(port, ipaddress);

And this is the socket endpoint: ws://js-camillorh.rhcloud.com:8000

Thank you!

Updated the code to this after comments:

var ipaddress = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var app = require("express");
var server = require("http").Server(app);
var io = require("socket.io")(server);

var handleClient = function (socket) {
    socket.sendUTF("hello");
    console.log("connect");
};

io.on("connection", handleClient);

console.log("listen: "+ipaddress+" "+port);
server.listen(port, ipaddress);

Log file is this:

DEBUG: Sending SIGTERM to child...
DEBUG: Running node-supervisor with
DEBUG:   program 'server.js'
DEBUG:   --watch '/var/lib/openshift/5551a336e0b8cd4ea50000db/app-root/data/.nodewatch'
DEBUG:   --ignore 'undefined'
DEBUG:   --extensions 'node|js|coffee'
DEBUG:   --exec 'node'
DEBUG: Starting child process with 'node server.js'
DEBUG: Watching directory '/var/lib/openshift/5551a336e0b8cd4ea50000db/app-root/data/.nodewatch' for changes.
listen: 127.13.35.129 8080

回答1:


You are binding to the wrong port, you need to bind to 8080 like this:

var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;

Then you will still access your application with this url:

ws://js-camillorh.rhcloud.com:8000

or for secure websockets

wss://js-camillorh.rhcloud.com:8443


来源:https://stackoverflow.com/questions/30627235/websockets-on-openshift-do-not-work-with-remote-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!