本博客要介绍的内容:
Openfire做服务器端,两个客户端:Spark、android模拟器。实现两个客户端之间的通信。
第一步:启动openfire服务器。(这里需要用到两个用户登录,前面的博客中已经说明如何添加用户了。)
[img]
[/img]
第二步:启动Spark客户端,如果您还没有Spark,下载请到:
http://www.igniterealtime.org/downloads/index.jsp;安装简单。
我电脑的ip为:192.168.0.124,根据自己的修改。
安装后运行如下下图:
[img]
[/img]
第三步:启动eclipse中的项目(另一个客户端),代码如下:
package com.example.openfiretest;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Message;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText accountEditText;
private EditText passwordEditText;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
accountEditText = (EditText) findViewById(R.id.username);
passwordEditText = (EditText) findViewById(R.id.password);
findViewById(R.id.login).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String account = accountEditText.getText().toString();
String password = passwordEditText.getText().toString();
if (account.equals("") || password.equals("")) {
Toast.makeText(MainActivity.this, "账号或密码不能为空!",
Toast.LENGTH_SHORT).show();
} else {
ClientConServer ccs = new ClientConServer(MainActivity.this);
boolean b = ccs.login(account, password);
// 如果登录成功
if (b) {
Toast.makeText(MainActivity.this, "登陆成功!",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,ChatActivity.class);
startActivity(intent);
} else {
Toast.makeText(MainActivity.this, "登陆失败!",
Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
package com.example.openfiretest;
import java.util.Collection;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterGroup;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;
public class ClientConServer {
private static int PORT=5222;
private Context context;
public ClientConServer(Context context){
this.context=context;
}
//这里收到消息后,通过广播将消息发送到需要的地方.哈哈,既然收到了服务器发送来的信息,如何处理自己决定。
private Handler handler = new Handler(){
public void handleMessage(android.os.Message m) {
Message msg=new Message();
msg=(Message) m.obj;
//把从服务器获得的消息通过广播发送
Intent intent = new Intent("org.yhn.mes");
String[] message=new String[]{
msg.getFrom(),
msg.getBody()};
System.out.println("==========收到服务器消息 From==========="+message[0].toString());
System.out.println("==========收到服务器消息 Body==========="+message[1].toString());
intent.putExtra("message", message);
context.sendBroadcast(intent);
};
};
public boolean login(String a,String p){
//ConnectionConfiguration config = new ConnectionConfiguration("192.168.0.124", PORT);
/** 是否启用安全验证 */
//config.setSASLAuthenticationEnabled(false);
/** 是否启用调试 */
//config.setDebuggerEnabled(true);
/** 创建connection链接 */
//XMPPConnection connection = new XMPPConnection(config);
XMPPConnection connection = ConnUtil.getConnection();
try {
/** 建立连接 */
connection.connect();
/** 登录*/
connection.login(a, p);
/** 开启读写线程,并加入到管理类中*/
//ClientSendThread cst=new ClientSendThread(connection);
//cst.start();
//ManageClientThread.addClientSendThread(a, cst);
//获取用户组、成员信息。
System.out.println("======开始获取组及用户==========");
Roster roster = connection.getRoster();
Collection<RosterGroup> entriesGroup = roster.getGroups();
System.out.println("组的个数:"+entriesGroup.size());
for(RosterGroup group: entriesGroup){
Collection<RosterEntry> entries = group.getEntries();
System.out.println("=========groupName==="+group.getName());
for (RosterEntry entry : entries) {
//Presence presence = roster.getPresence(entry.getUser());
//Log.i("---", "user: "+entry.getUser());
System.out.println("组成员的名字:"+entry.getName());
System.out.println("组成员的user:"+entry.getUser());
//Log.i("---", "tyep: "+entry.getType());
//Log.i("---", "status: "+entry.getStatus());
//Log.i("---", "groups: "+entry.getGroups());
}
}
System.out.println("======结束获取组及用户==========");
//在登陆以后应该建立一个监听消息的监听器,用来监听收到的消息:
ChatManager chatManager = connection.getChatManager();
chatManager.addChatListener(new MyChatManagerListener());
return true;
} catch (XMPPException e) {
e.printStackTrace();
}
return false;
}
/** message listener*/
class MyChatManagerListener implements ChatManagerListener {
public void chatCreated(Chat chat, boolean arg1) {
chat.addMessageListener(new MessageListener(){
public void processMessage(Chat arg0, Message msg) {
/**通过handler转发消息*/
android.os.Message m=handler.obtainMessage();
m.obj=msg;
m.sendToTarget();
}
});
}
}
}
package com.example.openfiretest;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
public class ConnUtil {
private static XMPPConnection connection;
public static synchronized XMPPConnection getConnection(){
if(connection!=null){
return connection;
}else{
ConnectionConfiguration config = new ConnectionConfiguration("192.168.0.124", 5222);
/** 是否启用安全验证 */
config.setSASLAuthenticationEnabled(false);
/** 是否启用调试 */
//config.setDebuggerEnabled(true);
/** 创建connection链接 */
connection = new XMPPConnection(config);
}
return connection;
}
}
package com.example.openfiretest;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class ChatActivity extends Activity {
String account;
String chatNick;
public static String chatContents;
TextView chatTextView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_chat);
/** 设置top面板信息*/
//chatNick=getIntent().getStringExtra("nick");
//account=getIntent().getStringExtra("account");
//这里的用户昵称和账号是手动添加的。
chatNick = "忍者无敌笑哈哈";
account = "number1@admin-pc";
TextView nick_tv=(TextView) findViewById(R.id.chat_top_nick);
nick_tv.setText(chatNick);
Button send_btn=(Button) findViewById(R.id.chat_btn_send);
send_btn.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
EditText input=(EditText) findViewById(R.id.chat_input);
String content=input.getText().toString();
try {
XMPPConnection connection = ConnUtil.getConnection();
ChatManager cm = connection.getChatManager();
Chat chat=cm.createChat(account, new MessageListener(){
public void processMessage(Chat arg0, Message msg) {
Log.i("---", msg.getFrom()+"说:"+msg.getBody());
//添加消息到聊天窗口 ,
}
});
Message m=new Message();
m.setBody(content);
chat.sendMessage(m);
} catch (XMPPException e) {
e.printStackTrace();
}
}
});
}
}
启动后如下图:
[img]
[/img]
第四步:这时,Spark客户端的篮球组中多了一个人。(不清楚组添加、用户添加的请看前面的博客);
[img]
[/img]
第五步:手机模拟器发送消息给Spark,输入信息后点击发送,
[img]
[/img]
第六步:Spark给手机模拟器发送消息,输入消息后按Ctrl+Enter发送,手机端把收到的消息,通过控制台打印。
[img]
[/img]
第七步:openfire服务器端发送消息给所有的客户端:
[img]
[/img]
over
菜鸟学习openfire,如有不妥之处,多多批评。