Android 基於Netty的訊息推送方案之字串的接收和發送(三),androidnetty

來源:互聯網
上載者:User

Android 基於Netty的訊息推送方案之字串的接收和發送(三),androidnetty

在上一篇文章中《Android 基於Netty的訊息推送方案之概念和工作原理(二)》 ,我們介紹過一些關於Netty的概念和工作原理的內容,今天我們先來介紹一個叫做ChannelBuffer的東東。

ChannelBuffer


 Netty中的訊息傳遞,都必須以位元組的形式,以ChannelBuffer為載體傳遞。簡單的說,就是你想直接寫個字串過去,對不起,拋異常。雖然,Netty定義的writer的介面參數是Object的,這可能也是會給新上手的朋友容易造成誤會的地方。Netty源碼中,是這樣判斷的。

SendBuffer acquire(Object message) {        if (message instanceof ChannelBuffer) {            return acquire((ChannelBuffer) message);        } else if (message instanceof FileRegion) {            return acquire((FileRegion) message);        }         throw new IllegalArgumentException(                "unsupported message type: " + message.getClass());    }
接下來我們寫一個Demo來學習它。

服務端代碼如下

public class MessageServer {public static void main(String args[]){//服務啟動器ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),Executors.newCachedThreadPool()));//設定一個處理用戶端訊息和各種訊息事件的類(Handler)bootstrap.setPipelineFactory(new ChannelPipelineFactory(){@Overridepublic ChannelPipeline getPipeline() throws Exception {return Channels.pipeline(new BusinessHandler());}});//開放8000連接埠供用戶端串連bootstrap.bind(new InetSocketAddress(8000));}private static class BusinessHandler extends SimpleChannelHandler{// 服務端收到用戶端發送過來的訊息時,觸發此方法@Override        public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {        ChannelBuffer buffer = (ChannelBuffer)e.getMessage();        System.out.println("Receive:"+buffer.toString(Charset.defaultCharset()));        String msg = buffer.toString(Charset.defaultCharset()) + "has been processed!";        ChannelBuffer buffer2 = ChannelBuffers.buffer(msg.length());        buffer2.writeBytes(msg.getBytes());        e.getChannel().write(buffer2);        }}}
用戶端代碼如下

public class MessageClient {public static void main(String args[]) {ClientBootstrap bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));bootstrap.setPipelineFactory(new ChannelPipelineFactory() {@Overridepublic ChannelPipeline getPipeline() throws Exception {return Channels.pipeline(new MessageClientHandler());}});bootstrap.connect(new InetSocketAddress("127.0.0.1", 8000));}private static class MessageClientHandler extends SimpleChannelHandler {/** * 當綁定到服務端的時候觸發,給服務端發訊息。 */@Overridepublic void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {// 將字串,構造成ChannelBuffer,傳遞給服務端String msg = "Hello, I'm client.";ChannelBuffer buffer = ChannelBuffers.buffer(msg.length());buffer.writeBytes(msg.getBytes());e.getChannel().write(buffer);}}}

先啟動服務端,再啟動用戶端,可以看到服務端列印如下字串

Receive:Hello, I'm client.





你好 Spring mvc java伺服器ios android推送的 你最終怎實現的

使用Socket,參考netty等socket架構。Ios和Android稍有區別,Android下App可以同樣使用netty等或者直接使用原生Socket與伺服器簡曆Socket串連,IOS下代碼差不多,不過需要去蘋果申請一些亂七八糟的東西,Socket會由蘋果伺服器轉寄。
 
使用Google Cloud Messaging for Android在中國不可以接收推送?

目前GCM的伺服器都在國外,不能保證國內的使用者隨時能串連到國外的伺服器。要等以後Google在國內建了GCM伺服器,才能保證Google Cloud Messaging(GCM)服務的可靠性。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.