生活随笔
收集整理的這篇文章主要介紹了
集成环信即时通讯(IM)及使用——服务端
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 目的
- 流程
- 使用
- 注冊(cè)賬號(hào)和創(chuàng)建應(yīng)用
- 調(diào)用環(huán)信接口
- 建議
- 如何自己搭建一個(gè)im服務(wù)器
目的
本篇講述服務(wù)端如何集成環(huán)信SDK,實(shí)現(xiàn)IM系統(tǒng)。
流程
- 注冊(cè)賬號(hào)
- 創(chuàng)建應(yīng)用
- 調(diào)用環(huán)信接口
使用
注冊(cè)賬號(hào)和創(chuàng)建應(yīng)用
登錄[https://www.easemob.com/]注冊(cè)賬號(hào),選擇注冊(cè)即時(shí)通訊云,注冊(cè)完畢后,登錄即可創(chuàng)建應(yīng)用創(chuàng)建之后可以在控制臺(tái)看到你的應(yīng)用
調(diào)用環(huán)信接口
??首先我把環(huán)信服務(wù)端文檔地址發(fā)出來(lái),可以看文檔,寫(xiě)的比較詳細(xì),畢竟別人要賺錢(qián)的,寫(xiě)的差也沒(méi)人用了。
??我們可以在控制臺(tái)獲取應(yīng)用的一些信息,替換我的工具類(lèi)的一些信息就可以使用了。其實(shí)服務(wù)端調(diào)用環(huán)信的sdk就是進(jìn)行網(wǎng)絡(luò)請(qǐng)求,進(jìn)行網(wǎng)絡(luò)請(qǐng)求有很多方式,比如http client,URLConnection,okhttp等,這里我使用spring提供的RestTemplate,因?yàn)镽estTemplate進(jìn)行rest調(diào)用相對(duì)來(lái)說(shuō)比較簡(jiǎn)單。具體的請(qǐng)求接口請(qǐng)?jiān)L問(wèn) http://api-docs.easemob.com/
public class HXUtil {private static RestTemplate restTemplate
= new RestTemplate();private static final String ORG_NAME
= "1166170913115446";private static final String CLIENT_ID
= "YXA6MvScoJhlEeeE2SOuxTV6gQ";private static final String CLIENT_SECRET
= "YXA6EUcZtNNyxqCXRN8pIxZuHBXQj3Y";private static final String APP_NAME
= "im";private static final String URL_PREFIX
= "http://a1.easemob.com/" + ORG_NAME
+ "/" + APP_NAME
+ "/";public enum HXMessageType
{txt
,img
,loc
,audio
,video
,file
}public static Token
getToken() {try {JSONObject body
= new JSONObject();body
.put("grant_type", "client_credentials");body
.put("client_id", CLIENT_ID
);body
.put("client_secret", CLIENT_SECRET
);HttpEntity httpEntity
= new HttpEntity(body
.toString(), null
);ResponseEntity
<Token> tokenResponseEntity
= restTemplate
.postForEntity(URL_PREFIX
+ "token", httpEntity
, Token
.class);return tokenResponseEntity
.getBody();} catch (RestClientException e
) {e
.printStackTrace();return null
;}}public static boolean addUser(String username
, String password
) {try {JSONArray body
= new JSONArray();JSONObject jsonObject
= new JSONObject();jsonObject
.put("username", username
);jsonObject
.put("password", password
);body
.add(jsonObject
);HttpEntity httpEntity
= new HttpEntity(body
.toString(), null
);ResponseEntity responseEntity
= restTemplate
.postForEntity(URL_PREFIX
+ "users", httpEntity
, null
);return responseEntity
.getStatusCodeValue() == 200;} catch (RestClientException e
) {e
.printStackTrace();return false;}}public static boolean updatePassword(String username
, String newpassword
) {try {JSONObject body
= new JSONObject();body
.put("newpassword", newpassword
);HttpHeaders headers
= new HttpHeaders();headers
.add("Authorization", "Bearer " + getToken().getAccess_token());HttpEntity httpEntity
= new HttpEntity(body
.toString(), headers
);ResponseEntity responseEntity
= restTemplate
.postForEntity(URL_PREFIX
+ "users/{username}/password", httpEntity
, null
, username
);System
.out
.println(responseEntity
.getStatusCodeValue());return responseEntity
.getStatusCodeValue() == 200;} catch (RestClientException e
) {e
.printStackTrace();return false;}}public static boolean deleteUser(String username
) {try {HttpEntity httpEntity
= new HttpEntity(null
, getHttpHeaders(MediaType
.TEXT_PLAIN
, MediaType
.APPLICATION_JSON
));ResponseEntity
<HXUser> responseEntity
= restTemplate
.exchange(URL_PREFIX
+ "users/{username}", HttpMethod
.DELETE
, httpEntity
, HXUser
.class, username
);System
.out
.println(responseEntity
.getStatusCodeValue());return responseEntity
.getStatusCodeValue() == 200;} catch (RestClientException e
) {e
.printStackTrace();return false;}}public static boolean addFriend(String ownerUsername
, String friendName
) {try {HttpEntity httpEntity
= new HttpEntity(null
, getHttpHeaders(MediaType
.APPLICATION_JSON
, MediaType
.APPLICATION_JSON
));ResponseEntity responseEntity
= restTemplate
.postForEntity(URL_PREFIX
+ "users/{owner_username}/contacts/users/{friend_username}", httpEntity
, HXUser
.class, ownerUsername
, friendName
);System
.out
.println(responseEntity
.getStatusCodeValue());return responseEntity
.getStatusCodeValue() == 200;} catch (RestClientException e
) {e
.printStackTrace();return false;}}public static boolean deleteFriend(String ownerUsername
, String friendName
) {try {HttpEntity httpEntity
= new HttpEntity(null
, getHttpHeaders(MediaType
.APPLICATION_JSON
, MediaType
.APPLICATION_JSON
));ResponseEntity responseEntity
= restTemplate
.exchange(URL_PREFIX
+ "users/{owner_username}/contacts/users/{friend_username}", HttpMethod
.DELETE
, httpEntity
, HXUser
.class, ownerUsername
, friendName
);System
.out
.println(responseEntity
.getStatusCodeValue());return responseEntity
.getStatusCodeValue() == 200;} catch (RestClientException e
) {e
.printStackTrace();return false;}}public static boolean sendToUser(String sendUser
, String targetUser
, String msg
) {try {JSONObject body
= new JSONObject();body
.put("target_type", "users");JSONArray targetUserjson
= new JSONArray();targetUserjson
.add(targetUser
);body
.put("target", targetUserjson
);JSONObject msgJson
= new JSONObject();msgJson
.put("type", HXMessageType
.txt
.name());msgJson
.put("msg", msg
);body
.put("msg", msgJson
);body
.put("from", sendUser
);HttpEntity httpEntity
= new HttpEntity(body
, getHttpHeaders(MediaType
.APPLICATION_JSON
, MediaType
.APPLICATION_JSON
));ResponseEntity responseEntity
= restTemplate
.postForEntity(URL_PREFIX
+ "messages", httpEntity
, null
);System
.out
.println(responseEntity
.getStatusCodeValue());return responseEntity
.getStatusCodeValue() == 200;} catch (RestClientException e
) {e
.printStackTrace();return false;}}private static HttpHeaders
getHttpHeaders(MediaType contentType
, MediaType
... accept
) {HttpHeaders headers
= new HttpHeaders();headers
.add("Authorization", "Bearer " + getToken().getAccess_token());headers
.setContentType(contentType
!= null
? contentType
: MediaType
.APPLICATION_JSON
);headers
.setAccept(Arrays
.asList((accept
!= null
&& accept
.length
> 0) ? accept
: new MediaType[]{MediaType
.APPLICATION_JSON
}));return headers
;}}
這里并沒(méi)有把所有的接口都寫(xiě)出來(lái),只是寫(xiě)了部分的,其它的功能也是類(lèi)似。
其中封裝了一些實(shí)體類(lèi)
HXUser
public class HXUser {private String uuid
; private String type
; private Long created
;private Long modified
;private String username
; private String nickName
; private boolean activated
; public String
getUuid() {return uuid
;}public void setUuid(String uuid
) {this.uuid
= uuid
;}
}
Token
public class Token {private String access_token
; private String expires_in
; private String application
; public String
getAccess_token() {return access_token
;}public void setAccess_token(String access_token
) {this.access_token
= access_token
;}public String
getExpires_in() {return expires_in
;}public void setExpires_in(String expires_in
) {this.expires_in
= expires_in
;}public String
getApplication() {return application
;}public void setApplication(String application
) {this.application
= application
;}
建議
在使用環(huán)信SDK時(shí),我有些建議:
- 聊天這種即時(shí)信息不走后端,前端直接使用環(huán)信的sdk,比如Android引入jar包等,當(dāng)走后端調(diào)用時(shí)有兩個(gè)缺點(diǎn),1.如果后端宕機(jī)了,則不可使用im,不走則可以直接通向環(huán)信方服務(wù)器。2.網(wǎng)絡(luò)延時(shí)導(dǎo)致達(dá)不到即時(shí)通訊的作用。
- 前端進(jìn)行聊天記錄與好友等信息的緩存,這樣會(huì)降低網(wǎng)絡(luò)訪問(wèn),基本im應(yīng)用都是這么做的
如何自己搭建一個(gè)im服務(wù)器
這里我們是使用了第三方的即時(shí)通訊,優(yōu)點(diǎn)就是簡(jiǎn)單、方便、成熟可靠。缺點(diǎn)是我們不知道其具體細(xì)節(jié),作為一個(gè)刨根問(wèn)底的程序猿肯定是想繼續(xù)研究的。可以看我的下一篇Tigase進(jìn)行即時(shí)通訊的實(shí)現(xiàn)
總結(jié)
以上是生活随笔為你收集整理的集成环信即时通讯(IM)及使用——服务端的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。