Commit fdb194b5 authored by 杨伊博's avatar 杨伊博

web socket 2

parent 4751474b
...@@ -50,6 +50,12 @@ ...@@ -50,6 +50,12 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId> <artifactId>spring-boot-starter-websocket</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
</dependencies> </dependencies>
......
...@@ -5,10 +5,14 @@ package com.us.example; ...@@ -5,10 +5,14 @@ package com.us.example;
*/ */
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import static org.springframework.boot.SpringApplication.run; import static org.springframework.boot.SpringApplication.run;
@ComponentScan(basePackages ="com.us.example")
@SpringBootApplication @SpringBootApplication
@EnableScheduling
public class Application { public class Application {
public static void main(String[] args) { public static void main(String[] args) {
......
...@@ -4,11 +4,14 @@ package com.us.example.controller; ...@@ -4,11 +4,14 @@ package com.us.example.controller;
import com.us.example.bean.Message; import com.us.example.bean.Message;
import com.us.example.bean.Response; import com.us.example.bean.Response;
import com.us.example.service.WebSocketService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo; import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.security.Principal; import java.security.Principal;
...@@ -18,7 +21,10 @@ import java.security.Principal; ...@@ -18,7 +21,10 @@ import java.security.Principal;
*/ */
@Controller @Controller
public class WebSocketController { public class WebSocketController {
@Autowired
private WebSocketService ws;
//http://localhost:8080/ws
@MessageMapping("/welcome")//浏览器发送请求通过@messageMapping 映射/welcome 这个地址。 @MessageMapping("/welcome")//浏览器发送请求通过@messageMapping 映射/welcome 这个地址。
@SendTo("/topic/getResponse")//服务器端有消息时,会订阅@SendTo 中的路径的浏览器发送消息。 @SendTo("/topic/getResponse")//服务器端有消息时,会订阅@SendTo 中的路径的浏览器发送消息。
public Response say(Message message) throws Exception { public Response say(Message message) throws Exception {
...@@ -26,6 +32,15 @@ public class WebSocketController { ...@@ -26,6 +32,15 @@ public class WebSocketController {
return new Response("Welcome, " + message.getName() + "!"); return new Response("Welcome, " + message.getName() + "!");
} }
//http://localhost:8080/Welcome1
@RequestMapping("/Welcome1")
@ResponseBody
public String say2()throws Exception
{
ws.sendMessage();
return "is ok";
}
// @Autowired // @Autowired
// private SimpMessagingTemplate messagingTemplate;//1 // private SimpMessagingTemplate messagingTemplate;//1
// //
......
package com.us.example.service;
/**
* Created by yangyibo on 17/1/12.
*/
import com.us.example.bean.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;
@Service
public class WebSocketService {
@Autowired
private SimpMessagingTemplate template;
public void sendMessage() throws Exception{
for(int i=0;i<10;i++)
{
Thread.sleep(1000);
template.convertAndSend("/topic/getResponse",new Response("Welcome,yangyibo !"+i));
System.out.println("----------------------yangyibo"+i);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment