Design and Implementation of a Non-Long Connection Communication Software Based on PHP

A sudden whim wants to implement a PHP-based communication software, compared with the traditional socket-based communication software.

  • Disadvantage: Non-real-time communication, passive transfer of data server, higher operating costs.
  • Advantages: Short development cycle and low maintenance cost.

Server:

The server uses ThinkPHP as MVC architecture, designs interfaces for instant messaging, contact management and login registration modules, and uses json format to input and output unified communication with the client.

Message storage and credential storage are stored using Mysql database and ThinkPHP cache. ThinkPHP cache is used to store frequently used data such as credentials; MYSQL database and local sqlite database are used to store messages; and client database is responsible for storage and management of read messages, so as to alleviate the query pressure of mysql.

communication interface

1,Sign in API
URL:		http://127.0.0.1/user/login	
post:		json:{"uid":88888888,"passwd":"admin"}
return:		json:{"login":1,"access_token":"test","msg":"Login successfully"}

2,register API
URL:		http://127.0.0.1/user/reg/ajax_reg	
post:		json:{"name":"Xiao Wang","passwd":"admin"}
return:		json:{"reg":1,"uid":88888888,"msg":"success"}

3,pick up information API
URL:		http://127.0.0.1/user/info
post:		json:{"uid":88888888,"access_token":"test"}
return:		json:{"name":"Xiao Wang","head_img":"http://www.baidu.com/logo.png","time":1568599763}

4,Logout
URL:		http://127.0.0.1/user/logout
post:		json:{"uid":88888888,"access_token":"test"}
return:		json:{"error":0 , "msg":"success"}

5,Head photo upload
URL:		http://127.0.0.1/user/info/head_img
post:		Document content
return:		json:{"error":0 , "image":"./sss.jpg","msg":""}

5,Update user information
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,Get a list of contacts 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":"Xiao Wang","head_img":"http://Www.baidu.com/logo.png"}, {uid": 88888888,""name","Zhang","head_img":"http://www.baidu.com/logo.png"}}
				  
2,Getting contact information 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":"Xiao Wang","head_img":"http://www.baidu.com/logo.png"} ,
							{"uid":88888888,"name":"Xiao Wang","head_img":"http://www.baidu.com/logo.png"}]}
							
3,Add a Contact 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,Delete Contact 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,Pull and add your own contact list 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":"Xiao Wang","head_img":"http://www.baidu.com/logo.png"} , {"uid":88888888,"name":"test","head_img":"http://www.baidu.com/logo.png"}]}

6,Receive/Refuse to add a friend request 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,Send a message to the server 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,Getting messages from the server 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}]}

Client:

The front-end UI of client uses DuiLib interface library, SQLite3 (encrypted) database, libcurl library for communication and cJSON library for json parsing. It's not difficult to achieve.

PS: No money to find artists, just look for some network image material to do the interface, just look at it, do not like spraying.

About security

There is no absolutely safe software system in the world. The author only implements a demo based on functional requirements and uses HTTP communication mode. For data encryption in message communication, there are several schemes as follows:

  • 1. Using HTTPS communication, the author reserved HTTPS communication interface in CHttpClient class.
  • 2. Both client and server adopt local proxy, and use the form of encrypted client to encrypt the data and transmit it on the Internet, referring specifically to the communication mode of shadowsocks local proxy.

 

Keywords: JSON Database MySQL PHP

Added by jasonok6 on Wed, 18 Sep 2019 09:04:21 +0300