Skip to content

Commit fdb194b

Browse files
committed
web socket 2
1 parent 4751474 commit fdb194b

4 files changed

Lines changed: 51 additions & 0 deletions

File tree

springWebSocket/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
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

springWebSocket/src/main/java/com/us/example/Application.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
*/
66
import org.springframework.boot.autoconfigure.SpringBootApplication;
77
import org.springframework.context.ConfigurableApplicationContext;
8+
import org.springframework.context.annotation.ComponentScan;
9+
import org.springframework.scheduling.annotation.EnableScheduling;
810

911
import static org.springframework.boot.SpringApplication.run;
1012

13+
@ComponentScan(basePackages ="com.us.example")
1114
@SpringBootApplication
15+
@EnableScheduling
1216
public class Application {
1317

1418
public static void main(String[] args) {

springWebSocket/src/main/java/com/us/example/controller/WebSocketController.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44

55
import com.us.example.bean.Message;
66
import com.us.example.bean.Response;
7+
import com.us.example.service.WebSocketService;
78
import org.springframework.beans.factory.annotation.Autowired;
89
import org.springframework.messaging.handler.annotation.MessageMapping;
910
import org.springframework.messaging.handler.annotation.SendTo;
1011
import org.springframework.messaging.simp.SimpMessagingTemplate;
1112
import org.springframework.stereotype.Controller;
13+
import org.springframework.web.bind.annotation.RequestMapping;
14+
import org.springframework.web.bind.annotation.ResponseBody;
1215

1316
import java.security.Principal;
1417

@@ -18,14 +21,26 @@
1821
*/
1922
@Controller
2023
public 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
//
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
}

0 commit comments

Comments
 (0)