STOMPJS over和subscribe方法不能与Spring,SockJs,npm,webpack一起使用

时间:2017-01-11 11:39:37

标签: websocket npm webpack sockjs stompjs

我想在Spring-boot Web应用程序中使用带有SockJs和Stomp客户端的websocket以及Java服务器端的websocket。我已经安装了npm sockjs-client和stompjs。我的客户端代码是:

    var SockJS = require('sockjs-client');
    var Stomp = require("stompjs");        
    var socket = new SockJS('/webSocketEndPoint');
          stompClient = Stomp.over(socket);
          stompClient.connect({}, function (frame) {
          stompClient.subscribe('/topic/notification', function (notificationBody) {
             alert(JSON.parse(notificationBody.body).content);
          });
        })

over和subscribe的方法不起作用,所以我想构建的websocket不起作用。 IntelliJ说我"未解决的功能或方法超过"和"未解决的功能或方法订阅"。我已经包含在我的package.json stompjs和sockjs-client(正确的版本)中。最初的要求不会让我有问题,但是我无法运行这个脚本。有人可以帮助我吗?!非常感谢

1 个答案:

答案 0 :(得分:1)

也许是因为require(“stompjs”)中的引号错误? 这是我的工作示例:

let SockJS = require('sockjs-client');
const Stomp = require('stompjs');

function register(registrations) {
    let socket = SockJS('/webSocketEndPoint');
    let stompClient = Stomp.over(socket);
    stompClient.connect({}, function(frame) {
        registrations.forEach(function (registration) {
            stompClient.subscribe('/topic/notification', function (notificationBody) {
             alert(JSON.parse(notificationBody.body).content);
          });
        });
    });
}