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

modify Websocket package

parent 5e2d5fa2
package com.us.example.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter{
//为ws.HTML 提供便捷的路径映射。
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/ws").setViewName("/ws");
registry.addViewController("/login").setViewName("/login");
registry.addViewController("/chat").setViewName("/chat");
}
}
...@@ -31,9 +31,9 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter{ ...@@ -31,9 +31,9 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
protected void configure(AuthenticationManagerBuilder auth) throws Exception { protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth auth
.inMemoryAuthentication() .inMemoryAuthentication()
.withUser("wyf").password("wyf").roles("USER") .withUser("admin").password("admin").roles("USER")
.and() .and()
.withUser("wisely").password("wisely").roles("USER"); .withUser("abel").password("abel").roles("USER");
} }
//5忽略静态资源的拦截 //5忽略静态资源的拦截
@Override @Override
......
...@@ -29,6 +29,18 @@ public class WebSocketController { ...@@ -29,6 +29,18 @@ public class WebSocketController {
private SimpMessagingTemplate messagingTemplate; private SimpMessagingTemplate messagingTemplate;
@RequestMapping(value = "/login")
public String login(){
return "login";
}
@RequestMapping(value = "/ws")
public String ws(){
return "ws";
}
@RequestMapping(value = "/chat")
public String chat(){
return "chat";
}
//http://localhost:8080/ws //http://localhost:8080/ws
@MessageMapping("/welcome")//浏览器发送请求通过@messageMapping 映射/welcome 这个地址。 @MessageMapping("/welcome")//浏览器发送请求通过@messageMapping 映射/welcome 这个地址。
@SendTo("/topic/getResponse")//服务器端有消息时,会订阅@SendTo 中的路径的浏览器发送消息。 @SendTo("/topic/getResponse")//服务器端有消息时,会订阅@SendTo 中的路径的浏览器发送消息。
...@@ -54,15 +66,15 @@ public class WebSocketController { ...@@ -54,15 +66,15 @@ public class WebSocketController {
* 此处是一段硬编码。如果发送人是wyf 则发送给 wisely 如果发送人是wisely 就发送给 wyf。 * 此处是一段硬编码。如果发送人是wyf 则发送给 wisely 如果发送人是wisely 就发送给 wyf。
* 通过当前用户,然后查找消息,如果查找到未读消息,则发送给当前用户。 * 通过当前用户,然后查找消息,如果查找到未读消息,则发送给当前用户。
*/ */
if (principal.getName().equals("wyf")) { if (principal.getName().equals("admin")) {
//通过convertAndSendToUser 向用户发送信息, //通过convertAndSendToUser 向用户发送信息,
// 第一个参数是接收消息的用户,第二个参数是浏览器订阅的地址,第三个参数是消息本身 // 第一个参数是接收消息的用户,第二个参数是浏览器订阅的地址,第三个参数是消息本身
messagingTemplate.convertAndSendToUser("wisely", messagingTemplate.convertAndSendToUser("abel",
"/queue/notifications", principal.getName() + "-send:" "/queue/notifications", principal.getName() + "-send:"
+ message.getName()); + message.getName());
} else { } else {
messagingTemplate.convertAndSendToUser("wyf", messagingTemplate.convertAndSendToUser("admin",
"/queue/notifications", principal.getName() + "-send:" "/queue/notifications", principal.getName() + "-send:"
+ message.getName()); + message.getName());
} }
......
server.port=8081 server.port=8090
\ No newline at end of file
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