我已经通过Red-hat为OpenJhift Cloud平台部署了以下代码用于NodeJs聊天应用程序,我在控制台(F12)和响应代码中没有收到任何错误,如Ok 200 ..但是应用程序无法正常工作
服务器(您可以在https://github.com/varund29/openshift/blob/master/index.js找到完整的来源)
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server, { origins:'http://nodejs-atnodejs.rhcloud.com:8000' });
app.get('/', function (req, res) {
res.sendfile('index.html');
});
io.on('connection', function (socket) {
socket.on('chatmessage', function (msg) {
console.log('index.js(socket.on)==' + msg);
io.emit('chatmessage', msg);
});
});
server.listen(process.env.OPENSHIFT_NODEJS_PORT, process.env.OPENSHIFT_NODEJS_IP);
客户(您可以在https://github.com/varund29/openshift/blob/master/index.html找到完整的来源)
src="http://nodejs-atnodejs.rhcloud.com:8000/socket.io/socket.io.js
src="http://code.jquery.com/jquery-1.11.1.js"
var socket = io.connect('http://nodejs-atnodejs.rhcloud.com:8000');
$('button').click(function (e) {
console.log('index.html($(button).click)=' + $('#m').val());
socket.emit('chatmessage', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chatmessage', function (msg) {
console.log('index.html(socket.on)==' + msg);
$('#messages').append($('<li>').text(msg));
});
Html正文
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" />
<button>Send</button>
</form>
答案 0 :(得分:3)
当我运行你的代码时,我在OpenShift Online上的日志文件中遇到以下错误:
Option log level is not valid. Please refer to the README.
Option polling duration is not valid. Please refer to the README.
所以我在index.js
文件中注释掉了以下几行:
io.set('log level', 1); // reduce logging
io.set('transports', ['xhr-polling']);
io.set("polling duration", 10);
现在似乎工作正常。你可以在这里测试一下:http://nodejs-cdaley.rhcloud.com/ 您可以在此处查看代码:https://github.com/developercorey/nodejs