我已經在OpenShift Cloud平臺中通過Red-hat爲NodeJs聊天應用程序部署了以下代碼,在控制檯(F12)和響應代碼中沒有出現任何錯誤,如OK 200。 。但應用程序無法正常Openshift Nodejs Socket.io問題,但是200 Ok響應
服務器(你可以找到在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>
我參考了對於上述從http://socket.io/get-started/chat/ –