黑马 - websocket搭建在线聊天室

这里写自定义目录标题

一、消息推送常见方式

1、轮训方式

2、SSE(server-send event)服务器发送事件

3、websocket

二、websocket 是什么?

websocket 是一种基于TCP 连接上进行全双工通信的协议

三、websocket api的介绍

1、客户端 (浏览器)

  • websocket对象创建

  • websocket对象相关事件

  • websocket 对象提供的方法

2、服务端api Tomcat 的7.0.5版本开始支持 websocket , 并且实现了Java websocket 规范。

Java websocket 应用由一系列的 Endpoint组成。

Endpoint 是一个java对象,代表websocket连接的一段。对于服务端,我们可以视其为处理具体websocket消息的接口

我们可以通过两种方式定义EndPoint:

编程式,继承类 javax.websocket.Endpoint 并实现其方法

注解式,定义一个pojo,并添加@ServerEndPoint相关注解

  • 服务端如何接收客户端发过来的数据呢?

    • 编程式

      通过添加MessageHandler消息处理器来接收消息

    • 注解式

      在定义endpoint时,通过@OnMessage 注解指定接收消息的方法

  • 服务器如何推送消息给客户端

    发送消息由RemoteEndpoint完成,其实例由Session维护。

四、实现在线聊天室

1、需求

通过websocket实现在线聊天室

2、聊天室流程分析

3、消息格式

4、代码实现

1) 引入依赖

xml 复制代码
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>

2)编写配置类

扫描所有添加 @ServerEndpoint注解的Bean

java 复制代码
@Configuration
public class WebsocketConfig {

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
}

编写配置类,用于获取 HttpSession 对象

java 复制代码
public class GetHttpSessionConfig extends ServerEndpointConfig.Configurator {

    /**
     * @param sec
     * @param request  握手请求
     * @param response
     */
    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
        // 获取HttpSession对象
        HttpSession httpSession = (HttpSession) request.getHttpSession();
        // 将 HttpSession对象保存起来
        sec.getUserProperties().put(HttpSession.class.getName(), httpSession);
    }
}

在@ServerEndpoint 注解中引入配置器 @ServerEndpoint(value = "/chat", configurator = GetHttpSessionConfig.class)

java 复制代码
@ServerEndpoint(value = "/chat", configurator = GetHttpSessionConfig.class)
@Component
public class ChatEndpoint {

    /**
     * 使用static
     * ChatEndpoint是多例的,多个实例共享一个Map对象
     */
    private static final Map<String, Session> onlineUsers = new ConcurrentHashMap<>();

    private HttpSession httpSession;

    @OnOpen
    public void onOpen(Session session, EndpointConfig endpointConfig) {
        this.httpSession = (HttpSession) endpointConfig.getUserProperties().get(HttpSession.class.getName());
        String userName = (String) httpSession.getAttribute("user");
        // 1、将我们的session进行保存
        onlineUsers.put(userName, session);
        // 2、广播消息,将登录的用户推送给所有的用户
        String message = MessageUtils.getMessage(true, null, userName + "上线");
        boardcast(message);
        // 3、

    }


    /**
     * 广播消息
     */
    private void boardcast(String message) {
        // 遍历 map 集合
        Set<Map.Entry<String, Session>> entries = onlineUsers.entrySet();
        for (Map.Entry<String, Session> entry : entries) {
            Session session = entry.getValue();
            try {
                session.getBasicRemote().sendText(message);
            } catch (IOException e) {
                // 记录日志
            }
        }
    }

    /**
     * 浏览器发送消息到服务端,该方法会被调用
     */
    @OnMessage
    public void onMessage(String message, EndpointConfig endpointConfig) {
        try {
            this.httpSession = (HttpSession) endpointConfig.getUserProperties().get(HttpSession.class.getName());
            String fromName = (String) httpSession.getAttribute("user");
            // 将消息推送给指定的用户   message : {"toName":"张三","message":"你好"}
            ClientMessage message1 = JSON.parseObject(message, ClientMessage.class);
            String toName = message1.getToName();
            Session session = onlineUsers.get(toName);
            String message2 = MessageUtils.getMessage(false, fromName, message1.getMessage());
            session.getBasicRemote().sendText(message2);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 当websocket连接断开时,此方法会被处罚
     */
    @OnClose
    public void onClose(Session session, EndpointConfig endpointConfig) {
        // 从在线用户集合中剔除断开连接的用户
        this.httpSession = (HttpSession) endpointConfig.getUserProperties().get(HttpSession.class.getName());
        String userName = (String) httpSession.getAttribute("user");
        onlineUsers.remove(userName);
        // 通知其他用户当前用户下线
        String message = MessageUtils.getMessage(true, null, userName + "上线");
        boardcast(message);
    }
}
相关推荐
小白杨树树10 小时前
【WebSocket】SpringBoot项目中使用WebSocket
spring boot·websocket·网络协议
Sherry00711 小时前
实时数据传输协议:WebSocket vs MQTT
前端·websocket
Icoolkj13 小时前
WebRTC 与 WebSocket 的关联关系
websocket·网络协议·webrtc
2501_9160074718 小时前
绕过 Xcode?使用 Appuploader和主流工具实现 iOS 上架自动化
websocket·网络协议·tcp/ip·http·网络安全·https·udp
2501_9160137418 小时前
使用 Windows 完成 iOS 应用上架:Appuploader对比其他证书与上传方案
websocket·网络协议·tcp/ip·http·网络安全·https·udp
一只帆記2 天前
HTTP、WebSocket、SSE 对比
websocket·http
Sparkxuan2 天前
封装WebSocket
前端·websocket
2501_915921432 天前
高敏感应用如何保护自身不被逆向?iOS 安全加固策略与工具组合实战(含 Ipa Guard 等)
websocket·网络协议·tcp/ip·http·网络安全·https·udp
2501_915106322 天前
App 上线后还能加固吗?iOS 应用的动态安全补强方案实战分享(含 Ipa Guard 等工具组合)
websocket·网络协议·tcp/ip·http·网络安全·https·udp
2501_915918412 天前
iOS 项目怎么构建稳定性保障机制?一次系统性防错经验分享(含 KeyMob 工具应用)
websocket·网络协议·tcp/ip·http·网络安全·https·udp