logo
Home / ICE Developer Center / Start Programming

Start Programming with iPush

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

Step-by-Step develop an iPush client with Eclipse

Printing-friendly version

Run-time Experience 1 - Run As a Java Application

Does it really work? Just run to try it.

Select [Run] / [Run As] / [Java Application] to run this example.

It will show Connect OK! in the Console view if connected to iPush Server successfully. Otherwise you have to check your network status or information provided in Step 12 to find out why connection failure.

Figure 26. Run the example as a Java Application

Figure 27. in Console view, Connect OK! will be shown if connected to iPush Server successfully

Now the client has subscribed subject demo.eclipse.[your_name] and is ready for receiving messages pushed from iPush Server.

Run-time Experience 2 - Run iPush2JavaSubjectSample As Message Sender

We are going to use the sample program - iPush2JavaSubjectSample (which is shipped with the iPush V2 client Java Package) to send messages into iPush Server.

The Java source code is located in the subfolder Samples\iPush2JavaSubjectSample of iPush V2 Java Package API, please compile and run it with your J2SDK environment.

Note: you may modify the related file paths in make.bat and run.bat (in the same subfolder) for compiling and running iPush2JavaSubjectSample correctly.

Once iPush2JavaSubjectSample runs, fill in information for connecting to iPush Server:

  1. Server: www.icetechnology.com
  2. Port: 8000
  3. Company: ICE
  4. Product: iPush
  5. Username: eclipse
  6. Password: eclipse

Figure 28. run iPush2JavaSubjectSample as message sender

Click [Connect TCP] button.

After connected, the status text Connect to server [www.icetechnology.com] with Connection ID [x] will be shown.

Let's send a message via subject named demo.eclipse.[your_name].

(a) Type demo.eclipse.[your_name] in the Subject field. It must be the same subject name subscribed in Step 13.

(b) Type Hello, world! in the Message field.

(c) Click [Send subject msg] button to send the message to iPush Server.

Figure 29. send Hello, world! to iPush Server with subject demo.eclipse.[your_name]

The status text Sent: Hello world! will be shown.

Run-time Experience 3 - Check the Received Message in Eclipse

Check the Console view of Eclipse, it should show Subject = demo.eclipse.[your_name], Data = Hello world!.

Yes, your example does work.

Figure 30. Subject = demo.eclipse.[your_name], Data = Hello world! shown in the Console view of Eclipse

This example client is ready to receive more messages from iPush Server. You may improve it by yourself. Click [Terminate] tool button on Console view of Eclipse to stop the program.

Figure 31. terminate the running of example client

Improvement Edition - Auto-reply Robot

Let's go back to Java editor of Eclipse, in the method OnSubjectMessage(), has ipl send a (non-persistent) message Hello world, too! back via subject demo.eclipse.[your_name]_reply on receiving messages.

Here we use the method ipushSendNPSubjectData() to send the replied message to iPush Server, arguments used:

ip1.ipushSendNPSubjectData(connId, "demo.eclipse.tonyhsiao_reply", "Hello world, too!", (byte)5, (byte)0);


Figure 32. add the auto-reply robot

Again, a quick fixable error occurs on the left of the method ipushSendNPSubjectData(), click the icon and select [Surround with try/catch] to fix it.

Figure 33. click the quick fixable error icon

try {
     ip1.ipushSendNPSubjectData(connId, "demo.eclipse.tonyhsiao_reply", "Hello world, too!", (byte)5, (byte)0);
} catch (iPush2Exception e) {
     e.printStackTrace();
}


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

Run-time Experience 4 - Bi-directional communication

Let's  run to try it again.

(a) In Eclipse, select [Run] / [Run As] / [Java Application]. Connect OK! should show in the Console view.

(b) Switch to iPush2JavaSubjectSample window if it still opened.

(c) Type demo.eclipse.[your_name]_reply in Subject field. It must be the same subject name subscribed in section Improvement Edition - Auto-reply Robot.

(d) Click [Subscribe subject] button to subscribe the subject.

You'll see the status text shown:

Subject subscribed [demo.eclipse.[your_name]_reply]
iPush[1]:Subject Command OK (700-demo.eclipse.[your_name]_reply)

Figure 35. subscribe subject demo.eclipse.[your_name]_reply in iPush2JavaSubjectSample

(e) Type demo.eclipse.[your_name] in Subject field of iPush2JavaSubjectSample window. It must be the same subject name subscribed in Step 13.

(f) Click [Send subject msg] button to send the message Hello world!.

Figure 36. send the message Hello world! again

(g) You'll see Hello world! in Console view of Eclipse and Hello world, too! in the iPush2JavaSubjectSample window. Your example client now can receive and reply messages via iPush Server. This illustrates the bi-directional communication capability of iPush application.

Figure 37. bi-directional communication capability of iPush application

Conclusion - Easy to develop iPush application with Eclipse

From this example, we have shown you how simple and quick iPush application development is. You can create a real-time Java application or applet with Eclipse and iPush V2 Java Package API in minutes too.


Page 5 of 5  |  1  2  3  4  5