Spring-MVC,Cometd:检查谁在 Comet 聊天输入

Spring-MVC, Cometd : Check who is typing in chat in Comet

我正在开发 Spring-MVC 应用程序,我在其中使用 Cometd 实现了聊天功能。作为一项功能,我想知道 Cometd 是否有任何支持方式,或者我可以通过某种方式显示哪个用户正在输入。当然我可以检索用户信息。这是我的聊天代码。谢谢

聊天服务实现:

@Named
@Singleton
@Service
public class ChatServiceImpl {
    @Inject
    private BayeuxServer bayeux;

    @Session
    private ServerSession serverSession;

    @Listener(value = "/service/person/{id}")
    public void privateChat(ServerSession remote, ServerMessage.Mutable message,@Param("id")String id) {
        System.out.println("wassup");
        Person sender = this.personService.getCurrentlyAuthenticatedUser();
        String senderName = sender.getFirstName();

        Map<String, Object> input = message.getDataAsMap();
        String data = (String) input.get("name");
        String timestamp = (String) input.get("timestamp");
        String temp = message.getChannel();
        String temp1 = temp;
        temp = temp.replace("/service/person/", "");
        String channelName = temp1.replace("/service","");
        final int conversationId = Integer.valueOf(temp);

        Replies replies = new Replies();
        replies.setReplyingPersonName(senderName);
        replies.setReplyText(data);
        replies.setReplyTimeStamp(timestamp);
        replies.setReplyingPersonId(sender.getId());
        replies.setRead(false);
        Long replyId = this.repliesService.addReply(replies, conversationId, sender);

        Map<String, Object> output = new HashMap<String, Object>();
        output.put("text", data);
        output.put("firstname", senderName);
        output.put("channelname", channelName);
        output.put("timestamp", timestamp);
        output.put("id",sender.getId());
        output.put("read","true");
        output.put("replyid",replyId);

        ServerChannel serverChannel = bayeux.createChannelIfAbsent("/person/" + id).getReference();
        serverChannel.setPersistent(true);
        serverChannel.publish(serverSession, output);

    }

Application.js : 请注意,我在其他 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>');
        }

        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/1306', function(message)
                    {
                        var data = message.data;
                        $('#body').append('<div>Server Says: ' + data.firstname + '/' + data.accountid + data.time1+'</div>');
                    });
                });
            }
        }

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

        $(document).on('click', '#sender', function()
        {
            cometd.publish('/service/chat/1306', { name: 'hello_' + Date.now() });
        });

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

        cometd.websocketEnabled = false;

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

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

请告诉我如何实现这一点,因为我找不到很多参考资料。非常感谢。 :-)

这很容易实现,方法是在客户端检测输入 start/stop(以巧妙的方式避免向服务器发送过多消息),然后向服务器发送 CometD 服务消息。

然后服务器可以使用正在输入的用户的昵称将消息广播到一个特殊频道(比如 /chat/typing)。

客户端应用程序将订阅 /chat/typing 并接收这些消息,然后显示在正在打字的 UI 中,可能会将多个用户合并到一个 UI 通知中。

CometD 部分很简单,以智能方式检测 start/stop 的输入可能是大部分工作。