【Netty】实战:基于WebSocket的聊天室

news2024/9/20 16:52:42


本文将使用Netty快速实现一个聊天室应用,该应用基于WebSocket协议,用户可以在浏览器内聊天。

实现过程很简单,就几步。

一、处理Http请求

package cn.md.netty.websocket.groupchat;

import io.netty.channel.*;
import io.netty.handler.codec.http.*;
import io.netty.handler.ssl.SslHandler;
import io.netty.handler.stream.ChunkedNioFile;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.URISyntaxException;
import java.net.URL;

/**
 * * @Author: Martin
 * * @Date    2024/9/2 11:21
 * * @Description
 **/
public class HttpRequestHandler extends SimpleChannelInboundHandler<FullHttpRequest> {

    // websocket的请求地址
    private final String wsUri;

    // 页面文件的地址
    private static final File PAGE_FILE;


    static {
        URL location = HttpRequestHandler.class.getProtectionDomain()
                .getCodeSource().getLocation();
        String path = null;
        try {
            path = location.toURI() + "wsChatClient.html";
            path = path.contains("file:") ? path.substring(5):path;
            PAGE_FILE = new File("/Users/jack/Documents/TMP/wsChatClient.html");
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * see {@link #SimpleChannelInboundHandler(boolean)} with {@code true} as boolean parameter.
     */
    public HttpRequestHandler(String wsUri) {
        this.wsUri = wsUri;
    }

    /**
     * Is called for each message of type {@link I}.
     *
     * @param ctx the {@link ChannelHandlerContext} which this {@link SimpleChannelInboundHandler}
     *            belongs to
     * @param msg the message to handle
     * @throws Exception is thrown if an error occurred
     */
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
        if (wsUri.equalsIgnoreCase(request.uri())) {
            // 交给下一个InboudHandler 处理
            ctx.fireChannelRead(request.retain());
            return;
        }

        if (HttpUtil.is100ContinueExpected(request)) {
            // 发送 100 continue
            send100Continue(ctx);
        }
        // 读取文件内容到字节数组
        byte[] fileContent;
        try (RandomAccessFile file = new RandomAccessFile(PAGE_FILE, "r")) {
            fileContent = new byte[(int) file.length()];
            file.readFully(fileContent);
        } catch (IOException e) {
            // 处理文件读取错误
            DefaultFullHttpResponse errorResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
            ctx.writeAndFlush(errorResponse);
            return;
        }

        DefaultFullHttpResponse resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
        resp.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTF-8");

        boolean keepAlive = HttpUtil.isKeepAlive(request);
        if (keepAlive) {
            resp.headers().set(HttpHeaderNames.CONTENT_LENGTH, fileContent.length);
            resp.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
        }

        // 将文件内容设置为响应内容
        resp.content().writeBytes(fileContent);

        ChannelFuture channelFuture = ctx.writeAndFlush(resp);

        if (!keepAlive) {
            channelFuture.addListener(ChannelFutureListener.CLOSE);
        }
        channelFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture channelFuture) throws Exception {
                System.out.println("response empty last content success !");
            }
        });
    }

    private void send100Continue(ChannelHandlerContext ctx) {
        DefaultFullHttpResponse resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE);
        ctx.writeAndFlush(resp);
    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        Channel channel = ctx.channel();
        System.out.println("client " + channel.remoteAddress() + "异常");
        cause.printStackTrace();
        ctx.close();
    }
}

二、处理WebSocket帧

package cn.md.netty.websocket.groupchat;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.concurrent.GlobalEventExecutor;

/**
 * 处理Websocket帧
 * * @Author: Martin
 * * @Date    2024/9/2 13:21
 * * @Description
 **/
public class TextWebSocketFrameHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {

    public static ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);


    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        channels.add(channel);
        // 广播
        channels.writeAndFlush(new TextWebSocketFrame("[server] - " + channel.remoteAddress() + "加入"));
        System.out.println(channel.remoteAddress() + "加入");
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        channels.writeAndFlush(new TextWebSocketFrame("[server] - " + channel.remoteAddress() + "离开"));
        System.out.println(channel.remoteAddress() + "离开");
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        System.out.println(channel.remoteAddress() + "在线");
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        System.out.println(channel.remoteAddress() + "掉线");
    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        Channel channel = ctx.channel();
        System.out.println(channel.remoteAddress() + " 异常");
        cause.printStackTrace();
        ctx.close();
    }

    /**
     * Is called for each message of type {@link I}.
     *
     * @param ctx the {@link ChannelHandlerContext} which this {@link SimpleChannelInboundHandler}
     *            belongs to
     * @param msg the message to handle
     * @throws Exception is thrown if an error occurred
     */
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {
        System.out.println("收到消息:" + msg.text());
        Channel channel = ctx.channel();
        for (Channel ch : channels){
            if (channel != ch){
                ch.writeAndFlush(new TextWebSocketFrame(channel.remoteAddress() + ":" + msg.text()));
            }else {
                ch.writeAndFlush(new TextWebSocketFrame("[you]" + msg.text()));
            }
        }
    }

}

三、实现ChannelInitializer

package cn.md.netty.websocket.groupchat;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * * @Author: Martin
 * * @Date    2024/9/2 13:37
 * * @Description
 **/
public class WebSocketChatServerInitializer extends ChannelInitializer<SocketChannel> {

    /**
     * This method will be called once the {@link Channel} was registered. After the method returns this instance
     * will be removed from the {@link ChannelPipeline} of the {@link Channel}.
     *
     * @param ch the {@link Channel} which was registered.
     * @throws Exception is thrown if an error occurs. In that case it will be handled by
     *                   {@link #exceptionCaught(ChannelHandlerContext, Throwable)} which will by default close
     *                   the {@link Channel}.
     */
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {

        ch.pipeline()
                .addLast(new HttpServerCodec())
                .addLast(new HttpObjectAggregator(64 * 1024))
                .addLast(new ChunkedWriteHandler())
                .addLast(new HttpRequestHandler("/ws"))
                .addLast(new WebSocketServerProtocolHandler("/ws"))
                .addLast(new TextWebSocketFrameHandler());
    }
}

四、服务器启动程序

package cn.md.netty.websocket.groupchat;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * * @Author: Martin
 * * @Date    2024/9/2 13:40
 * * @Description
 **/
public class WebSocketChatServer {

    private final int port;

    public WebSocketChatServer(int port) {
        this.port = port;
    }

    public static void main(String[] args) throws Exception {
        new WebSocketChatServer(8088).run();
    }

    public void run() throws Exception {

        NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup,workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new WebSocketChatServerInitializer())
                    .option(ChannelOption.SO_BACKLOG,128)
                    .childOption(ChannelOption.SO_KEEPALIVE,true);

            ChannelFuture channelFuture = serverBootstrap.bind(port).sync();
            System.out.println("bind port success");
            channelFuture.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
            System.out.println("server stop");
        }


    }

}

五、编写客户端

编写html代码,我这里的页面地址是 /Users/jack/Documents/TMP/wsChatClient.html,你的需要修改HttpRequestHandler.java中的文件路径。代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>WebSocket Chat</title>
</head>
<body>


<script type="text/javascript">

    var socket;
    if (!window.WebSocket) {
        window.WebSocket = window.MozWebSocket;
    }

    if (window.WebSocket) {
        socket = new WebSocket("ws://localhost:8088/ws");
        socket.onmessage = function(event) {
            console.log("receive message:" + event.data)
            var ele = document.getElementById('respText');
            ele.value = ele.value + "\n" + event.data;
        };

        socket.onopen = function(event) {
            var ele = document.getElementById('respText');
            ele.value = "连接开启";
        }

        socket.onclose = function(event) {
            var ele = document.getElementById('respText');
            ele.value = ele.value + "连接被关闭";
        }

    } else {
        alert("Your browser does not support WebSocket!");
    }



    function sendMessage(message) {
        if (!window.WebSocket) {
            alert("Your browser does not support WebSocket!")
            return;
        }
        if (socket.readyState == WebSocket.OPEN) {
            socket.send(message);
            console.log("send message:" + message)
        } else {
            alert("The connection is not open.");
        }
    }


</script>


<form onsubmit="return false">
    <h3>WebSocket Chat</h3>

    <textarea name="" id= "respText" style="width: 500px;height: 300px"></textarea>
    <input type="text" name="message" style="width: 300px" value="Welcome to chat.marding.cn">
    <input type="button" value="发送" onclick="sendMessage(this.form.message.value)">
    </input>
</form>
</body>
</html>

六、测试

在这里插入图片描述

可以打开多个页面进行测试~~~


我是马丁,喜欢可以点赞+关注哦~ See you ~

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2102276.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

yolov8目标检测pyside6可视化图形界面+检测源码ui文件——用于计数统计

项目结构 YOLOv8模型加载&#xff1a;加载预训练的YOLOv8模型。PySide6 GUI&#xff1a;设计图形用户界面&#xff0c;用于显示检测结果和控制选项。摄像头/视频输入&#xff1a;从摄像头或视频文件读取图像帧。目标检测&#xff1a;使用YOLOv8模型对输入图像进行实时目标检测…

Explorer++:轻量级高效文件管理器!!

项目简介 Explorer 是一款专为Windows操作系统设计的轻量级且高效的文件管理器。作为Windows资源管理器的强大替代方案&#xff0c;它提供了丰富的特性和优化的用户体验&#xff0c;使得文件管理和组织变得更加便捷高效。无论是专业用户还是普通用户&#xff0c;都能从中受益&a…

Leetcode面试经典150题-92.反转链表II

解法都在代码里&#xff0c;不懂就留言或者私信 比反转链表I略微难一点点 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode() {}* ListNode(int val) { this.val val; }* ListNode(int val, Li…

0903作业+思维导图

一、作业 1》多态的实现 1、代码 #include <iostream>using namespace std; //父类 class Person { public:string name;int age; public:Person(){}Person(string n,int a):name(n),age(a){}~Person(){}//纯虚函数virtual void show() 0; }; //子类1 class Stu:publ…

游戏开发者必看:Perforce龙智即将携手亮相2024 Unreal Fest上海站,打造游戏开发版本控制新生态

2024年9月5- 6日&#xff08;周四-周五&#xff09;&#xff0c;Unreal Fest Shanghai 2024将在上海宝华万豪酒店隆重举行&#xff01;作为游戏行业备受瞩目的盛会之一&#xff0c;Unreal Fest每年都会吸引来自世界各地的技术专家和行业领导者齐聚一堂&#xff0c;分享最新的技…

『功能项目』销毁怪物蛋的Shaders消融特效【17】

本章项目成果展示 我们打开上一篇16主角的信息显示的项目&#xff0c; 本章要做的事情是在怪物消亡时生成一个销毁特效 首先创建一个Unlit Shader 重命名为Dissolve 双击进入脚本后编写脚本&#xff1a; Shader "Unlit/Dissolve"{Properties{//物体基础材质纹理[Hea…

Apache Kafka UI :一款功能丰富且美观的 Kafka 开源管理平台!!【送源码】

Apache Kafka UI 是一个免费的开源 Web UI&#xff0c;用于监控和管理 Apache Kafka 集群&#xff0c;可方便地查看 Kafka Brokers、Topics、消息、Consumer 等情况&#xff0c;支持多集群管理、性能监控、访问控制等功能。 1 特征 多集群管理&#xff1a; 在一个地方监控和管理…

软考高级网络规划设计师含金量高吗?

网络规划设计师含金量很高&#xff01;这个证书是计算机技术与软件领域的高级专业证书。 拿到这个证书的人&#xff0c;那在网络系统建设方面可是全能选手&#xff0c;从需求分析到规划设计&#xff0c;再到部署实施、评测运维&#xff0c;统统都能搞定。 他们得对网络技术应…

Flutter 小技巧之 Row/Column 即将支持 Flex.spacing

事实上这是一个相当久远的话题&#xff0c;如果对于前因后果不管兴趣&#xff0c;直接看最后就行。 这个需求最早提及应该是 2018 年初在 #16957 被人提起&#xff0c;因为在 Flutter 上 Wrap 有 runSpacing 和 spacing 用于配置垂直和水平间距&#xff0c;而为什么 Colum 和 …

单细胞组学大模型(3)--- scGPT,有非常详细的学习文档和应用说明,且有多种训练数据权重!

–https://doi.org/10.1038/s41592-024-02201-0 代码来源&#xff1a;https://github.com/bowang-lab/scGPT 学习参考&#xff1a;https://scgpt.readthedocs.io/en/latest/introduction.html scGPT: Towards Building a Foundation Model for Single-Cell Multi-omics Usin…

2024.9.3

#include <iostream> #include <cstring> using namespace std;class Stack { private:int len;int count 0;int *stack; public:Stack():len(10) //无参构造{stack new int[len];stack[len] {0};}Stack(int len):len(len) //有参构造{stac…

一文搞懂微服务架构之限流

前置知识 限流是通过限制住流量大小来保护系统&#xff0c;能够解决异常突发流量打崩系统的问题。例如常见的某个攻击者在攻击你维护的系统&#xff0c;那么限流就是极大程度上保护住你的系统。 算法 限流算法也可以像负载均衡算法那样&#xff0c;划分成静态算法和动态算法…

【软件测试专栏】测试分类篇

博客主页&#xff1a;Duck Bro 博客主页系列专栏&#xff1a;软件测试专栏关注博主&#xff0c;后期持续更新系列文章如果有错误感谢请大家批评指出&#xff0c;及时修改感谢大家点赞&#x1f44d;收藏⭐评论✍ 测试分类篇 关键词&#xff1a;测试方法的概念、测试类型、先后顺…

花生壳二级域名的绑定测试

1、花生壳客户端的登录 左下角显示的就是你的外部IP。 2、新建映射 点击新建映射&#xff0c;就会进入一个管理页面&#xff0c;如下图&#xff1a; 3、可以通过域名访问网站了 就可以二级域名直接访问&#xff0c;在192.168.1.11:8080 上建立的Tomcat网站了&#xff0c;非常…

uniapp写的一个年月日时分秒时间选择功能

代码: <template><view><picker mode"multiSelector" :value"multiIndex" :range"multiRange" change"onMultiChange"><view class"picker">当前选择&#xff1a;{{ formattedDateTime }}</vie…

各业务领域相关方案

电商 电商系统的简单架构 电商系统的简单架构_电商交易平台 系统架构-CSDN博客 订单系统 美团团购订单系统优化记 vivo 全球商城&#xff1a;订单中心架构设计与实践 库存系统 电商库存系统的防超卖和高并发扣减方案 vivo全球商城&#xff1a;库存系统架构设计与实践 资金…

开篇_____何谓安卓机型“工程固件” 与其他固件的区别 作用

此系列博文将分析安卓系列机型与一些车机 wifi板子等工程固件的一些常识。从早期安卓1.0起始到目前的安卓15&#xff0c;一些厂家发布新机型的常规流程都是从工程机到量产的过程。在其中就需要调试各种参数以便后续的量产参数可以固定到最佳&#xff0c;工程固件由此诞生。 后…

30道python自动化测试面试题与答案汇总

对于机器学习算法工程师而言,Python是不可或缺的语言,它的优美与简洁令人无法自拔,下面这篇文章主要给大家介绍了关于30道python自动化测试面试题与答案汇总的相关资料,需要的朋友可以参考下 1、什么项目适合做自动化测试&#xff1f; 关键字&#xff1a;不变的、重复的、规范…

【Agent】Agent Q: Advanced Reasoning and Learning for Autonomous AI Agents

1、问题背景 传统的训练Agent方法是在静态数据集上进行监督预训练&#xff0c;这种方式对于要求Agent能够自主的在动态环境中可进行复杂决策的能力存在不足。例如&#xff0c;要求Agent在web导航等动态设置中执行复杂决策。 现有的方式是用高质量数据进行微调来增强Agent在动…

专业文件搜索工具 | UltraSearch Pro v4.4.1.1015 绿色特别版

大家好&#xff0c;今天电脑天空给大家推荐一款非常实用的文件搜索软件——UltraSearch Pro。这款软件在文件搜索领域有着出色的表现。 UltraSearch Pro 是一款专业的文件搜索工具&#xff0c;以其快速、全面、精准的搜索能力赢得了用户的一致好评。无论是本地硬盘、网络驱动器…