faye is not available on client browser for node j

2019-06-08 15:26发布

问题:

I have created a sample application which is a chat server using nodejs + faye + epxress which is running fine on my local machine and I able to send message to server and publish it to client browser. But when I deployed my app on Openshift it is not working. I am able to send message but I am not able to publish message. Here is my code snippet

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 Repo URL

https://github.com/pulkitsharva/ChatAppOpenshift

Application URL

http://chat-yummyfoods.rhcloud.com

I am not able to figure out what wrong I am doing here

回答1:

I found solution to my problem, here is what I followed

Step 1) I was using wrong url to connect to faye, remove the endpoint from client

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

Step 2) Get the websocket of your app and disable it for faye client

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

thats it :)