雖然google爲Android開發者提供了GCM實現push,但是因爲需要系統安裝了google play、google帳號、系統>2.2、google push服務器在國外等多種原因,在中國,Android上想實現push還需要自己努力。
當前最火的開源push是基於xmpp協議的androidpn。androidpn是基於Mina框架的,所以這裏從Mina框架開始入手。
下面通過簡單的例子來學習mina的使用。首先創建服務端,工程正使用了3個jar包 看代碼:Server
HelloMina的處理器:
- /**
- * HelloMina的處理邏輯
- * @author zhangxy
- */
- class MinaServerHandler extends IoHandlerAdapter {
- @Override
- public void exceptionCaught( IoSession session, Throwable cause ) throws Exception{
- cause.printStackTrace();
- session.close();
- }
- @Override
- public void messageReceived( IoSession session, Object message ) throws Exception
- {
- String str = message.toString();
- if( str.trim().equalsIgnoreCase("quit") ) {
- session.close();
- return;
- }
- System.err.println("收到客戶端發來的消息::"+str);
- StringBuilder buf = new StringBuilder(str.length());
- for (int i = str.length() - 1; i >= 0; i--) {
- buf.append(str.charAt(i));
- }
- // and write it back.
- session.write(buf.toString());
- }
- @Override
- public void sessionIdle( IoSession session, IdleStatus status ) throws Exception{
- System.out.println( "IDLE " + session.getIdleCount( status ));
- }
- @Override
- public void messageSent(IoSession session, Object message)
- throws Exception {
- // TODO Auto-generated method stub
- super.messageSent(session, message);
- }
- @Override
- public void sessionClosed(IoSession session) throws Exception {
- // TODO Auto-generated method stub
- super.sessionClosed(session);
- System.out.println( "session closed");
- }
- @Override
- public void sessionCreated(IoSession session) throws Exception {
- // TODO Auto-generated method stub
- super.sessionCreated(session);
- System.out.println( "session create");
- }
- @Override
- public void sessionOpened(IoSession session) throws Exception {
- // TODO Auto-generated method stub
- super.sessionOpened(session);
- System.out.println( "session opened");
- }
- }
client
下面是Client代碼,Client沒有使用NIO,使用的普通socket實現:
- public class HelloMinaClient {
- private Socket socket;
- private DataOutputStream out;
- private DataInputStream in;
- public HelloMinaClient() throws IOException {
- }
- /**
- * @param args
- */
- public static void main(String[] args) throws Exception {
- // TODO Auto-generated method stub
- HelloMinaClient minaClient = new HelloMinaClient();
- minaClient.minaClient();
- }
- /**
- * 發送消息
- * @param out
- */
- public void sendMessage(Socket s) {
- try {
- out = new DataOutputStream(s.getOutputStream());
- out.writeBytes("mina\n");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void receiveMessage(Socket s) {
- try {
- in = new DataInputStream(s.getInputStream());
- System.err.println("收到服務端發來的消息::"+in.readLine());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void minaClient() throws Exception {
- while (true) {
- try {
- socket = new Socket("192.168.21.121", 9124);
- sendMessage(socket);
- receiveMessage(socket);
- out.close();
- in.close();
- Thread.sleep(5000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch(Exception e){
- e.printStackTrace();
- }finally {
- try{
- if(socket!=null)socket.close(); //斷開連接
- }catch (IOException e) {e.printStackTrace();}
- }
- }
- }
- }
SOCKET作爲短連接,即收發消息後SOCKET斷開一次,線程過5秒又建立連接收發消息。
/**
* @author 張興業
* 郵箱:xy-zhang#163.com
* android開發進階羣:278401545
* http://blog.csdn.net/xyz_lmn
* http://xyzlmn.blog.51cto.com/
*/