webSocket

1 webSocket介绍

WebSocket是一种在单个TCP连接上提供全双工通信的协议,允许在客户端和服务器之间进行实时数据传输。相比传统的HTTP协议,WebSocket更加轻量级且能够提供低延迟的实时通信。

WebSocket建立在TCP协议之上,通过在HTTP握手阶段使用Upgrade头来升级连接。一旦连接建立,客户端和服务器之间就可以直接发送数据帧,而无需重新建立连接。这种全双工通信方式大大减少了通信延迟,并提供了更高效的实时数据传输。

服务端API

Tomcat的7.0.5 版本开始支持WebSocket,并且实现了Java WebSocket规范。

Java WebSocket应用由一系列的Endpoint组成。Endpoint是一个iava对象,代表WebSocket链接的一端,对于服务端,我们可以视为处理具体WebSocket消息的接口。

我们可以通过两种方式定义Endpoint:
**第一种是编程式:**即继承类javax.websocket.Endpoint并实现其方法

第二种是注解式: 即定义一个POJO,并添加 @ServerEndpoint相关注解

Endpoint实例在Websocket握手时创建,并在客户端与服务端链接过程中有效,最后在链接关闭时结束。在Endpoint接口中明确定义了与其生命周期相关的方法, 规范实现者确保生命周期的各个阶段调用实例的相关方法。生命周期方法

2 webSocket项目 (在线聊天室)

**步骤一:**编写配置类,扫描添加有@ServerEndpoint注解的Bean

复制代码
@Configuration
public class WebSocketConfig {

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

步骤二:将 HttpSession保存起来

存储 httpSession是为了在websocket连接建立之后能够获取到session对象

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

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

步骤三:websocket连接诶建立,发送消息,断开

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

    private static final Map<String,Session> onlinesUsers = new ConcurrentHashMap<>();

    private HttpSession httpSession;

    /**
     * 建立websocket连接后,被调用
     * @param session
     */
    @OnOpen
    public void onOpen(Session session, EndpointConfig config) throws IOException {

        //1. 将session进行保存
        this.httpSession = (HttpSession)config.getUserProperties().get(HttpSession.class.getName());
        String user = (String) this.httpSession.getAttribute("user");
        onlinesUsers.put(user,session);
        //2.广播消息,需要将登陆的所有用户推送给所有的用户
        String message = MessageUtils.getMessage(true, null, getFrends());
        broadcastAllUser(message);
    }

    private Set getFrends(){
        Set<String> strings = onlinesUsers.keySet();
        return strings;
    }

    private void broadcastAllUser(String message) throws IOException {

            try {
                for (Map.Entry<String, Session> entry: onlinesUsers.entrySet()) {
                    //获取到所有用户对应的session对象
                    Session session = entry.getValue();
                    //发送消息
                    session.getBasicRemote().sendText(message);

                }
            } catch (IOException e) {
                throw new RuntimeException(e);
        }
    }

    /**
     * 浏览器发送消息到服务器
     *
     * 张三》》》李四
     * @param message
     */
    @OnMessage
    public void onMessage(String message){

        //将消息推送给指定的用户
        Message msg = null;
        try {
            msg = JSON.parseObject(message, Message.class);
            //获取消息接受方的用户名
            String toName = msg.getToName();
            String mess = msg.getMessage();
            //获取消息接收方用户对象的session对象
            Session session = onlinesUsers.get(toName);
            String user = (String) this.httpSession.getAttribute("user");
            String msg1 = MessageUtils.getMessage(false, user, mess);
            session.getBasicRemote().sendText(msg1);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }



    }


    /**
     * 连接断开时被调用
     * @param session
     */
    @OnClose
    public void onClose(Session session) throws IOException {
        //从onlineusers剔除当前用户的session对象
        String user = (String) this.httpSession.getAttribute("user");
        onlinesUsers.remove(user);
        //2.通知其它的所有用户,当前用户下线了
        String message = MessageUtils.getMessage(true, null, getFrends());
        broadcastAllUser(message);

    }



}
相关推荐
妳人話9 分钟前
TCP的三次握手和四次挥手
网络·网络协议·tcp/ip
纳祥科技22 分钟前
分享:一种为蓝牙、WIFI、U段音频发射设备提供ARC回传数字音频桥接功能的方案
网络·单片机·音视频
2501_916013741 小时前
HTTPS 抓包难点分析,从端口到工具的实战应对
网络协议·http·ios·小程序·https·uni-app·iphone
萧鼎1 小时前
深入解析 Python 的 pytun 库:虚拟网络接口与隧道技术实战指南
服务器·网络·python
Yuki’2 小时前
网络编程---UDP
c语言·网络·网络协议·udp
鲸屿1952 小时前
python之socket网络编程
开发语言·网络·python
天天年年天天。3 小时前
TCP/IP、HTTP 和 HTTPS简介
网络
傻傻虎虎3 小时前
【CentOS7】使用yum安装出错,报HTTPS Error 404 - Not Found
网络协议·http·https
徐子元竟然被占了!!3 小时前
DNS协议
网络协议
想睡hhh3 小时前
网络原理——传输层协议UDP
网络·网络协议·udp