Giter VIP home page Giter VIP logo

javaclassdesign_litemessage's Introduction

基本思路

分为Server端和Client端 两程序相互独立


对象流传输

建立一个DataPackage类,使用socket传送的内容为DataPackage对象

数据包可用于登录发送消息判断在线情况

对象流注意点

在网络通讯中,主机与客户端若使用ObjectInputStreamObjectOutputStream建立对象通时,有时会发生线程阻塞问题。

这是因为当从InputStream创建一个ObjectInputStream时,需要从流中读入并验证一个Header,这时如果对方的ObjectOutputStream没有写入一个HeaderObjectInputStream的构造函数便会阻塞(block)

解决这个问题的方法是调整ObjectInputStreamObjectOutputStream的声明顺序。比如:主机端先建立ObjectInputStream后建立ObjectOutputStream,则对应地客户端要先建立ObjectOutputStream后建立ObjectInputStream

Client 登录

  1. Client发送一个LOGIN类型的数据包,并用setLogin()函数设定用于验证的用户名和密码
  2. Sever收到后,用getUserID()getUserPassword(),并调用公共类CheckLogin检查用户信息是否合法
  3. 返回检验结果,setStat()函数设定数据包作用结果。
  4. 若结果通过将对应userIDsocket加入到matchList中,若不通过则无其他操作
  5. 最后将此DataPackage包传回Client,Client通过getStat()做出登录成功或失败的对应操作

ps: 可能需要添加若登录成功,向所有在线用户广播此用户上线消息,以实现在线用户展示功能。实现方式大致构想一下:在Client端不单独开判断分支,而是当数据包的类型为LOGINpackageStatLOGIN_SUCCESSFUL、最后userID和本机的不同时,那么判断有新用户上线,并且对其进行展示

Client 发送消息

经过反复思考,还是决定把向离线用户发送消息的功能取消掉。感觉实现略复杂,且不实用

  1. Client发送一个SEND_MESSAGE类型的数据包,并调用sendMessage设置接收者和消息内容
  2. Server收到数据包后,先提取接收者的userID,再到matchList中进行匹配得到对应的socket,向此socket写入这个数据包,由于接收端有一个线程在不断从对象流中提取数据包,所以写入后不用担心接收者接受数据的问题
  3. 成功将数据包传送到接受者后,在对此数据包setStat(),并传回发送者,便于发送者显示消息记录等

离线

//TODO

javaclassdesign_litemessage's People

Watchers

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