最近在整理一些這方面的資料,閑話少說,咱還是直奔主題吧 :)
一、基於xmpp實現的openfire的配置安裝
1、 下載最新的openfire安裝文件
官方下載站點:
http://www.igniterealtime.org/downloads/index.jsp#openfire
下載地址:
Exe:
http://www.igniterealtime.org/downloads/download-landing.jsp?file=openfire/openfire_3_7_1.exe
ZIP:
http://www.igniterealtime.org/downloads/download-landing.jsp?file=openfire/openfire_3_7_1.zip
在這里面openfire是服務器,下面還有一個spark,這個是一個XMPP協議通信聊天的CS的IM軟件,它可以通過openfire進行聊天對話。
2、 下載完成后,如果你下載的是exe文件,執行你的安裝文件,進行安裝。這里我是zip的文件。解壓后,復制openfire目錄到C:\Program Files\目錄下;一定要在C:\Program Files\目錄下的;這樣openfire就安裝完成了。
3、 下面我們啟動openfire服務器,並配置它。在C:\Program Files\openfire\bin目錄下有一個電燈泡的openfire.exe文件,雙擊執行,啟動完成后可以看到
4、 點擊Launch Admin按鈕進入http://127.0.0.1:9090/setup/index.jsp頁面,配置openfire服務器
5、 選擇語言 中文簡體
點擊continue進入
6、 配置服務器域名
如果你是本地訪問,那么你可以不修改或是使用localhost、127.0.0.1的方式
如果你用於外網或局域網訪問,那么你的地址配置成外網或局域網地址
7、 選擇數據庫
選擇openfire自帶的,當然你也可以選擇你的數據庫類型。如Oracle、SQLServer、MySQL等。如果openfire沒有帶jdbc的連接驅動,你需要添加連接數據庫的jdbc驅動;驅動放在C:\Program Files\openfire\lib目錄下
8、 選擇特性配置,默認即可
9、 管理員郵件,可以跳過這步
10、 安裝完成
進入管理員控制台頁面
11、 進入http://127.0.0.1:9090/login.jsp頁面后,輸入admin、密碼admin登陸進入
12、 進入后可以看到
服務器名稱就是jwchat的連接地址;你可以使用Spark、jwchat鏈接這個地址進行IM通信聊天……
至此,openfire的安裝和配置已經完成。下一篇文章開始完成jwchat的安裝和配置。
二、Android客戶端的實現
先瀏覽一下項目結構,然后開始逐一解析代碼:

主要代碼附上,最后是下載地址
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.formclient);
//獲取Intent傳過來的用戶名
this.pUSERID = getIntent().getStringExtra("USERID");
ListView listview = (ListView) findViewById(R.id.formclient_listview);
listview.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
this.adapter = new MyAdapter(this);
listview.setAdapter(adapter);
//獲取文本信息
this.msgText = (EditText) findViewById(R.id.formclient_text);
this.pb = (ProgressBar) findViewById(R.id.formclient_pb);
//消息監聽
ChatManager cm = XmppTool.getConnection().getChatManager();
//發送消息給water-pc服務器water(獲取自己的服務器,和好友)
// final Chat newchat = cm.createChat(this.pUSERID+"@water-pc", null);
final Chat newchat = cm.createChat("lee@water-pc", null);
final Chat newchat1 = cm.createChat("chai@water-pc", null);
final Chat newchat2 = cm.createChat("huang@water-pc", null);
cm.addChatListener(new ChatManagerListener() {
@Override
public void chatCreated(Chat chat, boolean able)
{
chat.addMessageListener(new MessageListener() {
@Override
public void processMessage(Chat chat2, Message message)
{
Log.v("--tags--", "--tags-form--"+message.getFrom());
Log.v("--tags--", "--tags-message--"+message.getBody());
//收到來自water-pc服務器water的消息(獲取自己的服務器,和好友)
if(message.getFrom().contains(pUSERID+"@water-pc"))
{
//獲取用戶、消息、時間、IN
String[] args = new String[] { pUSERID, message.getBody(), TimeRender.getDate(), "IN" };
//在handler里取出來顯示消息
android.os.Message msg = handler.obtainMessage();
msg.what = 1;
msg.obj = args;
msg.sendToTarget();
}
else
{
//message.getFrom().cantatins(獲取列表上的用戶,組,管理消息);
//獲取用戶、消息、時間、IN
String[] args = new String[] { message.getFrom(), message.getBody(), TimeRender.getDate(), "IN" };
//在handler里取出來顯示消息
android.os.Message msg = handler.obtainMessage();
msg.what = 1;
msg.obj = args;
msg.sendToTarget();
}
}
});
}
});
//附件
Button btattach = (Button) findViewById(R.id.formclient_btattach);
btattach.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0)
{
Intent intent = new Intent(FormClient.this, FormFiles.class);
startActivityForResult(intent, 2);
}
});
//發送消息
Button btsend = (Button) findViewById(R.id.formclient_btsend);
btsend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//獲取text文本
String msg = msgText.getText().toString();
if(msg.length() > 0){
//發送消息
listMsg.add(new Msg(pUSERID, msg, TimeRender.getDate(), "OUT"));
//刷新適配器
adapter.notifyDataSetChanged();
try {
//發送消息給xiaowang
newchat.sendMessage(msg);
newchat1.sendMessage(msg);
newchat2.sendMessage(msg);
}
catch (XMPPException e)
{
e.printStackTrace();
}
}
else
{
Toast.makeText(FormClient.this, "請輸入信息", Toast.LENGTH_SHORT).show();
}
//清空text
msgText.setText("");
}
});
//接受文件
FileTransferManager fileTransferManager = new FileTransferManager(XmppTool.getConnection());
fileTransferManager.addFileTransferListener(new RecFileTransferListener());
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//發送附件
if(requestCode==2 && resultCode==2 && data!=null){
String filepath = data.getStringExtra("filepath");
if(filepath.length() > 0)
{
sendFile(filepath);
}
}
}
最后不能忘了上項目代碼,地址為:
http://download.csdn.net/detail/sky_monkey/5820879









