关于使用环信web-im实现登录(直接登录)

1,去环信官网下载个WEB-IM

我用的是V1.4.11版本,当时还没有出12嘛,所以12也没研究,不过应该变动不大。

2, 将项目解压到你的web项目中的WebRoot下,更换掉appKey即可。(在web-im/demo/javascript/dist/webim.config.js里)

3,在web-im下创建一个html文件。

4,引入标签





5,写一个ajax来实现登录

$(function () {
$.ajax({  
    type:"post",  
    url:getRootPath()+"/hx/toHx",  //建议使用绝对路径
    data:{ 
    },  
    cache: false,  
    dataType:"json",  
    success:function(data){  
    var username= data.username;var password= data.password;
    var conn = new WebIM.connection({
       https: WebIM.config.https,
       url: WebIM.config.xmppURL,
       isAutoLogin: WebIM.config.isAutoLogin,
       isMultiLoginSessions: WebIM.config.isMultiLoginSessions
    });
    var encryptUsername = btoa(username);
    encryptUsername = encryptUsername.replace(/=*$/g, "");
    var options = {
       apiUrl: WebIM.config.apiURL,
       user: username,
       pwd: password,
       appKey: WebIM.config.appkey,
       success: function (token) {
           var token = token.access_token;
           WebIM.utils.setCookie('webim_' + encryptUsername, token, 1);
          location.href=getRootPath()+"/web-im/index.html#username="+encryptUsername;
       },
       error: function(){
       }
    };
    conn.open(options);
   }  
});  

});

这样,跳转到这个文件路径的时候就会直接登录了,后台只需把用户名和密码参数传过来即可。


下面加一个环信注册的方法:

/**
* 授权注册
* @return
* @throws HttpException
* @throws IOException
*/
public static String addUsers(String username,String password) throws HttpException, IOException{
client = new HttpClient();
PostMethod postMethod = new PostMethod("https://a1.easemob.com/"+org_name+"/"+app_name+"/users");
postMethod.getParams().setParameter(HttpClientParams.HTTP_CONTENT_CHARSET,"UTF-8");
postMethod.getParams().setBooleanParameter(HttpClientParams.USE_EXPECT_CONTINUE,false);
postMethod.getParams().setParameter(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,  new DefaultHttpMethodRetryHandler());
//设置requestHeader
postMethod.setRequestHeader("Content-Type", "application/json");
postMethod.setRequestHeader("Authorization", getOpenCode());
// 填入各个表单域的值  
JSONObject jsonObject = new JSONObject();
jsonObject.put("username", username);
jsonObject.put("password", password);
String transJson = jsonObject.toString();
RequestEntity se = new StringRequestEntity(transJson, "application/json", "UTF-8");
postMethod.setRequestEntity(se);
client.executeMethod(postMethod);
//接收返回参数
StringBuffer contentBuffer = new StringBuffer();//返回的数据
InputStream ein = postMethod.getResponseBodyAsStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(ein,postMethod.getResponseCharSet()));
String inputLine = null;
while((inputLine = reader.readLine()) != null){contentBuffer.append(inputLine);}
String code = postMethod.getStatusLine().toString();    //打印服务器返回的状态  
ein.close();
postMethod.releaseConnection();
String con = contentBuffer.toString();
System.out.println(DateUtil.getTime()+"--"+code);  
System.out.println("con:"+con);
return code;
}


你可能感兴趣的:(java开发)