Giter Club home page Giter Club logo

chat's Introduction

Chat

听说下个学期 Java 的期末作业是写个聊天室, 干脆趁着暑假没事提前干掉吧.
因为之前用 python 写过一次聊天室, 所以基本没啥难度, 主要是熟悉 java 的
语法以及 api.

而且 Java 的 GUI 听说很屎 0.0, 所以就没写了, 之后有空再补上.

已经加上了. 但是 Javafx 实在是跟 QT 没法比啊, 写的蛋疼.
其中有不少浪费资源的举动... 不过也懒的去优化了, 比如 Net 完全可以做成一个队列, 这样就不用新建一个数组来将长度和消息又写一遍来防止多线程发消息时打架.
聊天功能做的差不多了, 基本作为一个聊天室是够用了, 如果要加上注册机制的话还得搞数据库就算了 233

Chat 是聊天室的客户端, ChatServer 是服务端, 均依赖 com.google.code.gsonjavafx
为了 Debug 方便, 在 Client 处有 private boolean DEBUG = true;, 为 true 时, 将会自动随机生成用户名 而不是从 .config.json 里面读取, 方便同时开启多个客户端.

通信协议

采用 tcp 长连接, 主体部分为 json, 两边都方便处理, 在头部有 4 字节为主体的长度, 便于划分
tcp 流边界.

采用 gson 作为 object -> json 以及 json -> object 的转换器, 可以将对象
直接转换为 json 还是很方便的, Message 即是用于传递的对象, 包括主体、
类型、发件人(收件人) 信息.

另外有心跳包机制, 客户端每 3 秒发送一次, 10 秒内一次都没收到服务器会主动断开链接.

服务端

经典的多线程模式, 对每个链接都开启一个线程管理. 内部管理一个 HashMap 作为在线的用户集
方便用户间互相发送消息.

客户端

没啥好说的, 连接、监听服务端就完事了 (逃

chat's People

Contributors

rmb122 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.