Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.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
Javascript 如何与spring websocket通信_Javascript_Spring_Spring Mvc_Websocket_Sockjs - Fatal编程技术网

Javascript 如何与spring websocket通信

Javascript 如何与spring websocket通信,javascript,spring,spring-mvc,websocket,sockjs,Javascript,Spring,Spring Mvc,Websocket,Sockjs,我正在尝试构建一个应用程序,在这个应用程序中,服务器将在某个时间间隔内不断向客户端推送消息。 我有这样一个简单的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/sto

我正在尝试构建一个应用程序,在这个应用程序中,服务器将在某个时间间隔内不断向客户端推送消息。 我有这样一个简单的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>
@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();
        }
        }
    }
当我按下连接按钮时,我可以看到连接已建立。 但是在那之后,我没有看到任何应该从服务器推送的消息出现在浏览器中


我希望“helloooo”消息应该在每个时间间隔打印在我的浏览器控制台中。

从此
更改stomp客户端订阅代码中的URLhttp://localhost:8099/topic/jobconfig
至此
/topic/jobconfig

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