2
  • 我正在开发一个 Spring-MVC 应用程序,我想在其中为 GroupChat 使用 Cometd 聊天机制。我查看了这些示例,StockPriceEmitter 和 Client-Side server hello 很好,但是我在教程中找不到一件基本的事情是如何组合这些示例。
    • 教程链接:链接
    • 如果有人知道这些示例是如何工作的,他们可以跳过这一段。因此,基本上客户端服务器 hello 的工作原理是在服务通道“/service/hello”上发送 hello 消息,并在服务器端打印它。StockPriceEmitter 则相反,它在后端创建一些值并将它们发送到特定通道上的前端。
    • 我的问题是我想将从客户端收到的消息广播给所有频道订阅者,本质上是结合上述两个示例。

更新

在大多数时候都没有回复之后,我想出了创建代码的解决方案,这样我就可以使用 Java、Javascript 通过多个渠道发送和接收消息。如果有人需要,这是代码:

贝叶初始化器:

@Component
public class BayeuxInitializer implements DestructionAwareBeanPostProcessor, ServletContextAware
{
    private BayeuxServer bayeuxServer;
    private ServerAnnotationProcessor processor;

    @Inject
    private void setBayeuxServer(BayeuxServer bayeuxServer)
    {
        this.bayeuxServer = bayeuxServer;
    }

    @PostConstruct
    private void init()
    {
        this.processor = new ServerAnnotationProcessor(bayeuxServer);
    }

    @PreDestroy
    private void destroy()
    {
    }

    public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException
    {
        processor.processDependencies(bean);
        processor.processConfigurations(bean);
        processor.processCallbacks(bean);
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String name) throws BeansException
    {
        return bean;
    }

    public void postProcessBeforeDestruction(Object bean, String name) throws BeansException
    {
        processor.deprocessCallbacks(bean);
    }

    @Bean(initMethod = "start", destroyMethod = "stop")
    public BayeuxServer bayeuxServer()
    {
        BayeuxServerImpl bean = new BayeuxServerImpl();
    //    bean.setTransports(new WebSocketTransport(bean), new JSONTransport(bean), new JSONPTransport(bean));
        return bean;
    }

    public void setServletContext(ServletContext servletContext)
    {
        servletContext.setAttribute(BayeuxServer.ATTRIBUTE, bayeuxServer);
    }
}

你好服务:

@Named
@Service("chat")
public class HelloService
{
    @Inject
    private BayeuxServer bayeux;
    @Session
    private ServerSession serverSession;



  @PostConstruct
    public void init()
    {
    }

 // The below is dynamic channel, it will listen to anything like /chat/hello  or /chat/weareborg
    @Listener("/chat/*")
    public void processCheck(ServerSession remote, ServerMessage.Mutable message){
        System.out.println("We reached in process check"+message.getData().toString());
        Map<String, Object> input = message.getDataAsMap();
        String name = (String)input.get("name");
    Map<String, Object> output = new HashMap<>();
    output.put("greeting", name);
    output.put("channelname",message.getChannel());
    //   remote.deliver(serverSession, "/hello", output);
    ServerChannel serverChannel = bayeux.getChannel(message.getChannel());
    serverChannel.setPersistent(true);
    serverChannel.publish(serverSession, output);

}
}

应用程序.js:

(function($)
{
    var cometd = $.cometd;

    $(document).ready(function()
    {
        function _connectionEstablished()
        {
           // $('#body').append('<div>CometD Connection Established</div>');
        }

        function _connectionBroken()
        {
           // $('#body').append('<div>CometD Connection Broken</div>');
        }

        function _connectionClosed()
        {
         //   $('#body').append('<div>CometD Connection Closed</div>');
        }

        // Function that manages the connection status with the Bayeux server
        var _connected = false;
        function _metaConnect(message)
        {
            if (cometd.isDisconnected())
            {
                _connected = false;
                _connectionClosed();
                return;
            }

            var wasConnected = _connected;
            _connected = message.successful === true;
            if (!wasConnected && _connected)
            {
                _connectionEstablished();
            }
            else if (wasConnected && !_connected)
            {
                _connectionBroken();
            }
        }

        // Function invoked when first contacting the server and
        // when the server has lost the state of this client
        function _metaHandshake(handshake)
        {
            if (handshake.successful === true)
            {
                cometd.batch(function()
                {
                    cometd.subscribe('/chat/hello', function(message)
                    {
                        if(!(message.data.greeting==undefined)) {
                         /*   $.ajax({
                                url: "/getfirstname",
                                dataType: "html",
                                cache: false,
                                success: function(response ){*/
                                    $('.hello1').append('<div>Server Says: ' + message.data.greeting + ' ' + message.data.channelname + '</div>');


//                                }
//                            })

                        }
                    });

                    cometd.publish('/check/hello');

                });
            }
        }

        // Disconnect when the page unloads
        $(window).unload(function()
        {
            cometd.disconnect(true);
        });

        var cometURL = location.protocol + "//" + location.host + config.contextPath + "/cometd";
        cometd.configure({
            url: cometURL,
            logLevel: 'debug'
        });

        cometd.addListener('/meta/handshake', _metaHandshake);
        cometd.addListener('/meta/connect', _metaConnect);

        cometd.handshake();
    });
})(jQuery);

索引.jsp:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <script type="text/javascript" src="${pageContext.request.contextPath}/jquery/jquery-2.1.3.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/org/cometd.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/jquery/jquery.cometd.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/resources/application.js"></script>
</head>
<body>

<div id="body">
    <input id="enterText" type="text" />Enter text
    <input id="sendMessage" type="button"/>
</div>

<div class="hello1">

</div>
<div class="hello123">

</div>

<script type="text/javascript">
    var config = {
        contextPath: '${pageContext.request.contextPath}'
    };
    var cometd = $.cometd;



   /* setInterval(function(){
        cometd.batch(function()
        {
            cometd.subscribe('/hello', function(message) {
                $('#body').append('<div>Server Says: ' + message.data.greeting + '</div>');
            });
            cometd.publish('/service/hello', { name: 'Akshay' });
        });
    }, 10000);*/

    $(document).on("click", "#sendMessage", function(){
        var text = $("#enterText").val();
        cometd.publish('/chat/hello', { name: text });
      //  $('#body').append('<div>Server Says: ' + message.data.greeting + '</div>');
    });

</script>
</body>
</html>

请使用这些 cometd 依赖项,我注意到其他版本的依赖项冲突:

  <dependency>
            <groupId>org.cometd.java</groupId>
            <artifactId>bayeux-api</artifactId>
            <version>3.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.cometd.javascript</groupId>
            <artifactId>cometd-javascript-jquery</artifactId>
            <version>3.0.4</version>
            <type>war</type>
        </dependency>
        <dependency>
            <groupId>org.cometd.java</groupId>
            <artifactId>cometd-java-server</artifactId>
            <version>3.0.4</version>
        </dependency>

  <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-client</artifactId>
            <version>9.3.0.M2</version>
        </dependency>

        <dependency>
            <groupId>org.cometd.java</groupId>
            <artifactId>cometd-java-client</artifactId>
            <version>2.2.0</version>
        </dependency>


        <dependency>
            <groupId>org.cometd.java</groupId>
            <artifactId>cometd-java-annotations</artifactId>
            <version>3.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-servlets</artifactId>
            <version>9.2.9.v20150224</version>
        </dependency>

以上就是解决方案,如果有任何疑问,请向我提问。

4

0 回答 0