Android app did not receive data from SignalR hub

前端 未结 2 1735
难免孤独
难免孤独 2021-02-06 01:10

I already read these topics:
how to use SignalR in Android
Android Client doesn\'t get data but .net client getting data from SignalR server

I write a simple cha

2条回答
  •  走了就别回头了
    2021-02-06 01:21

    For the futures this is the way I finally achieved the answer (please first read the question android codes):

    public class ChatActivity extends AppCompatActivity
    {
        // private fields
        HubConnection connection;
        HubProxy hub;
        ClientTransport transport;
    
        protected void onCreate(Bundle savedInstanceState) {
    
            Logger logger = new Logger() {
                @Override
                public void log(String message, LogLevel logLevel) {
                    Log.e("SignalR", message);
                }
            };
    
            Platform.loadPlatformComponent(new AndroidPlatformComponent());
            connection = new HubConnection("192.168.1.100");
            hub = connection.createHubProxy("chatHub"); // case insensitivity
    
            /* ****new codes here**** */
            hub.subscribe(this);
    
            transport = new LongPollingTransport(connection.getLogger());
    
            /* ****new codes here**** */
            connection.start(transport);
    
            /* ****new codes here**** */
            /* ****seems useless but should be here!**** */
            hub.subscribe(new Object() {
                @SuppressWarnings("unused")
                public void newMessage(final String message, final String messageId, final String chatId,
                                       final String senderUserId, final String fileUrl, final String replyToMessageId) {
    
    
                }
            });
    
    
            /* ********************** */
            /* ****new codes here**** */
            /* **** the main method that I fetch data from server**** */
            connection.received(new MessageReceivedHandler() {
                @Override
                public void onMessageReceived(final JsonElement json) {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            JsonObject jsonObject = json.getAsJsonObject();
                            Log.e("", "response = " + jsonObject.toString());
    
                        }
                    });
                }
            });
            /* ********************** */
    
        }
    }
    

    !important note:
    The priority of the codes is important. this is how I fix my problem in this topic.

提交回复
热议问题