Android app did not receive data from SignalR hub

前端 未结 2 1736
难免孤独
难免孤独 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("<Debug>", "response = " + jsonObject.toString());
    
                        }
                    });
                }
            });
            /* ********************** */
    
        }
    }
    

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

    0 讨论(0)
  • 2021-02-06 01:32

    You does not provider parameters in your client-side which should be same as your side-site. The code should be below:

          hub.on("NewMessage", new SubscriptionHandler1<String>() {
            @Override
            public void run(String message) {
                Log.d("<Debug", "new message received in `on`"); 
            }
        },String.class);  //do not forget say the String class in the end
    
    0 讨论(0)
提交回复
热议问题