使用SignalR推送服務在Android的實現 SignalA_Android

來源:互聯網
上載者:User

由於我的版本最低是2.2,所以只有把源碼下下來自己改,如果你覺得太多了可自己編譯成jar引用,本人不才,對java不是很熟悉,如果此版本中有錯誤還請大家指出來,此圖顯示的是安卓2.2與4.0的版本。

chat_hub代碼:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   android:layout_width="match_parent"   android:layout_height="match_parent"   android:orientation="vertical" >   <EditText     android:id="@+id/chat_text"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:layout_margin="20dp"     android:gravity="top"     android:inputType="textMultiLine"     android:text="" />  </LinearLayout>

ChatHub.java代碼:

package com.loan.zhironghuimobile;  import org.json.JSONArray;  import com.zsoft.SignalA.Hubs.HubConnection; import com.zsoft.SignalA.Hubs.HubOnDataCallback; import com.zsoft.SignalA.Hubs.IHubProxy; import com.zsoft.SignalA.Transport.StateBase; import com.zsoft.SignalA.Transport.Longpolling.LongPollingTransport;  import android.app.Activity; import android.content.OperationApplicationException; import android.os.Bundle; import android.widget.EditText;  public class ChatHub extends Activity {      private final static String HUB_URL="http://192.168.1.200:82/signalr/hubs";   @Override   protected void onCreate(Bundle savedInstanceState) {     // TODO Auto-generated method stub     super.onCreate(savedInstanceState);     setContentView(R.layout.chat_hub);     beginConnect();   }   /**   * hub連結   */   private HubConnection conn=new HubConnection(HUB_URL, this, new LongPollingTransport()) {     @Override     public void OnError(Exception exception) {     }     @Override     public void OnMessage(String message) {     }     @Override     public void OnStateChanged(StateBase oldState, StateBase newState) {     }   };   /**   * hub代理 panderman 2013-10-25   */   private IHubProxy hub = null;   /**   * 開啟推送服務 panderman 2013-10-25   */   private void beginConnect(){     try {       hub=conn.CreateHubProxy("ChatHub");     } catch (OperationApplicationException e) {       e.printStackTrace();     }     hub.On("addNewMessageToPage", new HubOnDataCallback()     {       @Override       public void OnReceived(JSONArray args) {         EditText chatText=(EditText)findViewById(R.id.chat_text);         //chatText.setText(args.toString());         for(int i=0; i<args.length(); i++)         {           chatText.append(args.opt(i).toString());         }       }     });     conn.Start();   } }

SignalR伺服器端代碼參照http://www.asp.net/signalr來寫

更多用法參照SignalA官方文檔

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.