2
  • GroupChatにCometdチャットメカニズムを使用したいSpring-MVCアプリケーションに取り組んでいます。例を確認したところ、StockPriceEmitter と Client-Side サーバーの hello は優れていますが、チュートリアルで見つけられない重要な点が 1 つあります。それは、これらの例をどのように組み合わせるかということです。
    • チュートリアル リンク :リンク
    • 例がどのように機能するかを知っている人は、この段落をスキップできます。したがって、基本的にクライアント側サーバーの hello のしくみは、hello メッセージがサービス チャネル '/service/hello' で送信され、サーバー側で出力されます。StockPriceEmitter は逆に機能し、バックエンドでいくつかの値を作成し、特定のチャネルのフロントエンドに送信します。
    • 私の問題は、クライアントから受信したメッセージを、本質的に上記の 2 つの例を組み合わせて、すべてのチャネル サブスクライバーにブロードキャストしたいということです。

アップデート

ほとんどの場合のように返信がなかった後、Java、Javascript を使用して複数のチャネルでメッセージを送受信できるように、コードを作成するソリューションを見つけました。誰かが必要とする場合のコードは次のとおりです。

BayeuxInitializer :

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

index.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