歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

WebSocket實現Android消息推送

前段時間做一個項目,需要Android客戶端作為管理工具與web服務器後台實時交互,想了很多方法,包括androidpn、openfire+smack、xmpp協議,要麼太繁瑣,要麼無法滿足實時性。想來還是用socket,經人提醒想到了websocket。

websocket協議是近些年隨html5發展而誕生的,主要用於解決web服務器與客戶端無法雙向交互的問題。如今已經被W3C收入標准協議。

服務器支持:tomcat、jetty的最新版本都已支持websocket;如果不想更換現有服務器,也可用spring4.0作為替代。據說新版本的jre將收入websocket類,沒具體接觸。

客戶端支持:目前的主流浏覽器都已經實現了websocket,但由於前期協議版本變化太快,很多廠商沒有跟上。android默認浏覽器就不支持websocket,IE也直到IE10才支持。

網上已有通過html實現websocket client的例子,這裡我們用java實現客戶端連接。廢話不說,上Demo:

1.服務器端

服務器用了tomcat 7.0,直接使用tomcat的websocket實現

1)連接管理類

import java.io.IOException;
import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.List;
import org.apache.catalina.websocket.MessageInbound;
import org.apache.catalina.websocket.WsOutbound;

public class MessageCenter
{
 private static MessageCenter        instance          = new MessageCenter();

 private List<MessageInbound>        socketList;

 private MessageCenter()
 {
  this.socketList = new ArrayList<MessageInbound>();
 }

 public static MessageCenter getInstance()
 {
  return instance;
 }

 public void addMessageInbound(MessageInbound inbound)
 {
  socketList.add(inbound);
 }

 public void removeMessageInbound(MessageInbound inbound)
 {
  socketList.remove(inbound);
 }

 public void broadcast(CharBuffer msg) throws IOException
 {
  for (MessageInbound messageInbound : socketList)
  {
   CharBuffer buffer = CharBuffer.wrap(msg);
   WsOutbound outbound = messageInbound.getWsOutbound();
   outbound.writeTextMessage(CharBuffer.wrap("broadcasting:" + msg));
   // outbound.writeTextMessage(buffer);
   outbound.flush();
  }
 }
}

2)消息入口類

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.util.Date;

import org.apache.catalina.websocket.MessageInbound;
import org.apache.catalina.websocket.WsOutbound;

public class MyMessageInbound extends MessageInbound {

 @Override
 protected void onBinaryMessage(ByteBuffer arg0) throws IOException {
  // TODO Auto-generated method stub
 
 }

 @Override
 protected void onTextMessage(CharBuffer msg) throws IOException {
  System.out.println("Received:"+msg);
  MessageCenter.getInstance().broadcast(msg);
 
 }

 @Override
 protected void onClose(int status) {
  System.out.println("close:"+new Date());
  MessageCenter.getInstance().removeMessageInbound(this);
  super.onClose(status);
 }

 @Override
 protected void onOpen(WsOutbound outbound) {
  System.out.println("open :"+new Date());
  super.onOpen(outbound);
  MessageCenter.getInstance().addMessageInbound(this);
 }
}

3)Websocket servlet

import javax.servlet.http.HttpServletRequest;

import org.apache.catalina.websocket.StreamInbound;
import org.apache.catalina.websocket.WebSocketServlet;

public class MeWebSocketServlet extends WebSocketServlet
{

private static final long serialVersionUID = -7178893327801338294L;

@Override
protected StreamInbound createWebSocketInbound(String subProtocol, HttpServletRequest request)
{
System.out.println("##########client login#########");
return new MeMessageInbound();
}

}

4)添加servlet到web.xml

< servlet>
< servlet-name> android</ servlet-name >
< servlet-class> MyWebSocketServlet </servlet-class >
</ servlet>
< servlet-mapping>
< servlet-name> android</ servlet-name >
< url-pattern> *.do</ url-pattern >
</ servlet-mapping>

更多詳情見請繼續閱讀下一頁的精彩內容: http://www.linuxidc.com/Linux/2013-11/92474p2.htm

Copyright © Linux教程網 All Rights Reserved