测试完成
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package top.fjy8018.websocket;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class WebsocketApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(WebsocketApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package top.fjy8018.websocket.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
|
||||
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
|
||||
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
|
||||
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
* @date 2018-06-09 19:10
|
||||
*/
|
||||
@Configuration
|
||||
@EnableWebSocketMessageBroker
|
||||
public class WebSocketStompConfig extends AbstractWebSocketMessageBrokerConfigurer {
|
||||
/**
|
||||
* 注册stomp的端点
|
||||
*/
|
||||
@Override
|
||||
public void registerStompEndpoints(StompEndpointRegistry registry) {
|
||||
// 允许使用socketJs方式访问,访问点为webSocketServer,允许跨域
|
||||
// 在网页上我们就可以通过这个链接
|
||||
// http://localhost:8080/webSocketServer
|
||||
// 来和服务器的WebSocket连接
|
||||
registry.addEndpoint("/webSocketServer").setAllowedOrigins("*").withSockJS();
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置信息代理
|
||||
*/
|
||||
@Override
|
||||
public void configureMessageBroker(MessageBrokerRegistry registry) {
|
||||
// 订阅Broker名称
|
||||
registry.enableSimpleBroker("/queue", "/topic");
|
||||
// 全局使用的消息前缀(客户端订阅路径上会体现出来)
|
||||
registry.setApplicationDestinationPrefixes("/app");
|
||||
// 点对点使用的订阅前缀(客户端订阅路径上会体现出来),不设置的话,默认也是/user/
|
||||
// registry.setUserDestinationPrefix("/user/");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package top.fjy8018.websocket.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import top.fjy8018.websocket.entity.ServerMessage;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
* @date 2018-06-09 19:15
|
||||
*/
|
||||
@Controller
|
||||
public class MessageController {
|
||||
|
||||
@Autowired
|
||||
private SimpMessagingTemplate messagingTemplate;
|
||||
|
||||
//客户端只要订阅了/topic/subscribeTest主题,调用这个方法即可
|
||||
public void templateTest() {
|
||||
messagingTemplate.convertAndSend("/topic/subscribeTest", new ServerMessage("服务器主动推的数据"));
|
||||
}
|
||||
|
||||
@RequestMapping("/test")
|
||||
public String test(){
|
||||
return "webSocket.html";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package top.fjy8018.websocket.controller;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.messaging.handler.annotation.MessageMapping;
|
||||
import org.springframework.messaging.handler.annotation.SendTo;
|
||||
import org.springframework.messaging.simp.annotation.SubscribeMapping;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import top.fjy8018.websocket.entity.ClientMessage;
|
||||
import top.fjy8018.websocket.entity.ServerMessage;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
* @date 2018-06-09 19:12
|
||||
*/
|
||||
@Slf4j
|
||||
@Controller
|
||||
public class WebSocketController {
|
||||
|
||||
@MessageMapping("/sendTest")
|
||||
@SendTo("/topic/subscribeTest")
|
||||
public ServerMessage sendDemo(ClientMessage message) {
|
||||
log.info("接收到了信息" + message.getName());
|
||||
return new ServerMessage("你发送的消息为:" + message.getName());
|
||||
}
|
||||
|
||||
@SubscribeMapping("/subscribeTest")
|
||||
public ServerMessage sub() {
|
||||
log.info("XXX用户订阅了我。。。");
|
||||
return new ServerMessage("感谢你订阅了我。。。");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package top.fjy8018.websocket.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
* @date 2018-06-09 19:11
|
||||
*/
|
||||
@Data
|
||||
public class ClientMessage {
|
||||
|
||||
private String name;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package top.fjy8018.websocket.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
* @date 2018-06-09 19:12
|
||||
*/
|
||||
@Data
|
||||
public class ServerMessage {
|
||||
|
||||
private String responseMessage;
|
||||
|
||||
public ServerMessage(String responseMessage) {
|
||||
this.responseMessage = responseMessage;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user