(转)android(SignalA)接收.net(SignalR)推送过来的消息

从网络上搜索到的Demo,自己进行了稍微的改动

Signala类库从https://github.com/erizet/SignalA获得,不过相关引用有错误,需要手动修正。

下载相关源码

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.zsoft.SignalADemo;  
  2.   
  3. import android.app.Activity;  
  4.   
  5. import org.json.JSONArray;  
  6.   
  7. import com.zsoft.signala.hubs.*;  
  8. import com.zsoft.signala.hubs.HubConnection;  
  9. import com.zsoft.signala.transport.StateBase;  
  10. import com.zsoft.signala.transport.longpolling.*;  
  11.   
  12. import android.content.OperationApplicationException;  
  13. import android.os.Bundle;  
  14. import android.util.Log;  
  15. import android.widget.EditText;  
  16.   
  17. /** 
  18.  * Created by King on 2016/8/3. 
  19.  */  
  20. public class chatHubActivity extends Activity {  
  21.   
  22.     private final static String TAG = "KING";  
  23.     private final static String HUB_URL = "http://192.168.1.110:8022/signalr/hubs";  
  24.   
  25.     @Override  
  26.     protected void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.chat_hub);  
  29.         beginConnect();  
  30.     }  
  31.   
  32.     /** 
  33.      * hub链接 
  34.      */  
  35.     private HubConnection conn = new HubConnection(HUB_URL, this, new LongPollingTransport()) {  
  36.         @Override  
  37.         public void OnError(Exception exception) {  
  38.             Log.d(TAG, "OnError=" + exception.getMessage());  
  39.         }  
  40.   
  41.         @Override  
  42.         public void OnMessage(String message) {  
  43.             Log.d(TAG, "message=" + message);  
  44.         }  
  45.   
  46.         @Override  
  47.         public void OnStateChanged(StateBase oldState, StateBase newState) {  
  48.             Log.d(TAG, "OnStateChanged=" + oldState.getState() + " -> " + newState.getState());  
  49.         }  
  50.     };  
  51.   
  52.     /* 
  53.      * hub代理 panderman 2013-10-25 
  54.      */  
  55.     private IHubProxy hub = null;  
  56.   
  57.     /** 
  58.      * 开启推送服务 panderman 2013-10-25 
  59.      */  
  60.     private void beginConnect() {  
  61.         try {  
  62.             //服务器端的HUB为ChatHub  
  63.             hub = conn.CreateHubProxy("ChatHub");  
  64.         } catch (OperationApplicationException e) {  
  65.             e.printStackTrace();  
  66.         }  
  67.         hub.On("addNewMessageToPage", new HubOnDataCallback() {  
  68.             @Override  
  69.             public void OnReceived(JSONArray args) {  
  70.                 EditText chatText = (EditText) findViewById(R.id.chat_text);  
  71.                 //chatText.setText(args.toString());  
  72.                 for (int i = 0; i < args.length(); i++) {  
  73.                     chatText.append(args.opt(i).toString());  
  74.                 }  
  75.             }  
  76.         });  
  77.         conn.Start();  
  78.     }  
  79. }  
posted @ 2017-02-20 16:35  90后梦想大师  阅读(2217)  评论(0编辑  收藏  举报