为什么不调用该方法?

时间:2018-01-09 16:56:45

标签: spring angular ionic-framework websocket

使用Ionic框架,我正在尝试使用在port 8080本地启动的Spring应用程序实现websocket连接。

我用这篇文章实现了这个,在控制台中我可以看到websocket连接被打开了:

https://medium.com/oril/spring-boot-websockets-angular-5-f2f4b1c14cee

但是,当我输入消息并调用“sendMessage”函数时,不会在Spring端调用消息处理函数

enter image description here

enter image description here

修改1

WebSocketConfiguration.java:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration extends AbstractWebSocketMessageBrokerConfigurer{

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

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.setApplicationDestinationPrefixes("/api/v1")
                .enableSimpleBroker("/chat");
    }
}

ChatCtrl.java

@CrossOrigin
@RestController(value = "chatCtrl")
@RequestMapping("api/v1/chats")
public class ChatCtrl {

    @Autowired
    ChatRepo repo;

    @Autowired
    MessageRepo messageRepo;

    @Autowired
    private SimpMessagingTemplate template;

    @RequestMapping(value="/{id}/messages", method=RequestMethod.POST)
    public void processMessage(@PathVariable Long id, @RequestBody Message message){
        //Process Message
        System.out.println("Method invoked!");
        message.setChat(repo.findOne(id));
        messageRepo.save(message);
        //Notify clients
        this.template.convertAndSend("/chat", message);
    }
}
    

聊天details.ts

@Component({
        selector: 'page-chat-detail',
        templateUrl: 'chat-detail.html'
        })
export class ChatDetail {

    chat: ChatModel;
    currentUser: User;
    message: Message = new Message();

    private serverUrl = 'http://localhost:8080/socket';
    private stompClient;

    constructor()
    {
        this.initializeWebSocketConnection();
    }

...

    sendMessage(message: Message) {
        message.notifyTime = new Date();
        message.user = this.currentUser;
        message.chat = this.chat;
        this.stompClient.send("/api/v1/chats/" + message.chat.id + "/messages" , {}, message);
    }

    initializeWebSocketConnection(){
        let ws = new SockJS(this.serverUrl);
        this.stompClient = Stomp.over(ws);
        let that = this;
        this.stompClient.connect({}, function(frame) {
            that.stompClient.subscribe("/chat ", (message) => {
            if(message.body) {
                console.log(message.body);
            }
          });
        });
    }
}

修改2

AddEndpoint设置为/ socket,在前端,我从http://localhost:8080/socket调用新的SockJS

我认为没关系(你可以在第一张图片上看到)

setApplicationDestinationPrefixes我设置为api / v1,因为所有请求都通过该位置

enableSimpleBroker设置为聊天

现在我正在尝试,使用Send函数(stompClient.send(“/ api / v1 / chats /”+ message.chat.id +“/ messages”,{},message);)来调用Spring上的方法side,使用requestmapping定义

在其中,有System.out.println(“方法调用!”);所以我可以看到该函数是否被触发,但是当我查看日志时,我发现它不是

还有SimpMessagingTemplate.convertAndSend(“/ chat”,message);将消息返回到所有打开的连接

我想知道在调用该位置的spring方法时我在哪里错了

0 个答案:

没有答案