File tree Expand file tree Collapse file tree
src/main/java/com/us/example Expand file tree Collapse file tree Original file line number Diff line number Diff line change 5050 <groupId >org.springframework.boot</groupId >
5151 <artifactId >spring-boot-starter-websocket</artifactId >
5252 </dependency >
53+
54+ <dependency >
55+ <groupId >org.springframework</groupId >
56+ <artifactId >spring-messaging</artifactId >
57+ <version >4.2.3.RELEASE</version >
58+ </dependency >
5359 </dependencies >
5460
5561
Original file line number Diff line number Diff line change 55 */
66import org .springframework .boot .autoconfigure .SpringBootApplication ;
77import org .springframework .context .ConfigurableApplicationContext ;
8+ import org .springframework .context .annotation .ComponentScan ;
9+ import org .springframework .scheduling .annotation .EnableScheduling ;
810
911import static org .springframework .boot .SpringApplication .run ;
1012
13+ @ ComponentScan (basePackages ="com.us.example" )
1114@ SpringBootApplication
15+ @ EnableScheduling
1216public class Application {
1317
1418 public static void main (String [] args ) {
Original file line number Diff line number Diff line change 44
55import com .us .example .bean .Message ;
66import com .us .example .bean .Response ;
7+ import com .us .example .service .WebSocketService ;
78import org .springframework .beans .factory .annotation .Autowired ;
89import org .springframework .messaging .handler .annotation .MessageMapping ;
910import org .springframework .messaging .handler .annotation .SendTo ;
1011import org .springframework .messaging .simp .SimpMessagingTemplate ;
1112import org .springframework .stereotype .Controller ;
13+ import org .springframework .web .bind .annotation .RequestMapping ;
14+ import org .springframework .web .bind .annotation .ResponseBody ;
1215
1316import java .security .Principal ;
1417
1821 */
1922@ Controller
2023public class WebSocketController {
24+ @ Autowired
25+ private WebSocketService ws ;
2126
27+ //http://localhost:8080/ws
2228 @ MessageMapping ("/welcome" )//浏览器发送请求通过@messageMapping 映射/welcome 这个地址。
2329 @ SendTo ("/topic/getResponse" )//服务器端有消息时,会订阅@SendTo 中的路径的浏览器发送消息。
2430 public Response say (Message message ) throws Exception {
2531 Thread .sleep (1000 );
2632 return new Response ("Welcome, " + message .getName () + "!" );
2733 }
2834
35+ //http://localhost:8080/Welcome1
36+ @ RequestMapping ("/Welcome1" )
37+ @ ResponseBody
38+ public String say2 ()throws Exception
39+ {
40+ ws .sendMessage ();
41+ return "is ok" ;
42+ }
43+
2944// @Autowired
3045// private SimpMessagingTemplate messagingTemplate;//1
3146//
Original file line number Diff line number Diff line change 1+ package com .us .example .service ;
2+
3+ /**
4+ * Created by yangyibo on 17/1/12.
5+ */
6+
7+ import com .us .example .bean .Response ;
8+ import org .springframework .beans .factory .annotation .Autowired ;
9+ import org .springframework .messaging .simp .SimpMessagingTemplate ;
10+ import org .springframework .stereotype .Service ;
11+
12+ @ Service
13+ public class WebSocketService {
14+ @ Autowired
15+ private SimpMessagingTemplate template ;
16+
17+ public void sendMessage () throws Exception {
18+ for (int i =0 ;i <10 ;i ++)
19+ {
20+ Thread .sleep (1000 );
21+ template .convertAndSend ("/topic/getResponse" ,new Response ("Welcome,yangyibo !" +i ));
22+ System .out .println ("----------------------yangyibo" +i );
23+ }
24+ }
25+
26+ }
You can’t perform that action at this time.
0 commit comments