【问题标题】:need idea for project : how to invite people to group chat? (Spring_EJB_JMS_Glassfish)项目需要想法:如何邀请人们进行群聊? (Spring_EJB_JMS_Glassfish)
【发布时间】:2015-10-05 18:13:53
【问题描述】:

我正在使用以下技术在 Java 中做一个服务器-客户端群聊应用程序:JMS、EJB(远程无状态、消息驱动 bean)、Spring(依赖注入)、Hibernate(数据库内容)。我在 mysql 数据库中有一个用户(用户名,密码)表。问题是它需要:登录后用户可以邀请其他用户与他聊天,我不知道该怎么做?因此,如果我邀请某人,我必须在 glassfish 中创建一个新主题,或者无论如何要使用 1 个主题?如何在运行时在 glassfish 中创建主题?有人建议我使用选择器来过滤消息,这样我就不必创建多个主题,可以这样做吗?以及如何在我的应用程序中与其他用户通信以向他们发送邀请?因此,任何人都可以向我提出任何想法、代码、示例......关于如何做到这一点将不胜感激。到目前为止。我已经设法让用户在我在 glassfish Web 控制台中预定义的主题中互相聊天。这是我的客户端代码的一部分。

Context ctx;
            try {
                Properties props = new Properties();

                ctx = new InitialContext(props);
                tcf = (TopicConnectionFactory) ctx.lookup("p2scon");
                tpConnection = tcf.createTopicConnection(); // anomyous access
                tpConnection.setClientID(tfName.getText());

                // false means the TopicSession will not be transacted
                // acknowledgment mode used by the JMS client.
                // An acknowledgment is a notification to the message server that the client has received the messag
                // AUTO_ACKNOWLEDGE means the message is automatically acknowledged after it is received by the client
                tpSession = tpConnection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);

                // look up JMS topic
                topic = (Topic) ctx.lookup("p2sdes");
                tpPublisher = tpSession.createPublisher(topic);

               /* Nondurable subscribers are temporary subscriptions that receive messages only when
                they are actively listening on the topic. Durable subscribers, on the other hand, will
                receive a copy of every message published, even if they are “offline” when the message
                is published.*/
                tpSubscribe = tpSession.createDurableSubscriber(topic, tfName.getText());
                tpSubscribe.setMessageListener(MainFrame1.this);
                tpConnection.start();
                /*t = new Thread(MainFrame1.this);
                t.start();*/
                btnConnect.setVisible(false);
            } catch (NamingException e) {
                e.printStackTrace();
                JOptionPane.showMessageDialog(null, "Can't connect");
            } catch (JMSException e) {
                e.printStackTrace();
                JOptionPane.showMessageDialog(null, "Can't connect");
            }

【问题讨论】:

  • 您知道,尽管 JMS 代表 Java Messaging Service,但它并不是真正用于聊天应用程序的东西?
  • 是的!我知道!但仍然。这是我的导师要求我做的一个项目,所以我无能为力。只需要处理它。

标签: java spring glassfish ejb jms


【解决方案1】:

如果您想在进程和机器之间进行通信,则需要消息服务,例如Apache's ActiveMQ。 要邀请用户加入主题,您需要另一种交流方式。两种模式应运而生:

  • 每个用户都登录到应用程序。用户登录后,另一个用户可以通过发送 P2P 消息邀请他们加入聊天,该消息(如果接受)会导致他们订阅主题。在这种情况下,用户需要先登录才能发送邀请,但用户可以拥有一个状态,例如 XMPP 的“状态”。
  • 用户可以邀请任何人加入某个主题,该主题会发送离线通信(可能是一封电子邮件),请求他们登录并加入。除了邀请当前未登录的用户外,这还允许用户邀请以前未使用该服务的朋友。但是,一旦他们登录,订阅该主题将是一个手动过程。

一般来说,如果您想了解聊天服务的模式,最好查看XMPP

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    相关资源
    最近更新 更多