用 websocket、JQuery开发仿微信聊天

npm install express --save

服务端代码(app.js)

//创建了http服务器

var app = require(‘express’)();

var server = require(‘http’).Server(app);

var io = require(‘socket.io’)(server);

server.listen(3000);

app.get(‘/’, function (req, res) {

res.sendFile(__dirname + ‘/online-chat.html’);

});

io.on(‘connection’, function (socket) {

socket.emit(‘send’, { name: ‘haha’ });

});

客户端代码(inline-chat)

在线聊天室 //必须引入

在这里插入图片描述

接收到了服务端 发过来的 消息,说明配置成功了。

3.静态页面搭建

==========================================================================

创建一个public文件夹,把所有静态资源都放进public文件夹中,如图所示


在这里插入图片描述

由于要使用public的静态资源,必须使用express 中的语句


// express使用静态资源

app.use(require(‘express’).static(‘public’));

此时index .html


聊天室

用户登录

用户名

选择头像

    • 登录

      清流

      用户列表

        聊天室(99)

        按下Ctrl+Enter发送

        发送

        css


        • {

        margin: 0;

        padding: 0;

        list-style: none;

        }

        html,

        body {

        height: 100%;

        }

        body {

        background: url(‘…/images/bg.jpg’) no-repeat center center;

        background-size: cover;

        }

        .container {

        max-width: 1000px;

        min-width: 800px;

        height: 100%;

        margin: 0 auto;

        background-color: pink;

        }

        .user-list {

        width: 280px;

        height: 100%;

        float: left;

        position: relative;

        background-color: #2e3238;

        }

        .box {

        overflow: hidden;

        height: 100%;

        background-color: #eee;

        position: relative;

        }

        .box-hd {

        text-align: center;

        position: absolute;

        top: 0;

        left: 0;

        width: 100%;

        line-height: 30px;

        }

        .box-hd h3 {

        font-size: 18px;

        font-weight: 400;

        padding: 10px 0;

        margin: 0 20px;

        border-bottom: 1px solid #ccc;

        z-index: 999;

        box-sizing: border-box;

        }

        .message-box {

        overflow: hidden;

        }

        .box-bd {

        position: absolute;

        width: 100%;

        bottom: 180px;

        top: 51px;

        overflow-y: auto;

        overflow-x: hidden;

        }

        .system {

        overflow: hidden;

        }

        .message_system {

        text-align: center;

        margin: 10px auto;

        max-width: 50%;

        }

        .message_system .content {

        display: inline-block;

        font-size: 12px;

        padding: 1px 18px;

        color: #b2b2b2;

        border-radius: 2px;

        }

        .other {

        margin-bottom: 16px;

        float: left;

        width: 100%;

        padding-left: 20px;

        box-sizing: border-box;

        }

        .my {

        margin-bottom: 16px;

        float: right;

        width: 100%;

        text-align: right;

        padding-right: 20px;

        box-sizing: border-box;

        }

        .my.message .avatar {

        float: right;

        }

        .message .content {

        overflow: hidden;

        }

        .message .content .nickname {

        font-weight: 400;

        padding-left: 10px;

        font-size: 12px;

        height: 22px;

        line-height: 24px;

        color: #4f4f4f;

        width: 350px;

        overflow: hidden;

        text-overflow: ellipsis;

        white-space: nowrap;

        word-wrap: normal;

        }

        .message .avatar {

        width: 40px;

        height: 40px;

        border-radius: 2px;

        float: left;

        cursor: pointer;

        }

        .my.message .bubble {

        background-color: #b2e281;

        }

        .message .bubble {

        max-width: 500px;

        min-height: 1em;

        display: inline-block;

        vertical-align: top;

        position: relative;

        text-align: left;

        font-size: 14px;

        border-radius: 3px;

        margin: 0 10px;

        background-color: #fff;

        }

        .message .bubble img {

        display: inline-block;

        cursor: pointer;

        max-width: 350px;

        max-height: 240px;

        }

        .other .bubble:before {

        position: absolute;

        top: 14px;

        left: -10px;

        border: 6px solid transparent;

        content: ’ ';

        border-right-color: #fff;

        border-right-width: 4px;

        }

        .my .bubble:before {

        position: absolute;

        top: 14px;

        right: -10px;

        border: 6px solid transparent;

        content: ’ ';

        border-left-color: #b2e281;

        border-left-width: 4px;

        }

        .bubble_cont {

        word-wrap: break-word;

        word-break: break-all;

        min-height: 25px;

        padding: 9px 13px;

        }

        .box-ft {

        border-top: 1px solid #ccc;

        position: absolute;

        height: 180px;

        bottom: 0;

        right: 0;

        left: 0;

        }

        .box-ft .toolbar {

        height: 30px;

        padding: 5px 20px;

        }

        .box-ft .toolbar .face {

        display: inline-block;

        vertical-align: middle;

        width: 30px;

        height: 30px;

        background: url(‘…/images/wechat-sprit.png’) no-repeat -404px -398px;

        }

        .box-ft .toolbar .screen-cut {

        display: inline-block;

        vertical-align: middle;

        width: 30px;

        height: 30px;

        background: url(‘…/images/wechat-sprit.png’) no-repeat -30px -432px;

        }

        .box-ft .toolbar .file label {

        opacity: 0;

        width: 100%;

        height: 100%;

        display: block;

        cursor: pointer;

        background: rgb(255, 255, 255);

        }

        .box-ft .toolbar .file {

        display: inline-block;

        vertical-align: middle;

        width: 30px;

        height: 30px;

        background: url(‘…/images/wechat-sprit.png’) no-repeat -120px -432px;

        }

        .box-ft .content {

        height: 90px;

        overflow-x: hidden;

        padding: 0px 20px;

        }

        .box-ft .content .text {

        resize: none;

        border: none;

        outline: none;

        width: 100%;

        height: 84px;

        font-size: 16px;

        background-color: #eee;

        }

        .box-ft .action {

        text-align: right;

        margin-top: 5px;

        padding-right: 20px;

        }

        .box-ft .action .desc {

        color: #888;

        font-size: 12px;

        margin-left: 10px;

        margin-right: 7px;

        }

        .btn-send {

        display: inline-block;

        border: 1px solid #c1c1c1;

        text-decoration: none;

        background-color: #fff;

        color: #222;

        border-radius: 4px;

        padding: 3px 30px;

        font-size: 14px;

        }

        .btn-send:hover {

        background-color: #d8d8d8;

        }

        .header {

        padding: 18px;

        position: relative;

        }

        .header .avatar {

        display: table-cell;

        vertical-align: middle;

        word-wrap: break-word;

        word-break: break-all;

        white-space: nowrap;

        padding-right: 10.625px;

        }

        .header .avatar .img {

        width: 40px;

        height: 40px;

        border-radius: 2px;

        display: block;

        cursor: pointer;

        }

        .header .info {

        display: table-cell;

        vertical-align: middle;

        word-wrap: break-word;

        word-break: break-all;

        width: 2000px;

        }

        .header .info h3 {

        display: inline-block;

        font-weight: 400;

        width: 156px;

        overflow: hidden;

        text-overflow: ellipsis;

        white-space: nowrap;

        word-wrap: normal;

        color: #fff;

        font-size: 18px;

        vertical-align: top;

        line-height: 31px;

        text-decoration: none;

        }

        .title {

        padding: 13px 18px 11px;

        border-bottom: 1px solid #24272c;

        border-top: 1px solid #24272c;

        color: #fff;

        }

        .title h3 {

        font-weight: 400;

        font-size: 18px;

        }

        .user {

        overflow: hidden;

        padding: 12px 18px 11px;

        border-bottom: 1px solid #292c33;

        cursor: pointer;

        position: relative;

        }

        .user .avatar {

        float: left;

        margin-right: 10px;

        position: relative;

        }

        .user .avatar img {

        display: block;

        width: 40px;

        height: 40px;

        border-radius: 2px;

        }

        .user .name {

        color: #fff;

        overflow: hidden;

        line-height: 36px;

        }

        .login_box {

        position: absolute;

        top: 50%;

        left: 50%;

        width: 380px;

        height: 380px;

        transform: translate(-50%, -50%);

        border-radius: 4px;

        background-color: #fff;

        box-shadow: #999 0 2px 10px;

        }

        .login_box h3 {

        text-align: center;

        color: #333;

        font-size: 24px;

        font-weight: 400;

        line-height: 100px;

        }

        .login_box input {

        width: 300px;

        height: 30px;

        line-height: 30px;

        margin: 0 auto;

        padding: 0;

        display: block;

        outline: none;

        margin-bottom: 5px;

        }

        .weui-btn {

        position: relative;

        display: block;

        width: 300px;

        margin: 0 auto;

        box-sizing: border-box;

        font-size: 14px;

        text-align: center;

        text-decoration: none;

        color: #ffffff;

        line-height: 2.55555556;

        border-radius: 5px;

        -webkit-tap-highlight-color: rgba(0, 0, 0, 0);

        overflow: hidden;

        background-color: #1aad19;

        border: none;

        cursor: pointer;

        margin-top: 5px;

        }

        .login_box ul {

        overflow: hidden;

        width: 280px;

        margin: 0 auto;

        border: 1px solid #ccc;

        padding: 0 10px;

        }

        .login_box li {

        float: left;

        width: 44px;

        height: 44px;

        border: 2px solid transparent;

        margin: 0 4px;

        cursor: pointer;

        }

        .login_box li.now {

        border-color: #1aad19;

        }

        .login_box img {

        width: 40px;

        height: 40px;

        display: block;

        }

        .login_box p {

        height: 30px;

        line-height: 30px;

        padding-left: 38px;

        }

        ::-webkit-scrollbar-track-piece {

        background-color: #f8f8f8;

        }

        ::-webkit-scrollbar {

        width: 6px;

        height: 6px;

        border-radius: 3px;

        }

        ::-webkit-scrollbar-thumb {

        background-color: #ccc;

        background-clip: padding-box;

        min-height: 28px;

        }

        ::-webkit-scrollbar-thumb:hover {

        background-color: #666;

        }

        4.实现简单登陆

        ==========================================================================

        index.js

        /*

        1. 连接socketio服务

        */

        var socket = io(‘http://localhost:3000’)

        var username, avatar

        /*

        1. 登录功能

        */

        $(‘#login_avatar li’).on(‘click’, function() {

        $(this)

        .addClass(‘now’)

        .siblings()

        .removeClass(‘now’)

        })

        // 点击按钮,登录

        $(‘#loginBtn’).on(‘click’, function() {

        // 获取用户名

        var username = $(‘#username’)

        .val()

        .trim()

        if (!username) {

        alert(‘请输入用户名’)

        return

        }

        // 获取选择的头像

        var avatar = $(‘#login_avatar li.now img’).attr(‘src’)

        // 需要告诉socket io服务,登录

        socket.emit(‘login’, {

        username: username,

        avatar: avatar

        })

        })

        // 监听登录失败的请求

        socket.on(‘loginError’, data => {

        alert(‘用户名已经存在’)

        })

        // 监听登录成功的请求

        socket.on(‘loginSuccess’, data => {

        // 需要显示聊天窗口

        // 隐藏登录窗口

        $(‘.login_box’).fadeOut()

        $(‘.container’).fadeIn()

        // 设置个人信息

        console.log(data)

        $(‘.avatar_url’).attr(‘src’, data.avatar)

        $(‘.user-list .username’).text(data.username)

        username = data.username

        avatar = data.avatar

        })

        在这里插入图片描述

        app.js中


        自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

        深知大多数前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

        因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

        img

        既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

        由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

        如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:前端)

        最后

        正值金三银四招聘旺季,很多小伙伴都询问我有没有前端方面的面试题,特地整理出来赠送给大家!

        资料领取方式:点击这里前往获取

        // 监听登录失败的请求

        socket.on(‘loginError’, data => {

        alert(‘用户名已经存在’)

        })

        // 监听登录成功的请求

        socket.on(‘loginSuccess’, data => {

        // 需要显示聊天窗口

        // 隐藏登录窗口

        $(‘.login_box’).fadeOut()

        $(‘.container’).fadeIn()

        // 设置个人信息

        console.log(data)

        $(‘.avatar_url’).attr(‘src’, data.avatar)

        $(‘.user-list .username’).text(data.username)

        username = data.username

        avatar = data.avatar

        })

        在这里插入图片描述

        app.js中


        自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

        深知大多数前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

        因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

        [外链图片转存中…(img-qQ7Stzpj-1712817177374)]

        [外链图片转存中…(img-0snYJKFm-1712817177375)]

        既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

        [外链图片转存中…(img-4iOfmzL6-1712817177375)]

        由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

        如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:前端)

        最后

        正值金三银四招聘旺季,很多小伙伴都询问我有没有前端方面的面试题,特地整理出来赠送给大家!

        资料领取方式:点击这里前往获取

        前端资料图.PNG

      要实现类似微信聊天的功能,你可以使用WebSocket来进行实时通信。WebSocket是一种在客户端和服务器之间双向通信的协议。在前端中,你可以使用TextWebSocketFrame来处理WebSocket的文本消息。在后端,你可以创建一个WebSocket处理器来处理WebSocket的消息。下面是一个示例代码: 在后端,你可以创建一个名为ChatHandler的类,继承自SimpleChannelInboundHandler。在这个类中,你可以重写channelRead0方法来处理接收到的WebSocket消息。你可以使用TextWebSocketFrame来获取文本消息的内容,然后根据业务逻辑进行处理。 在前端,你可以创建一个名为socket.js的文件,并在其中定义一个WebSocket的实例。你可以使用WebSocket的init方法来初始化WebSocket连接。在init方法中,你可以实例化WebSocket对象,并设置onmessage、onerror和onclose的回调函数。在onmessage回调函数中,你可以处理接收到的消息。在send方法中,你可以发送消息给服务器。 这样,你就可以在前端和后端之间建立起一个双向通信的WebSocket连接,实现类似微信聊天的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [websocket实现仿微信聊天功能](https://blog.csdn.net/qq_42332821/article/details/99438673)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [webSocket仿微信聊天,vue+node.js](https://blog.csdn.net/weixin_45389051/article/details/108201288)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
      评论
      添加红包

      请填写红包祝福语或标题

      红包个数最小为10个

      红包金额最低5元

      当前余额3.43前往充值 >
      需支付:10.00
      成就一亿技术人!
      领取后你会自动成为博主和红包主的粉丝 规则
      hope_wisdom
      发出的红包
      实付
      使用余额支付
      点击重新获取
      扫码支付
      钱包余额 0

      抵扣说明:

      1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
      2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

      余额充值