增加项目说明
This commit is contained in:
@ -4,11 +4,7 @@ import javax.websocket.Session;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* @author : 罗祥
|
||||
* @description :
|
||||
* @date :create in 2018/12/27
|
||||
*/
|
||||
|
||||
public interface Constant {
|
||||
|
||||
String USER_NAME="username";
|
||||
|
@ -8,9 +8,7 @@ import org.springframework.web.bind.annotation.PostMapping;
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
/**
|
||||
* @author : 罗祥
|
||||
* @description : 简单登录
|
||||
* @date :create in 2018/12/27
|
||||
*/
|
||||
@Controller
|
||||
public class LoginController {
|
||||
|
@ -21,20 +21,28 @@ import java.net.URLDecoder;
|
||||
@Component
|
||||
public class ChatSocket {
|
||||
|
||||
|
||||
@OnOpen //建立连接时候触发
|
||||
/**
|
||||
* 建立连接时候触发
|
||||
*/
|
||||
@OnOpen
|
||||
public void onOpen(Session session, @PathParam("username") String username) {
|
||||
// 这个方法是线程不安全的
|
||||
Constant.nameAndSession.putIfAbsent(username, session);
|
||||
}
|
||||
|
||||
|
||||
@OnClose //关闭连接时候触发
|
||||
/**
|
||||
* 关闭连接时候触发
|
||||
*/
|
||||
@OnClose
|
||||
public void onClose(Session session, @PathParam("username") String username) {
|
||||
Constant.nameAndSession.remove(username);
|
||||
}
|
||||
|
||||
@OnMessage //处理消息
|
||||
/**
|
||||
* 处理消息
|
||||
*/
|
||||
@OnMessage
|
||||
public void onMessage(Session session, String message, @PathParam("username") String username) throws UnsupportedEncodingException {
|
||||
// 防止中文乱码
|
||||
String msg = URLDecoder.decode(message, "utf-8");
|
||||
|
@ -4,12 +4,19 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
|
||||
|
||||
import javax.websocket.server.ServerEndpoint;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
*/
|
||||
@Configuration
|
||||
public class WebSocketConfig {
|
||||
|
||||
/***
|
||||
* 检测{@link javax.websocket.server.ServerEndpointConfig}和{@link ServerEndpoint} 类型的bean,
|
||||
* 并在运行时使用标准Java WebSocket时注册。
|
||||
* 我们在{@link com.heibaiying.springboot.websocket.WebSocketConfig}中就是使用@ServerEndpoint去声明websocket服务
|
||||
*/
|
||||
@Bean
|
||||
public ServerEndpointExporter serverEndpointExporter() {
|
||||
return new ServerEndpointExporter();
|
||||
|
Reference in New Issue
Block a user