Socket.io和OpenShift上的节点 [英] Socket.io and node on OpenShift

查看:90
本文介绍了Socket.io和OpenShift上的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读过OpenShift在8080服务器端监听.我还读到它们迫使Web套接字连接到端口8000客户端.

我从哪里都看不到需要提及端口8000的服务器端,因为显然OpenShifts apache reverseProxy应该将8000路由到8080(并且由应用程序决定它是什么类型的连接?)

我已经阅读了很多东西,尝试了所有内容,但我实在无法理解这一点.

在8080上侦听并与var socket = io();客户端连接时,站点在本地主机中运行良好.

关于OpenShift,请参阅以下内容:

客户端:8000,服务器:8080 运行并建立WS连接,但服务器未响应.没有.on('connection', ... ).很有道理,而不是听8000.

客户端:8000,服务器:8080,服务器2:8000 尝试拥有2个单独的服务器并且站点无法构建(等待端口8080可用时超时)

客户端:8000,服务器:8000 网站无法建立(与上面相同)

客户端:只需使用io()服务器进行连接:8080 该站点按预期运行,但回退到xhr长时间轮询

有人知道直接使用Socket.io,OpenShift和Express 4的简单教程或示例吗?最糟糕的部分是,您看到的每个示例中的人们都以不同的方式(不建议使用,比我所知道的要新?)来启动服务器.对于看起来如此简单(甚至是广告宣传)的东西感到疯狂.

仅供参考:最后一个端口组合,只需连接io();并在服务器端侦听8080端口,就可以尽早地正常工作. websockets在OpenShift上有气质吗?

解决方案

与之抗争之后,我以为我会使用openshift 3,nodejs 6 +,express 4和socket.io 1共享我的设置.

完整信息在下面,但只是总结了一些主要观点,这些观点与我从其他来源看到的建议有所不同:

  • 服务器使用端口8080和ip 0.0.0.0
  • 不必要将客户端或服务器上的传输选项设置为"websocket"
  • 无需在客户端上指定端口
  • 对于远程客户端连接,请确保不仅为套接字URL调整ws/wss,而且还要为您的nodejsapp创建安全路由时调整openshift上不同的路径

对于服务器代码,我正在使用以下代码.请注意,一些较老的文章似乎将127.0.0.1指示为端口,但实际上在openshift 3上似乎为0.0.0.0

var express = require('express');
var app = express();

var http = require('http').Server(app);
var io = require('socket.io').listen(http);

var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080;
var ip_address = process.env.IP   || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0';

http.listen(port, ip_address, function(){
  console.log( "Listening on " + ip_address + ", port " + port );
});

对于客户端代码,如果您尝试从其连接的页面是通过openshift节点应用程序提供的,则连接非常简单.

// works for http or https
var socket = io.connect();

如果您尝试从远程客户端进行连接,则只需要提供通往应用程序的安全或不安全路由的路径,并使用ws/wss.无需指定端口8000或8443!

// http
var socket = io.connect('ws://ROUTENAME-APPNAME.regionandstuff.openshiftapps.com');

// https
var socket = io.connect('wss://SECUREROUTENAME-APPNAME.regionandstuff.openshiftapps.com');

I have read that OpenShift listening on port 8080 server side. I have also read that they force web sockets to connect to port 8000 client side.

Nowhere have I read that I need to mention port 8000 server side, since apparently OpenShifts apache reverseProxy is supposed to route 8000 to 8080 (and it is up to the application to decide what type of connection it is?)

I have read a whole lot, tried everything, and I really just can't figure this shite out.

Site runs fine in local host when listening on 8080 and connecting with var socket = io(); client side.

On OpenShift, refer to the following:

Client: 8000, Server: 8080 Runs and establishes WS connection, but no response from server. No .on('connection', ... ) to be had. Makes sense, not listening for 8000.

Client: 8000, Server: 8080, Server2: 8000 Attempting to have 2 separate servers and the site won't build (timeout when waiting for port 8080 available)

Client: 8000, Server: 8000 Site won't build (same as above)

Client: just connect using io() server: 8080 site runs as expected but falls back to xhr long polling

Does anyone know of a straightforward tutorial or example of someone using up to date Socket.io, OpenShift, and Express 4? The worst part is that every example you see people are starting their servers in different (deprecated, newer than I know about?) ways. its madness for something that seems so simple (and even advertised).

FYI: The last port combo, connecting with simply io(); and listening on port 8080 server-side was working flawlessly early. Are websockets temperamental on OpenShift?

解决方案

after battling with this for a bit thought i would share my setup using openshift 3, nodejs 6+, express 4 and socket.io 1.

full info is below but just to summarize some of the main points that differ from what i saw suggested by other sources:

  • server use port 8080 and ip of 0.0.0.0
  • its not necessary to set transports option to "websocket" on client or server
  • you do not need to specify a port number on client
  • for remote client connections make sure to adjust not just ws/wss for socket url but also the path which is different on openshift when you create a secure route for your nodejsapp

for server code i am using the following. note some older articles floating around seem to indicate 127.0.0.1 as the port, but it appears to in fact be 0.0.0.0 on openshift 3

var express = require('express');
var app = express();

var http = require('http').Server(app);
var io = require('socket.io').listen(http);

var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080;
var ip_address = process.env.IP   || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0';

http.listen(port, ip_address, function(){
  console.log( "Listening on " + ip_address + ", port " + port );
});

for client code, if the page you are trying to connect from is served off the openshift node app than the connection is quite easy..

// works for http or https
var socket = io.connect();

if you are trying to connect from a remote client then you just need to provide path to either secure or unsecure route to your app and use ws/wss. there is no need to specify a port of 8000 or 8443!

// http
var socket = io.connect('ws://ROUTENAME-APPNAME.regionandstuff.openshiftapps.com');

// https
var socket = io.connect('wss://SECUREROUTENAME-APPNAME.regionandstuff.openshiftapps.com');

这篇关于Socket.io和OpenShift上的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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