Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
S
springBoot
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
WitCloud
springBoot
Commits
fdb194b5
Commit
fdb194b5
authored
Jan 12, 2017
by
杨伊博
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
web socket 2
parent
4751474b
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
51 additions
and
0 deletions
+51
-0
pom.xml
springWebSocket/pom.xml
+6
-0
Application.java
...ngWebSocket/src/main/java/com/us/example/Application.java
+4
-0
WebSocketController.java
...n/java/com/us/example/controller/WebSocketController.java
+15
-0
WebSocketService.java
...rc/main/java/com/us/example/service/WebSocketService.java
+26
-0
No files found.
springWebSocket/pom.xml
View file @
fdb194b5
...
...
@@ -50,6 +50,12 @@
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-websocket
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework
</groupId>
<artifactId>
spring-messaging
</artifactId>
<version>
4.2.3.RELEASE
</version>
</dependency>
</dependencies>
...
...
springWebSocket/src/main/java/com/us/example/Application.java
View file @
fdb194b5
...
...
@@ -5,10 +5,14 @@ package com.us.example;
*/
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.context.annotation.ComponentScan
;
import
org.springframework.scheduling.annotation.EnableScheduling
;
import
static
org
.
springframework
.
boot
.
SpringApplication
.
run
;
@ComponentScan
(
basePackages
=
"com.us.example"
)
@SpringBootApplication
@EnableScheduling
public
class
Application
{
public
static
void
main
(
String
[]
args
)
{
...
...
springWebSocket/src/main/java/com/us/example/controller/WebSocketController.java
View file @
fdb194b5
...
...
@@ -4,11 +4,14 @@ package com.us.example.controller;
import
com.us.example.bean.Message
;
import
com.us.example.bean.Response
;
import
com.us.example.service.WebSocketService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.messaging.handler.annotation.MessageMapping
;
import
org.springframework.messaging.handler.annotation.SendTo
;
import
org.springframework.messaging.simp.SimpMessagingTemplate
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
java.security.Principal
;
...
...
@@ -18,7 +21,10 @@ import java.security.Principal;
*/
@Controller
public
class
WebSocketController
{
@Autowired
private
WebSocketService
ws
;
//http://localhost:8080/ws
@MessageMapping
(
"/welcome"
)
//浏览器发送请求通过@messageMapping 映射/welcome 这个地址。
@SendTo
(
"/topic/getResponse"
)
//服务器端有消息时,会订阅@SendTo 中的路径的浏览器发送消息。
public
Response
say
(
Message
message
)
throws
Exception
{
...
...
@@ -26,6 +32,15 @@ public class WebSocketController {
return
new
Response
(
"Welcome, "
+
message
.
getName
()
+
"!"
);
}
//http://localhost:8080/Welcome1
@RequestMapping
(
"/Welcome1"
)
@ResponseBody
public
String
say2
()
throws
Exception
{
ws
.
sendMessage
();
return
"is ok"
;
}
// @Autowired
// private SimpMessagingTemplate messagingTemplate;//1
//
...
...
springWebSocket/src/main/java/com/us/example/service/WebSocketService.java
0 → 100644
View file @
fdb194b5
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
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment