Where The Streets Have No Name

webview를 이용한 xmpp client 본문

Developement/Mobile

webview를 이용한 xmpp client

highheat 2011. 7. 29. 16:32
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<WebView 
	android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />
</LinearLayout>
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.webkit.WebView;
import android.widget.TextView;

public class XMPPClientActivity extends Activity {
	String TAG = "XMPPClient";
	WebView mWebView;
	Connection connection;
	Handler handler = new Handler();
	String recieveMsg = "";
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.addJavascriptInterface(new AndroidBridge(),"android");
        mWebView.loadUrl("file:///android_asset/index.html");
        
        connection = new XMPPConnection("호스트");
		try {
			connection.connect();
			connection.login("admin", "admin");
            sendMessage("아이디@호스트", "안녕하세요. XMPPClient가 실행되었습니다.");
			
			PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
			connection.addPacketListener(new PacketListener() {
                public void processPacket(Packet packet) {
                    Message message = (Message) packet;
                    if (message.getBody() != null) {
                        String fromName = StringUtils.parseBareAddress(message.getFrom());
                        Log.i(TAG, "Received message [" + message.getBody() + "] from [" + fromName + "]");
                        mWebView.loadUrl("javascript:recieveMessage('" + message.getBody() + "')");
                    }
                }
            }, filter);
			
		} catch (XMPPException e) {
			Log.e(TAG, "XMPPException", e);
		} catch (Exception ae){
			Log.e(TAG, "Exception", ae);
		}
        
        
    }
    
    private void sendMessage(String to, String message){
    	Message msg = new Message(to, Message.Type.chat);   
        msg.setBody(message);   
        connection.sendPacket(msg);
    }
    
    private class AndroidBridge{
    	public void sendMessage(final String to, final String message){
            XMPPClientActivity.this.sendMessage(to, message);
    	}
    }
    
}
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-1.6.1.js"></script> 
<script type="text/javascript">
function recieveMessage(arg){
	if(arg != ''){	
		var txt = $('#content').text();
		$('#content').append(arg+'<br />');
	}
}

$(document).ready(function(){
	$('#btnSendMessage').click(function(){
		window.android.sendMessage($('#txtTo').val(), $('#txtSendMessage').val());
	});
});

</script>
</head>
<body onload="/*aa()*/">
from : admin@호스트<br />
to : <input type="text" value="" size="30" id="txtTo"><br />
msg : <input type="text" value="" size="30" id="txtSendMessage"><br />
<input type="button" value="전송" id="btnSendMessage"><br />
<div id="content" style="background-color:green;"></div>
</body>
</html>