GithubHelp home page GithubHelp logo

hexiaodai / im_service Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 2.0 220 KB

Java课程期末作业。功能:登录、注册、添加好友和群、修改个人信息、心跳检测、单聊、群聊、发送图片、视频、表情包、好友上线、下线通知、添加好友、群通知、离线消息、消息漫游

Java 100.00%

im_service's Introduction

Java课程期末作业 & 软件架构期末作业

收获

  • 充分的理解 HTTP、Websocket协议之间的关系和区别
  • 了解 Redis 的基本用法,并且在实际项目中使用 Redis 处理用户的离线消息

项目地址

开发环境

  • jdk 11
  • mysql 8.0
  • redis 6.0.9

构建项目

  • Maven

框架

  • sparkjava

实现功能

  • 权限验证
  • 登录、注册
  • 添加好友、群
  • 修改个人信息
  • 心跳检测
  • 单聊
  • 群聊
  • 发送图片、视频、表情包
  • 好友上线、下线通知
  • 添加好友、群通知
  • 离线消息
  • 消息漫游

websocket

ws://127.0.0.1:9000/chat?email=email&token=token

用户管理 /user

用户登录 POST /user/login

Request

{
    "email": "[email protected]",
    "password": "hjm0819"
}

Response 200

{
    "code": 0,
    "msg": "",
    "data": {
        "id": 29,
        "email": "[email protected]",
        "uname": "原来是小民同学呀",
        "avatar": "https://im-java.oss-cn-shenzhen.aliyuncs.com/images/1618985589853_5726bbf7.jpg",
        "createAt": 1618914288981,
        "deleteTimestamp": 0,
        "token": "6E3E662DFD10ADB61206342FB8922BEA",
        "online": 1   
    }   
}

用户注册 POST /user/register

Request

{
    "email": "[email protected]",
    "password": "test2021",
    "password2": "test2021",
    "uname": "test123"
}

Response 200

{
    "code": 0,
    "data": {
        "createAt": 1618993248102,
        "deleteTimestamp": 0,
        "email": "[email protected]",
        "id": 0,
        "online": 1,
        "token": "548D7B319EF9AB24CD91FBB8031B44E1",
        "uname": "test123"
    },
    "msg": ""
}

搜索好友 GET /user/search_friend

Request

{
    "info": "hjm",
}

Response 200

{
    "code": 0,
    "data": [
        {
            "avatar": "https://im-java.oss-cn-shenzhen.aliyuncs.com/images/1618985589853_5726bbf7.jpg",
            "createAt": 1618914288981,
            "deleteTimestamp": 0,
            "email": "[email protected]",
            "id": 29,
            "online": 0,
            "uname": "原来是小民同学呀",
        }
    ],
    "msg": ""
}

修改用户信息 PUT /user/edit_user

Request

{
    "avatar": "https://im-java.oss-cn-shenzhen.aliyuncs.com/images/1618985589853_5726bbf7.jpg",
    "uname": "test456"
}

Response 200

{
    "code": 0,
    "data": {
        "avatar": "https://im-java.oss-cn-shenzhen.aliyuncs.com/images/1618985589853_5726bbf7.jpg",
        "createAt": 1618993248102,
        "deleteTimestamp": 0,
        "email": "[email protected]",
        "id": 0,
        "online": 1,
        "token": "548D7B319EF9AB24CD91FBB8031B44E1",
        "uname": "test456",
    },
    "msg": ""
}

获取用户信息(包括在线状态) GET /user/user_info

Request

{
    "email": "[email protected]"
}

Response 200

{
    "code": 0,
    "data": {
        "avatar": "https://im-java.oss-cn-shenzhen.aliyuncs.com/images/1618985589853_5726bbf7.jpg",
        "createAt": 1618847453422,
        "deleteTimestamp": 0,
        "email": "[email protected]",
        "id": 26,
        "uname": "何同学",
        "online": 0
    },
    "msg": ""
}

好友关系管理(好友,群) /contact

添加(好友、群)POST /contact/add_friend

Request

{
    "cmd": 15,
    "dstObj": "[email protected]",
    "msgType": 6,
    "userEmail": "[email protected]"
}

Response 200

{
    "cmd": 15,
    "content": "[email protected] 添加 [email protected]为好友",
    "createAt": 1618994048486,
    "deleteTimestamp": 0,
    "dstObj": "[email protected]",
    "msgType": 6,
    "userEmail": "[email protected]"
}

获取好友列表 GET /contact/friend_list

Request 登录成功

Response 200

{
    "code": 0,
    "data": {
        {
            "avatar": "https://im-java.oss-cn-shenzhen.aliyuncs.com/images/1618985589853_5726bbf7.jpg",
            "createAt": 1618914288981,
            "deleteTimestamp": 0,
            "email": "[email protected]",
            "id": 29,
            "online": 1,
            "uname": "原来是小民同学呀"
        }
    },
    "msg": ""
}

消息管理 /msg

获取好友消息列表 GET /msg/friend_msg_list

Request 登录成功

Response 200

{
    "code": 0,
    "data": {
        {
            "cmd": 10,
            "content": "@(呵呵)",
            "createAt": 1618994297941,
            "deleteTimestamp": 0,
            "dstObj": "[email protected]",
            "id": 1674,
            "msgType": 1,
            "userEmail": "[email protected]"
        },
        {
            "cmd": 10,
            "content": "this is a test",
            "createAt": 1618994306254,
            "deleteTimestamp": 0,
            "dstObj": "[email protected]",
            "id": 1675,
            "msgType": 1,
            "userEmail": "[email protected]"
        }
    },
    "msg": ""
}

获取群消息列表 GET /msg/community_msg_list

Request

{
    "dstObj": 1000
}

Response 200

{
    "code": 0,
    "data": {
        {
            "cmd": 11,
            "content": "@(惊恐)",
            "createAt": 1618994465192,
            "deleteTimestamp": 0,
            "dstObj": "1000",
            "id": 1676,
            "msgType": 1,
            "userEmail": "[email protected]"
        },
        {
            "cmd": 11,
            "content": "hello world!",
            "createAt": 1618994471825,
            "deleteTimestamp": 0,
            "dstObj": "1000",
            "id": 1677,
            "msgType": 1,
            "userEmail": "[email protected]"
        }
    },
    "msg": ""
}

群管理 /community

搜索群 GET /community/search_community

Request

{
    "info": "软件架构"
}

Response 200

{
    "code": 0,
    "data": [
        {
            "createAt": 1618847453422,
            "deleteTimestamp": 0,
            "icon": "https://im-java.oss-cn-shenzhen.aliyuncs.com/images/1618985589853_5726bbf7.jpg",
            "id": 1000,
            "name": "软件架构课程交流群",
            "ownerEmail": "[email protected]"
        }
    ],
    "msg": ""
}

获取群列表 GET /community/search_community

Request 登录成功

Response 200

{
    "code": 0,
    "data": [
        {
            "createAt": 1618847453422,
            "deleteTimestamp": 0,
            "icon": "https://im-java.oss-cn-shenzhen.aliyuncs.com/images/1618985589853_5726bbf7.jpg",
            "id": 1000,
            "name": "软件架构课程交流群",
            "ownerEmail": "[email protected]"
        }   
    ],
    "msg": ""
}

功能截图

  • 权限验证

        // api白名单
        public static String[] allowPath = new String[] {"user/login", "user/register"};
    
        public static void tokenAuthMiddleware(Request req, Response res) {
            if (Arrays.asList(TokenAuth.allowPath).contains(req.splat()[0])) return;
    
            // 得到header或query上的email和token
            var email = getArg(req, "email");
            var token = getArg(req, "token");
    
            // 若header和query都不存在email和token,直接401
            if (token == null || email == null) halt(401, "未经授权的请求");
            var redis = utils.Redis.getJedis();
            // 通过email拿到redis用户信息
            var json = redis.get(email);
            redis.close();
            if (json == null) halt(401, "未经授权的请求");
            var user = (new Gson().fromJson(json, UserModel.class));
            // 验证token是否合法
            if (!user.getToken().equals(token)) halt(401, "未经授权的请求");
        }
  • 登录、注册 输入图片说明

  • 添加好友、群 输入图片说明

  • 修改个人信息 输入图片说明

  • 心跳检测 输入图片说明

  • 单聊 输入图片说明

  • 群聊 输入图片说明

  • 发送图片、视频、表情包 输入图片说明 输入图片说明

  • 好友上线、下线通知 输入图片说明

  • 添加好友、群通知 输入图片说明

  • 离线消息 输入图片说明

  • 消息漫游 输入图片说明

im_service's People

Contributors

hexiaodai avatar

Stargazers

晗宝儿2222 avatar  avatar  avatar CathyLogs avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.