2015-07-24 15 views
9

Spring MVC'yi kullanarak Web Soket'i uyguladım ve bu benim için iyi çalışıyor çünkü bir tarayıcıdan bu kodu kullanarak bu soket için açık olan başka bir tarayıcıya çalışıyorum. Normal İstek Çağrısından @SendTo çağrısı Nasıl Yapılır? I.e @RequestMapping

@MessageMapping("/hello") 
    @SendTo("/topic/greetings") 
    public HelloMessage greeting(HelloMessage message) throws Exception { 
     Thread.sleep(3000); // simulated delay 
     return message; 
    } 

herhangi biri bu kullanmayı deneyin gelmiş, normal API controller.I gelen ("/ konu/selam") @SendTo kimi arasam için bana yardımcı olabilir ama bana

@RequestMapping(value = "/sendMessage") 
    @SendTo("/topic/greetings") 
    public HelloMessage sendMessage() throws Exception { 
     return new HelloMessage((int) Math.random(), "This is Send From Server"); 
    } 

için çalışmıyor bunun için herhangi bir fikrin var mı?

Teşekkür

cevap

11

bu biz WebSocket'e açmak için mesaj gönderebilirsiniz kullanarak bu

@Autowired 
private SimpMessagingTemplate template; 


@RequestMapping(value = "/sendMessage") 
public void sendMessage() throws Exception { 
    this.template.convertAndSend("/topic/greetings", new HelloMessage(
      (int) Math.random(), "This is Send From Server")); 
} 

için çözüm bulduk.

Teşekkürler

İlgili konular