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端口。
三、推送客戶端準備
a> 下載&解壓AndroidPushNotificationsDemo項目(下載地址:https://github.com/tokudu/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號。
四、發送服務準備
a> 下載&解壓PHP版的發送服務端代碼send_mqtt.zip(下載地址:http://download.csdn.net/detail/shagoo/4520102) b> 修改etc/config.php中推送服務端的IP地址和端口號,即MQTT_SERVER_HOST和MQTT_SERVER_POST常量。 c> 打開對應的URL地址,就能夠看到發送服務的界面,實際上就是向對應的推送客戶端推送消息。