【问题标题】:How to communicate with spring websocket如何与spring websocket通信
【发布时间】:2018-01-08 23:25:56
【问题描述】:

我正在尝试构建一个应用程序,在该应用程序中,服务器将在某个时间间隔内不断向客户端推送消息。 我有一个像这样的简单 html 文件。

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="sockjs/sockjs.js"></script>
<script src="stomp/stomp.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<button ng-click='connect()'>hi</button>
</div>

<script>
var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope) {
    $scope.connect = function() {
        var socket = new SockJS('http://localhost:8099/myws');
        $scope.stompClient = Stomp.over(socket);
        $scope.stompClient.connect({}, function (frame) {
            console.log('Connected:bhabani ' + frame);
            $scope.stompClient.subscribe('http://localhost:8099/topic/jobconfig', function (wsdata) {
                console.log("helloooooooooooooooooooooooooooooooooooooooooooooooooo");
                console.log(wsdata);
            });
        });
    }
});
</script>

我在文件系统中打开了 html 文件。 file:///export/data1/test-ws.html 在浏览器中。

现在我有一个像这样的弹簧网络套接字。

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Autowired
    private GreetingController gc;

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/myws").setAllowedOrigins("*").withSockJS();
        new Thread(gc).start();
    }

}

有一个像这样的问候控制器,它应该在一些内部向主题推送消息

@Component
public class GreetingController implements Runnable{

    @Autowired
    private SimpMessagingTemplate template;

    public void run() {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        while(true){
        try {
            System.out.println("Sending");
            Thread.sleep(1000);
            template.convertAndSend("/topic/jobconfig", new Greeting("hi"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        }
    }

在我按下连接按钮的地方,我可以看到连接已建立。 但在那之后我没有看到任何应该从服务器推送的消息进入浏览器。

我希望在每个时间间隔内都会在我的浏览器控制台中打印“helloooooooooooooo”消息。

【问题讨论】:

    标签: javascript spring spring-mvc websocket sockjs


    【解决方案1】:

    将 stomp 客户端订阅代码中的 URL 从 http://localhost:8099/topic/jobconfig 更改为 /topic/jobconfig

    $scope.stompClient.subscribe('/topic/jobconfig', function(wsdata) {
                        console.log("hello");
                        console.log(wsdata);
                    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-29
      • 1970-01-01
      • 1970-01-01
      • 2022-09-24
      • 2020-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多