(转)android(SignalA)接收.net(SignalR)推送过来的消息
从网络上搜索到的Demo,自己进行了稍微的改动
Signala类库从https://github.com/erizet/SignalA获得,不过相关引用有错误,需要手动修正。
- package com.zsoft.SignalADemo;
- import android.app.Activity;
- import org.json.JSONArray;
- import com.zsoft.signala.hubs.*;
- import com.zsoft.signala.hubs.HubConnection;
- import com.zsoft.signala.transport.StateBase;
- import com.zsoft.signala.transport.longpolling.*;
- import android.content.OperationApplicationException;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.EditText;
- /**
- * Created by King on 2016/8/3.
- */
- public class chatHubActivity extends Activity {
- private final static String TAG = "KING";
- private final static String HUB_URL = "http://192.168.1.110:8022/signalr/hubs";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- 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) {
- Log.d(TAG, "OnError=" + exception.getMessage());
- }
- @Override
- public void OnMessage(String message) {
- Log.d(TAG, "message=" + message);
- }
- @Override
- public void OnStateChanged(StateBase oldState, StateBase newState) {
- Log.d(TAG, "OnStateChanged=" + oldState.getState() + " -> " + newState.getState());
- }
- };
- /*
- * hub代理 panderman 2013-10-25
- */
- private IHubProxy hub = null;
- /**
- * 开启推送服务 panderman 2013-10-25
- */
- private void beginConnect() {
- try {
- //服务器端的HUB为ChatHub
- 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();
- }
- }
关注90後梦想大师,梦想从未止步.