简体   繁体   中英

SignalR java client can't invoke method and send data

I created a basic selfhosted SignalR server with the following code:

    class Program
    {
        static void Main(string[] args)
        {
            // This will *ONLY* bind to localhost, if you want to bind to all addresses
            // use http://*:8080 to bind to all addresses. 
            // See http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx 
            // for more information.
            string url = "http://localhost:8080";
            using (WebApp.Start(url))
            {
                Console.WriteLine("Server running on {0}", url);
                Console.ReadLine();
            }
        }
    }
    class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseCors(CorsOptions.AllowAll);
            app.MapSignalR();
        }
    }
    public class MyHub : Hub
    {
        public void Send(string name, string message)
        {
            Clients.All.addMessage(name, message);
        }
    }

Which is taken from: https://docs.microsoft.com/en-us/aspnet/signalr/overview/deployment/tutorial-signalr-self-host and works with the Javascript client.

I am now trying to create a Java client and got the following code that is simply supposed to send a message to the server:

       String host = "http://localhost:8080";

        HubConnection connection = new HubConnection(host);

        HubProxy proxy = connection.createHubProxy("MyHub");

        connection.start();

        try {
            System.out.println("Sendng message...");
            proxy.invoke( "Send", "Client", "Hello world!" ).get();
            System.out.println("Message sent!");
        } catch (InterruptedException e) {
            System.out.println("err1");
            // Handle ...
        } catch (ExecutionException e) {
            System.out.println("err2");
            // Handle ...
        }

The problem that im having is that the message is not received by the server, it seems like the code is stuck at the invoke call and doesn't print the Hello world! message. Does someone know what im doing wrong?

hubProxy.invoke("sendMessageByUser", Message, WebApiToken).done(new Action<Void>() {
        @Override
        public void run(Void aVoid) {


            if (aVoid != null)

                handler.post(new Runnable() {
                    @Override
                    public void run() {

                        Toast.makeText(MyApplicationService.this, "Mesaj gönderildi", Toast.LENGTH_SHORT).show();


                    }
                });


        }
    }).onError(new ErrorCallback() {
        @Override
        public void onError(final Throwable error) {


            handler.post(new Runnable() {
                @Override
                public void run() {

                    Toast.makeText(MyApplicationService.this.getApplicationContext(), "Bir hata oluştu" + error.toString(), Toast.LENGTH_SHORT).show();

                }
            });


        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM