【发布时间】:2016-03-03 02:45:55
【问题描述】:
我已通过 Red-hat 在 OpenShift 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>
【问题讨论】:
-
我从socket.io/get-started/chat参考了上述内容
标签: node.js socket.io openshift redhat