关于使用环信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,引入标签

<script src="../web-im/demo/javascript/dist/webim.config.js"></script>
<script src='../web-im/sdk/dist/strophe-1.2.8.min.js'></script>
<script src='../web-im/webrtc/dist/webrtc-1.4.11.js'></script>
<script src='../web-im/sdk/dist/websdk-1.4.11.js'></script>
<script type="text/javascript" src="../static/js/jquery-1.7.2.js"></script>

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;
}


评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值