生活随笔
收集整理的這篇文章主要介紹了
集成环信即时通讯(IM)及使用——服务端
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
目的
本篇講述服務端如何集成環信SDK,實現IM系統。
流程
使用
注冊賬號和創建應用
登錄[https://www.easemob.com/]注冊賬號,選擇注冊即時通訊云,注冊完畢后,登錄即可創建應用創建之后可以在控制臺看到你的應用
調用環信接口
??首先我把環信服務端文檔地址發出來,可以看文檔,寫的比較詳細,畢竟別人要賺錢的,寫的差也沒人用了。
??我們可以在控制臺獲取應用的一些信息,替換我的工具類的一些信息就可以使用了。其實服務端調用環信的sdk就是進行網絡請求,進行網絡請求有很多方式,比如http client,URLConnection,okhttp等,這里我使用spring提供的RestTemplate,因為RestTemplate進行rest調用相對來說比較簡單。具體的請求接口請訪問 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
;}}
這里并沒有把所有的接口都寫出來,只是寫了部分的,其它的功能也是類似。
其中封裝了一些實體類
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
;}
建議
在使用環信SDK時,我有些建議:
- 聊天這種即時信息不走后端,前端直接使用環信的sdk,比如Android引入jar包等,當走后端調用時有兩個缺點,1.如果后端宕機了,則不可使用im,不走則可以直接通向環信方服務器。2.網絡延時導致達不到即時通訊的作用。
- 前端進行聊天記錄與好友等信息的緩存,這樣會降低網絡訪問,基本im應用都是這么做的
如何自己搭建一個im服務器
這里我們是使用了第三方的即時通訊,優點就是簡單、方便、成熟可靠。缺點是我們不知道其具體細節,作為一個刨根問底的程序猿肯定是想繼續研究的。可以看我的下一篇Tigase進行即時通訊的實現
總結
以上是生活随笔為你收集整理的集成环信即时通讯(IM)及使用——服务端的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。