Android Netty-based message push solution-string receiving and sending (3), androidnetty

Source: Internet
Author: User

Android Netty-based message push solution-string receiving and sending (3), androidnetty

In the previous article "Concepts and working principles of the Netty-based message push solution for Android (ii)", we introduced some concepts and working principles of Netty, today we will first introduce something called ChannelBuffer.

ChannelBuffer


All messages in Netty must be transmitted in bytes, using ChannelBuffer as the carrier. Simply put, you want to write a string directly. Sorry, an exception is thrown. Although the writer interface parameter defined by Netty is Object, it may also cause misunderstandings to new friends. In the Netty source code, this is the case.

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());    }
Next we will write a Demo to learn about it.

The server code is as follows:

Public class MessageServer {public static void main (String args []) {// service initiator ServerBootstrap bootstrap = new ServerBootstrap (new NioServerSocketChannelFactory (Executors. newCachedThreadPool (), Executors. newCachedThreadPool (); // sets a Handler bootstrap class for processing client messages and various message events. setPipelineFactory (new ChannelPipelineFactory () {@ Overridepublic ChannelPipeline getPipeline () throws Exception {return Channels. pipeline (n Ew BusinessHandler () ;}}); // open port 8000 for the client to connect to bootstrap. bind (new InetSocketAddress (8000);} private static class BusinessHandler extends SimpleChannelHandler {// This method is triggered when the server receives a message sent from the client @ 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 );}}}
The client code is as follows:

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 () ;}}); boot Consumer. connect (new InetSocketAddress ("127.0.0.1", 8000);} private static class MessageClientHandler extends SimpleChannelHandler {/*** sends a message to the server when it is bound to the server. * // @ Overridepublic void channelConnected (ChannelHandlerContext ctx, ChannelStateEvent e) {// construct the String into a ChannelBuffer and pass it to the Server String msg = "Hello, I'm client. "; ChannelBuffer buffer = ChannelBuffers. buffer (msg. length (); buffer. writeBytes (msg. getBytes (); e. getChannel (). write (buffer );}}}

Start the server first and then start the client. You can see that the server prints the following string:

Receive:Hello, I'm client.





Hello, Spring mvc, java Server, ios, android, how do you implement it?

Use Socket, refer to socket framework such as netty. Ios and Android are slightly different. Android apps can also use netty or directly use native Socket to connect to the server resume Socket. The code in IOS is similar, but you need to apply for something messy from Apple, the Socket is forwarded by the Apple Server.

Cannot Google Cloud Messaging for Android receive push messages in China?

Currently, GCM servers are abroad, and users in China cannot connect to servers in other countries at any time. The reliability of Google Cloud Messaging (GCM) services will not be guaranteed until Google has established a GCM server in China.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.