无法使用OpenShift运行socket.io和nodejs [英] Can't get socket.io and nodejs running with OpenShift

查看:86
本文介绍了无法使用OpenShift运行socket.io和nodejs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在OpenShift上运行socket.io。我用谷歌搜索了几个小时,但没有什么真正帮助我。在本地它工作正常(当然有不同的端口和localhost作为主机)。

I can't get socket.io running on OpenShift. I googled for some hours now but nothing really helped me. Locally it works fine (with different ports and localhost as host of course).

这是我的server.js文件:

This is my server.js file:

var port      = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var ipadr = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var http = require('http').createServer(function(request, response) {   
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.end();
}).listen(port,ipadr);
console.log(port+":"+ipadr);

var io = require('socket.io').listen(http),
fs = require('fs'),
request = require('request'),
mysql = require('mysql'),
moment = require('moment'),
connectionsArray = [],
connection = mysql.createConnection({
host: 'xxx',
user: 'xxx',
password: 'xxx',
database: 'xxx',
port: 3306
}),
POLLING_INTERVAL = 1000,
pollingTimer;

io.on('connection', function(socket){
  console.log('a user connected');
});

var updateSockets = function(data) {
    // adding the time of the last update
    t = new Date();
    t = moment().format('H:mm:ss');
    console.log('(%s) Connections: %s', t, connectionsArray.length);
    // sending new data to all the sockets connected
    connectionsArray.forEach(function(tmpSocket) {
    tmpSocket.volatile.emit('notification', data);
    });
};
console.log('server.js executed\n');

当我在SSH OpenShift上运行它时,它只显示第一个带有我的端口和ipadress的console.log OpenShift和我的server.js代码的最后一行:

When I run this on SSH OpenShift it just shows the first console.log with my port and ipadress of OpenShift and the last line of my server.js code:


server.js已执行

server.js executed

就是这样。因此,当我在本地测试时,我没有收到用户连接消息。

and that's it. So I don't get the "a user connected" message like when I test it locally.

这就是我想要连接的方式我的客户端.js文件:

This is how I want to connect in my client-side .js-file:

var socket = io.connect('http://njs-uniqo.rhcloud.com:8080/');  

浏览器(Chrome)控制台输出:

Browser (Chrome) Console outputs:

> ?s=product&id=10:1 XMLHttpRequest cannot load http://xxx.rhcloud.com:8000/socket.io/?EIO=3&transport=polling&t=1430395459829-1.
> No 'Access-Control-Allow-Origin' header is present on the requested
> resource. Origin 'http://localhost' is therefore not allowed access.
> The response had HTTP status code 503.

如果我在客户端将端口更改为8080。 js-file我得到 ERR_CONNECTION_TIMED_OUT

If I change the port to 8080 on the client-side .js-file I get ERR_CONNECTION_TIMED_OUT:

GET http://xxx.rhcloud.com:8080/socket.io/?EIO=3&transport=polling&t=1430395346761-6 net::ERR_CONNECTION_TIMED_OUT


推荐答案

你需要使用端口8000.这是websockets的openshift力量。

You need to use port 8000. It's what openshift forces for websockets.

 var socket = io.connect('http://yourapp.rhcloud.com:8000/',{'forceNew':true });

端口8000用于http,8443用于https

Port 8000 is for http and 8443 is for https

来源

这篇关于无法使用OpenShift运行socket.io和nodejs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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