首页 前端知识 用 websocket、JQuery开发仿微信聊天

用 websocket、JQuery开发仿微信聊天

2024-06-08 09:06:27 前端知识 前端哥 573 770 我要收藏

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%以上前端开发知识点,真正体系化!

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

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

        最后

        文章到这里就结束了,如果觉得对你有帮助可以点个赞哦,如果有需要前端校招面试题PDF完整版的朋友可以点击这里即可获取,包括答案解析。

        {

        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-0ylz3VAB-1713372240791)]

        [外链图片转存中…(img-xf1kOXIK-1713372240791)]

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

        [外链图片转存中…(img-5lBNoPMp-1713372240792)]

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

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

        最后

        文章到这里就结束了,如果觉得对你有帮助可以点个赞哦,如果有需要前端校招面试题PDF完整版的朋友可以点击这里即可获取,包括答案解析。

        [外链图片转存中…(img-l42qwDeb-1713372240792)]

      转载请注明出处或者链接地址:https://www.qianduange.cn//article/11441.html
      标签
      websocket
      评论
      发布的文章

      echarts柱状图自动轮播

      2024-06-16 01:06:46

      Echarts--markPoint属性

      2024-06-16 01:06:45

      大家推荐的文章
      会员中心 联系我 留言建议 回顶部
      复制成功!