基於XMPP實現的Openfire的配置安裝+Android客戶端的實現

最近在整理一些這方面的資料,閒話少說,咱仍是直奔主題吧 :)java


1、基於xmpp實現的openfire的配置安裝

一、 下載最新的openfire安裝文件
android

 

官方下載站點:數據庫

http://www.igniterealtime.org/downloads/index.jsp#openfire服務器

下載地址:jsp

Exe:ide

http://www.igniterealtime.org/downloads/download-landing.jsp?file=openfire/openfire_3_7_1.exethis

ZIP:
spa

http://www.igniterealtime.org/downloads/download-landing.jsp?file=openfire/openfire_3_7_1.zip.net

在這裏面openfire是服務器,下面還有一個spark,這個是一個XMPP協議通訊聊天的CS的IM軟件,它能夠經過openfire進行聊天對話。3d


二、 下載完成後,若是你下載的是exe文件,執行你的安裝文件,進行安裝。這裏我是zip的文件。解壓後,複製openfire目錄到C:\Program Files\目錄下;必定要在C:\Program Files\目錄下的;這樣openfire就安裝完成了。

 

三、 下面咱們啓動openfire服務器,並配置它。在C:\Program Files\openfire\bin目錄下有一個電燈泡的openfire.exe文件,雙擊執行,啓動完成後能夠看到

image

四、 點擊Launch Admin按鈕進入http://127.0.0.1:9090/setup/index.jsp頁面,配置openfire服務器

 

五、 選擇語言 中文簡體

image

點擊continue進入

 

六、 配置服務器域名

image

若是你是本地訪問,那麼你能夠不修改或是使用localhost、127.0.0.1的方式

若是你用於外網或局域網訪問,那麼你的地址配置成外網或局域網地址

 

七、 選擇數據庫

image

選擇openfire自帶的,固然你也能夠選擇你的數據庫類型。如Oracle、SQLServer、MySQL等。若是openfire沒有帶jdbc的鏈接驅動,你須要添加鏈接數據庫的jdbc驅動;驅動放在C:\Program Files\openfire\lib目錄下

 

八、 選擇特性配置,默認便可

image

 

九、 管理員郵件,能夠跳過這步

image

 

十、 安裝完成

image

進入管理員控制檯頁面

 

十一、 進入http://127.0.0.1:9090/login.jsp頁面後,輸入admin、密碼admin登錄進入

image

 

十二、 進入後能夠看到

image

服務器名稱就是jwchat的鏈接地址;你能夠使用Spark、jwchat連接這個地址進行IM通訊聊天……

至此,openfire的安裝和配置已經完成。下一篇文章開始完成jwchat的安裝和配置。


2、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

相關文章
相關標籤/搜索