突发奇想想要实现一个基于PHP的通讯软件,和传统的基于socket的通讯软件相比
服务端采用ThinkPHP作为MVC架构,对即时消息、联系人管理、登陆注册三大模块进行接口设计,与客户端通讯统一采用json格式输入输出。
消息存储、凭据保存使用Mysql数据库和ThinkPHP缓存进行存储。对于凭据等使用频繁的数据,采用ThinkPHP缓存进行存储;对于消息采用后端MYSQL数据库和客户端本地sqlite数据库进行存储,对于已经读取的消息交由客户端数据库负责存储管理,减轻mysql的查询压力。
1、登录API
URL: http://127.0.0.1/user/login
post: json:{"uid":88888888,"passwd":"admin"}
return: json:{"login":1,"access_token":"test","msg":"登录成功"}
2、注册API
URL: http://127.0.0.1/user/reg/ajax_reg
post: json:{"name":"小王","passwd":"admin"}
return: json:{"reg":1,"uid":88888888,"msg":"success"}
3、获取信息API
URL: http://127.0.0.1/user/info
post: json:{"uid":88888888,"access_token":"test"}
return: json:{"name":"小王","head_img":"http://www.baidu.com/logo.png","time":1568599763}
4、登出
URL: http://127.0.0.1/user/logout
post: json:{"uid":88888888,"access_token":"test"}
return: json:{"error":0 , "msg":"success"}
5、头像图片上传
URL: http://127.0.0.1/user/info/head_img
post: 文件内容
return: json:{"error":0 , "image":"./sss.jpg","msg":""}
5、更新用户信息
URL: http://127.0.0.1/user/info/update
post: json:{"uid":88888888,"access_token":"test","name":"","head_img":""}
return: json:{"error":0 , "msg":"success"}
--------------------------------------------------------------------------------------------------------------------
1、获取联系人列表API
URL: http://127.0.0.1/Contacts/list
post: json:{"uid":88888888,"access_token":"1xxaai5a1enfqnokevy1g6npv4jv4ute"}
return: json:{"num":2,
"msg":"success",
"data":[{"uid":77777777,"name":"小王","head_img":"http://www.baidu.com/logo.png"} , {"uid":88888888,"name":"小张","head_img":"http://www.baidu.com/logo.png"}]}
2、获取联系人信息API
URL: http://127.0.0.1/Contacts/Info
post: json:{"uid":88888888,
"access_token":"1xxaai5a1enfqnokevy1g6npv4jv4ute",
"data":[{"uid":77777777},{"uid":99999999}]}
return: json:{"num":2,
"msg":"success",
"data":[{"uid":77777777,"name":"小王","head_img":"http://www.baidu.com/logo.png"} ,
{"uid":88888888,"name":"小王","head_img":"http://www.baidu.com/logo.png"}]}
3、添加联系人API
URL: http://127.0.0.1/Contacts/IO/addContacts
post: json:{"uid":88888888,
"access_token":"qbjbvz9ir7j4v1ly534e3gc8nqzvb1a3",
"data":[{"uid":66666666}]}
return: json:{"num":2,
"msg":"success"}
4、删除联系人API
URL: http://127.0.0.1/Contacts/IO/delContacts
post: json:{"uid":88888888,
"access_token":"uax1gd95vibsu2m2ku3js78dpzz31jtt",
"data":[{"uid":66666666}]}
return: json:{"num":1,
"msg":"success"}
5、拉取添加自己的联系人列表API
URL: http://127.0.0.1/Contacts/list/add
post: json:{"uid":88888888,"access_token":"1xxaai5a1enfqnokevy1g6npv4jv4ute"}
return: json:{"num":2,
"msg":"success",
"data":[{"uid":77777777,"name":"小王","head_img":"http://www.baidu.com/logo.png"} , {"uid":88888888,"name":"test","head_img":"http://www.baidu.com/logo.png"}]}
6、接收/拒绝添加好友请求API
URL: http://127.0.0.1/Contacts/IO/acceptContacts
post: json:{"uid":88888888,uid_from:777777777,type:1,"access_token":"1xxaai5a1enfqnokevy1g6npv4jv4ute"}
return: json:{"error":0 , "msg":"success"}
--------------------------------------------------------------------------------------------------------------------
1、向服务器发送消息API
URL: http://127.0.0.1/Message/Send
post: json:{"uid":88888888,
"access_token":"uax1gd95vibsu2m2ku3js78dpzz31jtt",
"uid_to":66666666
"data":"test"}
return: json:{"num":1,
"msg":"success"}
2、从服务器获取消息API
URL: http://127.0.0.1/Message/Recv
post: json:{"uid":88888888,
"access_token":"uax1gd95vibsu2m2ku3js78dpzz31jtt"}
return: json:{"num":1,
"msg":"success",
data:[{"uid_from":77777777 , "message":"test" , "time":1532059012}]}
客户端前端UI使用DuiLib界面库,数据库采用sqlite3(加密)库,通讯采用libcurl库,json解析采用cJSON库。实现起来也不难。
PS:没钱找美工,就随便找了点网络图片素材做界面,看看就好,不喜勿喷。
天下没有绝对安全的软件系统,笔者只是根据功能需求实现了一个demo,并使用的式HTTP通讯方式,对于消息通讯时的数据加密,有以下几种方案: