d> 运行./broker broker_1883.cfg,显示如下: 20120823 110454.039 CWNAN9999I Really Small Message Broker 20120823 110454.039 CWNAN9997I Licensed Materials - Property of IBM 20120823 110454.039 CWNAN9996I Copyright IBM Corp. 2007, 2010 All Rights Reserved 20120823 110454.039 CWNAN9995I US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 20120823 110454.039 CWNAN0049I Configuration file name is broker_1883.cfg 20120823 110454.040 CWNAN0053I Version 1.2.0, Aug 18 2010 17:03:35 20120823 110454.040 CWNAN0054I Features included: bridge 20120823 110454.040 CWNAN9993I Author: Ian Craggs (icraggs@uk.ibm.com) 20120823 110454.040 CWNAN0014I MQTT protocol starting, listening on port 1883 ... ... 这样,推送服务的服务端就已经准备好了,监听1883端口。
3、推送客户端准备
a> 下载&解压AndroidPushNotificationsDemo项目(下载地址:) b> 将该项目导入Eclipse中(File -> Export -> Existing Projects into Workspace) c> 修改PushService.java中的MQTT_HOST常量为推送服务端的IP地址。 d> 启动Android模拟器,并安装该项目。
注意:在新版本的Android SDK中可能会遇到以下错误。 ... ... 08-23 02:28:44.184: W/dalvikvm(282): VFY: unable to find class referenced in signature (Lcom/ibm/mqtt/MqttPersistence;) 08-23 02:28:44.194: I/dalvikvm(282): Failed resolving Lcom/tokudu/demo/PushService$MQTTConnection; interface 35 'Lcom/ibm/mqtt/MqttSimpleCallback;' 08-23 02:28:44.194: W/dalvikvm(282): Link of class 'Lcom/tokudu/demo/PushService$MQTTConnection;' failed 08-23 02:28:44.194: E/dalvikvm(282): Could not find class 'com.tokudu.demo.PushService$MQTTConnection', referenced from method com.tokudu.demo.PushService.connect 08-23 02:28:44.194: W/dalvikvm(282): VFY: unable to resolve new-instance 42 (Lcom/tokudu/demo/PushService$MQTTConnection;) in Lcom/tokudu/demo/PushService; ... ... 08-23 02:28:44.404: E/AndroidRuntime(282): java.lang.VerifyError: com.tokudu.demo.PushService 08-23 02:28:44.404: E/AndroidRuntime(282): at com.tokudu.demo.PushActivity$1.onClick(PushActivity.java:32) 08-23 02:28:44.404: E/AndroidRuntime(282): at android.view.View.performClick(View.java:2408) 08-23 02:28:44.404: E/AndroidRuntime(282): at android.view.View$PerformClick.run(View.java:8816) 08-23 02:28:44.404: E/AndroidRuntime(282): at android.os.Handler.handleCallback(Handler.java:587) 08-23 02:28:44.404: E/AndroidRuntime(282): at android.os.Handler.dispatchMessage(Handler.java:92) 08-23 02:28:44.404: E/AndroidRuntime(282): at android.os.Looper.loop(Looper.java:123) 08-23 02:28:44.404: E/AndroidRuntime(282): at android.app.ActivityThread.main(ActivityThread.java:4627) 08-23 02:28:44.404: E/AndroidRuntime(282): at java.lang.reflect.Method.invokeNative(Native Method) 08-23 02:28:44.404: E/AndroidRuntime(282): at java.lang.reflect.Method.invoke(Method.java:521) 08-23 02:28:44.404: E/AndroidRuntime(282): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 08-23 02:28:44.404: E/AndroidRuntime(282): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 08-23 02:28:44.404: E/AndroidRuntime(282): at dalvik.system.NativeStart.main(Native Method) ... ... 原因是发布的时候没有加入wmqtt.jar包,解决办法如下: 1> 在项目根目录下创建libs目录,并把wmqtt.jar包移入该目录。 2> 重新配置项目的Java Build Path(右键菜单中的Properties选项中)。 3> 重新打包发布即可。
运行效果如下:
点击“Start Push Service”按钮即可开启推送服务。这时我们可以看到rsmb的服务日志中打出以下提示: 20120823 113742.297 CWNAN0033I Connection attempt to listener 1883 received from client tokudu/9774d56d682e549c on address 192.168.28.39:3345 其中的“9774d56d682e549c”就是对应的客户端ID号。
4、发送服务准备
a> 下载&解压PHP版的发送服务端代码send_mqtt.zip(下载地址:) b> 修改etc/config.php中推送服务端的IP地址和端口号,即MQTT_SERVER_HOST和MQTT_SERVER_POST常量。 c> 打开对应的URL地址,就可以看到发送服务的界面,实际上就是向对应的推送客户端推送消息。