利用websocket实现android消息推送
前段时间做一个项目,需要android客户端作为管理工具与web服务器后台实时交互,想了很多方法,包括androidpn、openfire+smack、xmpp协议,要么太繁琐,要么无法满足实时性。想来还是用socket,经人提醒想到了websocket。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
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(); } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
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 ); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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(); } } |
< 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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
import java.net.URI; import java.net.URISyntaxException; import org.java_websocket.client.WebSocketClient; import org.java_websocket.drafts.Draft; import org.java_websocket.drafts.Draft_10; import org.java_websocket.drafts.Draft_17; import org.java_websocket.framing.Framedata; import org.java_websocket.handshake.ServerHandshake; /** This example demonstrates how to create a websocket connection to a server. Only the most important callbacks are overloaded. */ public class ExampleClient extends WebSocketClient { public ExampleClient( URI serverUri , Draft draft ) { super ( serverUri, draft ); } public ExampleClient( URI serverURI ) { super ( serverURI ); } @Override public void onOpen( ServerHandshake handshakedata ) { System.out.println( "opened connection" ); // if you plan to refuse connection based on ip or httpfields overload: onWebsocketHandshakeReceivedAsClient } @Override public void onMessage( String message ) { System.out.println( "received: " + message ); } @Override public void onFragment( Framedata fragment ) { System.out.println( "received fragment: " + new String( fragment.getPayloadData().array() ) ); } @Override public void onClose( int code, String reason, boolean remote ) { // The codecodes are documented in class org.java_websocket.framing.CloseFrame System.out.println( "Connection closed by " + ( remote ? "remote peer" : "us" ) ); } @Override public void onError( Exception ex ) { ex.printStackTrace(); // if the error is fatal then onClose will be called additionally } public static void main( String[] args ) throws URISyntaxException { ExampleClient c = new ExampleClient( new URI( "ws://localhost:8080/myweb/android.do" ), new Draft_17() ); c.connect(); } } |
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。