[英]Can't get socket.io and nodejs running with OpenShift
I can't get socket.io running on OpenShift. 我无法在OpenShift上运行socket.io。 I googled for some hours now but nothing really helped me.
我现在用Google搜寻了几个小时,但没有任何帮助。 Locally it works fine (with different ports and localhost as host of course).
在本地它可以正常工作(当然,可以使用不同的端口和本地主机作为主机)。
This is my server.js file: 这是我的server.js文件:
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');
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: 当我在SSH OpenShift上运行它时,它仅显示第一个console.log以及我的端口和OpenShift的ipadress以及我的server.js代码的最后一行:
server.js executed
server.js执行
and that's it. 就是这样。 So I don't get the " a user connected " message like when I test it locally.
因此,我不会像在本地测试时那样收到“ 用户已连接 ”消息。
This is how I want to connect in my client-side .js-file: 这就是我要在客户端.js文件中进行连接的方式:
var socket = io.connect('http://njs-uniqo.rhcloud.com:8080/');
Browser (Chrome) Console outputs: 浏览器(Chrome)控制台输出:
> ?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.
If I change the port to 8080 on the client-side .js-file I get ERR_CONNECTION_TIMED_OUT : 如果将客户端.js文件上的端口更改为8080, 则会得到ERR_CONNECTION_TIMED_OUT :
GET http://xxx.rhcloud.com:8080/socket.io/?EIO=3&transport=polling&t=1430395346761-6 net::ERR_CONNECTION_TIMED_OUT
You must edit package.json
. 您必须编辑
package.json
。 Add... 加...
"socket.io": "~0.9.16"
...under dependencies. ...依存关系。 The server then automatically downloads socket.io which is not present by default.
然后,服务器会自动下载默认情况下不存在的socket.io。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.