logo
Home / ICE Developer Center / Start Programming

Start Programming with iPush

On-the-fly "Hello world" with Eclipse (page 4 of 5)

Step-by-Step develop an iPush client with Eclipse

Printing-friendly version

Step 13 - Subscribe demo subject demo.eclipse.[your_name]

After call ipushTCPConnect(), you need to check the return value (value of connId). If connection built successfully, connId will be greater than zero. Otherwise, connId will be less than zero.

In this example, the client will show message "Connect OK!" in the console view when connId is greater than zero and then subscribe a demo subject immediately by calling ipushSubSubject() method. Arguments needed:

  1. Connection ID: Connection ID of this client; connId used here.
  2. Subject Name: Subject name to be subscribed; demo.eclipse.[your_name] used here.

[your_name] represents your name or any string you assigned without any space.

Why you need to use your name to in a subscribed subject? It can avoid collision of subject using. Since other developers who see this article may try this example too, so attach your own name to the subject root demo.eclipse will reduce the opportunity of receiving messages sent by others. In this example, tonyhsiao is the name used by author.

if (connId > 0) {
     System.out.println("Connect OK!");
     ip1.ipushSubSubject(connId,"demo.eclipse.tonyhsiao");
}


Figure 20. subscribe a subject with your name for receiving messages

Step 14 - Quick Fix helps to add the try/catch codes

A red line shown beneath the method ipushSubSubject() indicates something wrong there. Click the icon of quick fixable error on the left and select [Surround with try/catch], Eclipse will help to add the try/catch codes.

Figure 21. the icon of quick fixable error on the left indicates there is an error in the line

try {
     ip1.ipushSubSubject(connId,"demo.eclipse.tonyhsiao");
} catch (iPush2Exception e) {
     e.printStackTrace();
}


Figure 22. Quick Fix helps to add the try/catch codes

Step 15 - Show an error message if connects to iPush Server failed

If the value of connId is less than zero, we want the client to show an error message for knowing the failure of connecting to iPush Server.

Please refer to the status code pages in iPush V2 Java API Programming Guide (file name: iPushV2_PG_Java_v2.x.x.pdf) to learn the meaning of each return code.

else {
        System.out.println("Connect Fail!! " + connId);
}


Figure 23. show an error message if connects to iPush Server failed

Step 16 - show the message received from iPush Server

When a message is received, the callback method onSubjectMessage() will be invoked. Use System.out.println() to show the subject name and content of the message in the console view.

The arguments of the onSubjectMessage() used here:

So String(arg3, 0, arg4) will return the text string of the message content.

System.out.println("Subject = " + arg1 + ", Data = " + new String(arg3, 0, arg4));


Figure 24. show the subject name and content of the received message in onSubjectMessage()

Step 17 - Create an instance of class iPushDemo in main()

Finally, create an instance of class iPushDemo in main() to complete this iPush client example.

new iPushDemo();


Figure 25. new an instance of class iPushDemo


Page 4 of 5  |  1  2  3  4  5

next page