XMPP即时通讯协议使用(十三)——获取当前在线用户或关闭指定用户

1、开启REST API插件或根据需求修改其插件源码;

2、添加服务器->服务器管理->系统属性中添加

plugin.restapi.enabled=true

3、pom依赖

<dependency>
	<groupId>org.igniterealtime</groupId>
	<artifactId>rest-api-client</artifactId>
	<version>1.1.4</version>
</dependency>

4、相关代码

import java.util.List;

import org.igniterealtime.restclient.RestApiClient;
import org.igniterealtime.restclient.entity.AuthenticationToken;
import org.igniterealtime.restclient.entity.SessionEntity;

public class Test {
	public static void main(String[] args) {
		// Set Shared secret key
		AuthenticationToken authenticationToken = new AuthenticationToken("admin", "123456");
		// Set Openfire settings (9090 is the port of Openfire Admin Console)
		RestApiClient restApiClient = new RestApiClient("http://127.0.0.1", 9090, authenticationToken);

		// Request all active Sessions
		System.out.println(restApiClient.getSessions());

		// Request all active Sessions from a specific user
		System.out.println(restApiClient.getSessions("admin"));
		
		List<SessionEntity> list = restApiClient.getSessions("admin").getSessions();
		for(SessionEntity session:list) {
			System.out.println(session.getSessionId());
		}
		// 关闭指定用户
		restApiClient.deleteSessions("admin");
	}
}

5、补充

Openfire源码下载地址:https://www.igniterealtime.org/downloads/source.jsp

rest-api-client源码地址:https://github.com/igniterealtime/REST-API-Client

posted on 2018-10-22 15:54  疯狂的小萝卜头  阅读(177)  评论(0编辑  收藏  举报