Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Comet发布输入数据映射,而不是等待服务器的输出_Java_Javascript_Spring_Cometd_Reverse Ajax - Fatal编程技术网

Java Comet发布输入数据映射,而不是等待服务器的输出

Java Comet发布输入数据映射,而不是等待服务器的输出,java,javascript,spring,cometd,reverse-ajax,Java,Javascript,Spring,Cometd,Reverse Ajax,我正在开发一个SpringMVC应用程序,它使用Comet进行聊天。我面临的唯一问题是,当我从客户端发送消息时,客户端直接将该消息附加到服务器,而不是等待我在后端调用发布机制,然后再附加这些内容。我在连接的两台不同的计算机上试过,结果是一样的。因此,我无法访问在后端设置的变量 请让我知道我能做什么。非常感谢 调试日志: DEBUG: org.cometd.server.BayeuxServerImpl.31913ea0 - < {data={name=check123}, channel

我正在开发一个SpringMVC应用程序,它使用Comet进行聊天。我面临的唯一问题是,当我从客户端发送消息时,客户端直接将该消息附加到服务器,而不是等待我在后端调用发布机制,然后再附加这些内容。我在连接的两台不同的计算机上试过,结果是一样的。因此,我无法访问在后端设置的变量

请让我知道我能做什么。非常感谢

调试日志:

DEBUG: org.cometd.server.BayeuxServerImpl.31913ea0 - <  {data={name=check123}, channel=/chat/1360}
wassup
DEBUG: org.cometd.server.BayeuxServerImpl.411c4c13 - Added channel /chat/1360
DEBUG: org.cometd.server.BayeuxServerImpl.411c4c13 - <  {data={accountid=1360, firstname=AKS, name=check123, channelname=/chat/1360, timestamp=2015-04-27 10:58:08.539}, channel=/chat/1360}
DEBUG: org.cometd.server.BayeuxServerImpl.31913ea0 - << {channel=/chat/1360, id=10, successful=true}
DEBUG: org.cometd.server.BayeuxServerImpl.31913ea0 - <  {channel=/chat/1360, id=10, successful=true}
DEBUG:org.cometd.server.BayeuxServerImpl.31913ea0-<{data={name=check123},channel=/chat/1360}
wassup
调试:org.cometd.server.BayeuxServerImpl.411c4c13-添加了channel/chat/1360
调试:org.cometd.server.BayeuxServerImpl.411c4c13-<{data={accountid=1360,firstname=AKS,name=check123,channelname=/chat/1360,timestamp=2015-04-27 10:58:08.539},channel=/chat/1360}
调试:org.cometd.server.BayeuxServerImpl.31913ea0-channel.setPersistent(true));
ServerChannel ServerChannel=bayeux.getChannel(message.getChannel());
serverChannel.publish(远程,输出);
线程=新线程(()->{
回复回复=新回复();
replies.setReplyingPersonName(senderName);
reples.setReplyText(数据);
this.replessService.addReply(回复、会话ID、发件人);
});
thread.start();
}
@侦听器(“/chat/*”)
public void processChat(服务器会话远程、服务器消息、可变消息){
System.out.println(“wassup”);
String firstName=this.personService.returnLoggedInUsersName();
Timestamp Timestamp=新的时间戳(System.currentTimeMillis());
映射输入=message.getDataAsMap();
字符串数据=(字符串)input.get(“name”);
String temp=message.getChannel();
字符串temp1=temp;
temp=临时替换(“/chat/”,“”);
最终Long groupAccountIdentifier=Long.valueOf(temp);
映射输出=新的HashMap();
输出。输入(“名称”,数据);
put(“channelname”,message.getChannel());
output.put(“firstname”,firstname);
put(“timestamp”,timestamp);
output.put(“accountid”,groupAccountIdentifier);
createChannelIfAbsent(message.getChannel(),channel->channel.setPersistent(true));
ServerChannel ServerChannel=bayeux.getChannel(message.getChannel());
发布(serverSession,输出);
线程=新线程(()->{
ChatMessages ChatMessages=新建ChatMessages();
chatMessages.setFirstName(firstName);
setChatText(数据);
setChannelName(message.getChannel());
setTimeStamp(新的时间戳((System.currentTimeMillis()));
this.chatService.addChatMessage(chatMessages,groupAccountIdentifier);
});
thread.start();
}
}
Javascript代码:

(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/1360', function(message)
                    {
                        $('#hello1').append('<div>Server Says: ' + message.data.name + ' ' + ' ' +message.data.firstname+'</div>');
                    });

                });
                cometd.publish('/chat/1360');
            }
        }

        // 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);
(函数($)
{
var cometd=$.cometd;
$(文档).ready(函数()
{
函数_连接已建立()
{
$('#body').append('已建立Comed连接');
}
函数_connectionbreaked()
{
$('#body').append('CometD Connection breaked');
}
函数_connectionClosed()
{
$('#body').append('CometD Connection Closed');
}
//管理与Bayeux服务器的连接状态的函数
var _connected=false;
功能_元连接(消息)
{
if(cometd.isDisconnected())
{
_连接=错误;
_连接关闭();
返回;
}
var wasconned=_connected;
_已连接=消息。成功===真;
如果(!wasconned&&u connected)
{
_已建立连接();
}
否则如果(已连接&&!\u已连接)
{
_连接断开();
}
}
//第一次联系服务器时调用的函数
//当服务器失去此客户端的状态时
功能_元握手(握手)
{
if(handshake.successful==true)
{
cometd.batch(函数()
{
cometd.subscribe('/chat/1360',函数(消息)
{
$('#hello1').append('服务器说:'+message.data.name+'''+''+message.data.firstname+'');
});
});
cometd.publish('/chat/1360');
}
}
//卸载页面时断开连接
$(窗口)。卸载(函数()
{
cometd.disconnect(真);
});
var cometURL=location.protocol+“/”+location.host+config.contextPath+“/cometd”;
cometd.configure({
url:cometURL,
日志级别:“调试”
});
cometd.addListener('/meta/handshake',_metaHandshake);
addListener('/meta/connect',_metaConnect);
cometd.握手();
});
})(jQuery);
如果有人有任何想法,请告诉我我能做什么。如果需要更多信息,请随时询问。非常感谢。:-)

更新

根据Sborder第二次的建议,我做了一些改变,但我取得了部分成功。另外,我忘了添加index.jsp,它正在发送实际的文本消息

首先,ChatServiceImpl:

     @Session
        private ServerSession serverSession;

  @Listener("/service/chat/{accountid}")
    public void processChat(ServerSession remote, ServerMessage.Mutable message,@Param("accountid")String accountid) {
        System.out.println("wassup and account id is "+accountid);
        String firstName = this.personService.returnLoggedInUsersName();
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());

        Map<String, Object> input = message.getDataAsMap();
        String text = (String) input.get("name");
        String temp = message.getChannel();
 Map<String, Object> data = new HashMap<String,Object>();
        data.put("name", text);
        data.put("channelname", message.getChannel());
        data.put("firstname", firstName);
        data.put("timestamp", timestamp);
     ServerChannel serverChannel = bayeux.createChannelIfAbsent("/chat/1306").getReference();
        serverChannel.setPersistent(true);
        System.out.println("Channel name is "+serverChannel.getChannelId());

       serverChannel.publish(remote, data);
    }
会话 专用服务器会话; @侦听器(“/service/chat/{accountid}”) public void processChat(ServerSession remote,ServerMessage.Mutable message,@Param(“accountid”)字符串accountid){ System.out.println(“wassup和帐户id为”+帐户id); 字符串名
     @Session
        private ServerSession serverSession;

  @Listener("/service/chat/{accountid}")
    public void processChat(ServerSession remote, ServerMessage.Mutable message,@Param("accountid")String accountid) {
        System.out.println("wassup and account id is "+accountid);
        String firstName = this.personService.returnLoggedInUsersName();
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());

        Map<String, Object> input = message.getDataAsMap();
        String text = (String) input.get("name");
        String temp = message.getChannel();
 Map<String, Object> data = new HashMap<String,Object>();
        data.put("name", text);
        data.put("channelname", message.getChannel());
        data.put("firstname", firstName);
        data.put("timestamp", timestamp);
     ServerChannel serverChannel = bayeux.createChannelIfAbsent("/chat/1306").getReference();
        serverChannel.setPersistent(true);
        System.out.println("Channel name is "+serverChannel.getChannelId());

       serverChannel.publish(remote, data);
    }
   function _metaHandshake(handshake)
    {
        if (handshake.successful === true)
        {
            cometd.batch(function()
            {
                cometd.subscribe('/chat/1306', function(message){
                    $('.hello1').append('<div>Server Says: ' +message.data.name + ' ' + ' ' +message.data.firstname+'</div>');

                });

            });

        }
    }
 <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;
    $(document).on("click", "#sendMessage", function(){
        var text = $("#enterText").val();
        cometd.publish('/service/chat/1306', { name: text});
    });