faye在节点js的客户端浏览器上不可用 [英] faye is not available on client browser for node js

查看:124
本文介绍了faye在节点js的客户端浏览器上不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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存储库URL

GIT Repo URL

https://github.com/pulkitsharva/ChatAppOpenshift

应用程序网址

http://chat-yummyfoods.rhcloud.com

我无法弄清楚我在做什么错

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

推荐答案

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

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

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

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
                });

第2步)获取您的应用的网络套接字,并为faye客户端禁用它

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);

就这样:)

这篇关于faye在节点js的客户端浏览器上不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆