在春天使用websockets获取此错误,未捕获的类型错误:onrecieve不是函数(..)stomp.js

时间:2016-12-09 15:39:52

标签: javascript spring spring-mvc spring-websocket sockjs

我收到此错误:

  

未捕获类型错误:onrecieve不是函数(..)stomp.js

任何人都可以帮助我,并使用stockjs,stomp和jars为我提供完整的设置吗?

我的档案:home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Reload..</title>




<%--  <script type="text/javascript" src='<c:url  value="/static/scripts/jquery.js"/>'></script>  --%>
<script type="text/javascript" src='<c:url value="/static/scripts/sockjs-  0.3.4.js"/>'></script> 
  <script type="text/javascript" src='<c:url value="/static/scripts/stomp.js"/>'></script>

   <script type="text/javascript">

   var stompClient=null;

  var onDebug = function(m) {
    console.log('DEBUG', m);
  };

  var connect=function(){
  var socket = new SockJS("/testwebsockets/portfolio");
    stompClient = Stomp.over(socket);
   stompClient.connect({}, function(frame) {
    console.log('Connecteds: ' + frame);
 stompClient.debug = onDebug;
stompClient.subscribe("/topic/portfolio",{},function(msgput){
    console.log("msg:"+msgput.body);
  });
},function(error){
  alert(error);
});
};





  var sendMessage=function(){
  stompClient.debug = onDebug;
  stompClient.send("/app/portfolio",   {},JSON.stringify({from:'kk',text:'hello'}));
  };



 </script>
 </head>
<body>
 <div>
            <button id="connect" onclick="connect();">Connect</button>


        <br />
        <div id="conversationDiv">

            <button id="sendMessage" onclick="sendMessage();">Send</button>

        </div>
    </div>


  </body>
  </html>

这是使用Javascript代码连接到websockets的jsp文件。它确实连接但是从控制器接收它时会收到此错误:

  

未捕获类型错误:onrecieve不是函数(..)stomp.js

     

打开Web Socket ...
  stomp.js; jsessionid = 45714C4BB09BD6111D49A41D92FBBAC0:130 Web套接字已打开...
  stomp.js; jsessionid = 45714C4BB09BD6111D49A41D92FBBAC0:130&gt;&gt;&gt; CONNECT
  接受版本:1.1,1.0
  心跳:10000,10000

     

&LT;&LT;&LT;连
  版本:1.1
  心跳:0,0

     

连接到服务器undefined
  (索引):34 Connecteds:CONNECTED
  心脏搏动:0,0
  版本:1.1

     

DEBUG&gt;&gt;&gt;订阅
  ID:分0
  目的地:/ topic / portfolio

     

DEBUG&gt;&gt;&gt;发送
  目的地:/应用/组合
  内容长度:28

     

{“from”:“kk”,“text”:“hello”}

     

DEBUG&lt;&lt;&lt;消息
  目的地:/主题/组合
  内容类型:application / JSON;字符集= UTF-8
  订阅:分0
  消息ID:h8_q6giw-0
  内容长度:48

     

{“from”:“kk”,“text”:“hello”,“timeStamp”:“08:26”}

     

stomp.js; jsessionid = 45714C4BB09BD6111D49A41D92FBBAC0:288 Uncaught TypeError:onreceive不是函数(...)(匿名函数)@ stomp.js; jsessionid = 45714C4BB09BD6111D49A41D92FBBAC0:288REventTarget.dispatchEvent @
  sockjs-0.3.4.js; jsessionid = 45714C4BB09BD6111D49A41D92FBBAC0:84SockJS._dispatchMessage @
   sockjs-0.3.4.js; jsessionid = 45714C4BB09BD6111D49A41D92FBBAC0:1025SockJS._didMessage @
   sockjs-0.3.4.js; jsessionid = 45714C4BB09BD6111D49A41D92FBBAC0:1083that.ws.onmessage @
   sockjs-0.3.4.js; JSESSIONID = 45714C4BB09BD6111D49A41D92FBBAC0:1215

1 个答案:

答案 0 :(得分:0)

您的问题源于您传递给subscribe方法的错误额外参数。

stompClient.subscribe("/topic/portfolio",{},function(msgput){
    console.log("msg:"+msgput.body);
});

应该成为:

stompClient.subscribe("/topic/portfolio",function(msgput){
    console.log("msg:"+msgput.body);
});