faye在客户端浏览器上不可用于节点js

时间:2014-05-14 09:22:59

标签: node.js openshift faye bayeux

我创建了一个示例应用程序,它是一个使用nodejs + faye + epxress的聊天服务器,它在我的本地机器上正常运行,我可以向服务器发送消息并将其发布到客户端浏览器。但是当我在Openshift部署我的应用程序时,它无效。我能够发送消息,但我无法发布消息。这是我的代码片段

Server.js

var path = require('path');var http = require('http'),    express = require('express'),    faye = require('faye');

var bodyParser = require('body-parser');
var bayeux = new faye.NodeAdapter({  
    mount:    '/faye',
    timeout:  45
    });
var app = express();
var routes = require('./routes/index');
var server = http.createServer(app);
var port = process.env.OPENSHIFT_NODEJS_PORT || 8123 ;
var ipaddr = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
bayeux.attach(server);// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.post('/message', function(req, res) 
        {   
            try 
            {   
                var publication=bayeux.getClient().publish('/channel', {
                    text: req.body.message
                    }); 
                publication.then(function(){
                    console.log("Message recieved by server");
                    },function(error)
                    {       console.log("eror coming up:"+error);   });
                    console.log("Posting message:"+req.body.message);
                    res.send(200);
            }
            catch(e)
            {
                console.log("error"+e); }
        });
server.listen(port,ipaddr);
console.log("Server up and listening on port "+port);
console.log("Server ip "+ipaddr); 

client1.jade

html
body
    head
        script(src=' https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js')
        style
            #messages { height: 300px; width: 300px; overflow: hidden; border: #ccc solid 1px;}
            section { margin-left: 20px; }
        section
        h1 Chat Client #1
        div#messages
        textarea#chat(rows='2', cols='35')
        button(onclick='something()') Chat
        script(src='http://chat-yummyfoods.rhcloud.com/faye/client.js')
        script.
            var something = function() {

            var $chat = $('#chat');
            console.log('something:'+$chat.val())
            var url = 'http://chat-yummyfoods.rhcloud.com/message';
            var message = {message: 'Client 1: ' + $chat.val()};
            var dataType = 'json';
            $.ajax({
            type: 'POST',
            'url': url,
            'data': message,
            'dataType': dataType
            });
            $chat.val('');
            var client = new Faye.Client('/faye',{
            endpoints:
            {
                websocket:'ws.chat-yummyfoods.rhcloud.com'
            }
            ,timeout: 20
            });
            client.disable('websocket');

            console.log("client:"+client);

            var subscription=client.subscribe('/channel', function(message) {
            console.log("Message:"+message.text);
            $('#messages').append('<p>'+message.text+'</p>');
            });
            subscription.then(function()
            {
                console.log('subscribe is active');
                alert('subscribe is active');
            });
            }

GIT回购网址

https://github.com/pulkitsharva/ChatAppOpenshift

申请网址

http://chat-yummyfoods.rhcloud.com

我无法弄清楚我在这里做错了什么

1 个答案:

答案 0 :(得分:2)

我找到了解决问题的方法,这是我遵循的方法

步骤1)我使用错误的网址连接到faye,从客户端删除端点

var client = new Faye.Client('http://chat-yummyfoods.rhcloud.com:8000/faye',{
                    timeout: 20
                });

步骤2)获取应用的websocket并为faye客户端禁用它

var Socket = new WebSocket('ws://chat-yummyfoods.rhcloud.com:8000' );
                    client.disable(Socket);

就是这样:))