前段时间做一个项目,需要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 socketList;
private MessageCenter()
{
this.socketList = new ArrayList();
}
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>
< servlet-mapping>
< servlet-name> android servlet-name >
< url-pattern> *.do url-pattern >
servlet-mapping>
推荐阅读: