Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/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
Angularjs 角度+;ngStomp+;弹簧websocket控制器未执行_Angularjs_Spring Mvc_Stomp_Spring Websocket_Sockjs - Fatal编程技术网

Angularjs 角度+;ngStomp+;弹簧websocket控制器未执行

Angularjs 角度+;ngStomp+;弹簧websocket控制器未执行,angularjs,spring-mvc,stomp,spring-websocket,sockjs,Angularjs,Spring Mvc,Stomp,Spring Websocket,Sockjs,我已经搜索了所有的教程,并且完全按照那里的解释做了,但是我无法找到我的控制器 以下是我的websocket xml配置: <websocket:handlers> <websocket:mapping path="/updateHandler" handler="updateHandler"/> <websocket:sockjs/> </websocket:handlers> <websocket:message-brok

我已经搜索了所有的教程,并且完全按照那里的解释做了,但是我无法找到我的控制器

以下是我的websocket xml配置:

<websocket:handlers>
    <websocket:mapping path="/updateHandler" handler="updateHandler"/>
    <websocket:sockjs/>
</websocket:handlers>

<websocket:message-broker application-destination-prefix="/app">
    <websocket:stomp-endpoint path="/update">
        <websocket:sockjs/>
    </websocket:stomp-endpoint>
    <websocket:simple-broker prefix="/topic"/>
</websocket:message-broker>
还有我的控制器

@RestController
public class WebSocketController {

    @MessageMapping("/update")
    @SendTo("/topic/messages")
    public OutputMessage sendmMessage(Message message) {

        return new OutputMessage(message, new Date());
    }
}
我从angular使用ngStomp,建议如下:

    var message = {message: 'message body', id: 1};

            $scope.testWebSocket = function () {
                $stomp.setDebug(function (args) {
                    console.log(args + '\n');
                });

                $stomp.connect('/myAppContext/update', {})
                        .then(function (frame) {
                            var connected = true;
                            var subscription = $stomp.subscribe('/topic/messages', function (payload, headers, res) {
                                $scope.payload = payload;
                            }, {});


                            $stomp.send('/myAppContext/app/update', message);

                            subscription.unsubscribe();


                            $stomp.disconnect(function () {
                              console.error('disconnected');
                            });
                        }, function(error){
                            console.error(error);
                        });
            };
我的消息类:

public class Message {

    private String message;
    private int id;

    public Message() {

    }

    public Message(int id, String text) {
        this.id = id;
        this.message = text;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}
我的OutputMessage类:

public class OutputMessage extends Message {

private Date time;

public OutputMessage(Message original, Date time) {
    super(original.getId(), original.getMessage());
}

public Date getTime() {
    return time;
}

public void setTime(Date time) {
    this.time = time;
}
执行testWebSocket()时,我得到以下输出:

Opening Web Socket...
Web Socket Opened...

>>> CONNECT
accept-version:1.1,1.0
heart-beat:10000,10000

<<< CONNECTED
version:1.1
heart-beat:0,0
user-name:user@domain.com

connected to server undefined

>>> SUBSCRIBE
id:sub-0
destination:/topic/messages

>>> SEND
destination:/myAppContext/app/update
content-length:33

{"message":"message body","id":1}
正在打开Web套接字。。。
Web套接字已打开。。。
>>>连接
接受版本:1.1,1.0
心跳:1000010000
>订阅
id:sub-0
目的地:/topic/消息
>>>发送
目的地:/myAppContext/app/update
内容长度:33
{“消息”:“消息体”,“id”:1}
为什么连接到未定义的服务器? 为什么我的控制器在发送消息后从未执行过


我正在使用带有security-core-3.2.5和Tomcat server 8.0.18的spring-4.1.4作为一个非漂亮的解决方案,我将websocket配置移动到Java配置中,它就可以工作了

配置如下:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

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

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/update")
                .withSockJS();
    }
我真的不知道为什么

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

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

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/update")
                .withSockJS();
    }