9

I'm having trouble connecting my java application to my SignalR Server.
The server is very simple and can be found here:
https://code.msdn.microsoft.com/windowsdesktop/Using-SignalR-in-WinForms-f1ec847b

I can connect web clients(javascript) and windows clients (C#) but I'm having trouble with my java client.(https://github.com/SignalR/java-client)

Here is my code so far:

package javaapplication2;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import microsoft.aspnet.signalr.client.SignalRFuture;
import microsoft.aspnet.signalr.client.hubs.HubConnection;
import microsoft.aspnet.signalr.client.hubs.HubProxy;

public class JavaApplication2 {
       public static void main(String[] args) throws IOException, InterruptedException, ExecutionException, TimeoutException 
       {
            String ServerURI = "http://localhost:8080/signalr";
            HubConnection Connection = new HubConnection(ServerURI);
            HubProxy HubProxy = Connection.createHubProxy("MyHub");
            HubProxy.on("AddMessage", () -> { System.out.println("Some message");        });

            Connection.error(new ErrorCallback() {
                @Override
                public void onError(Throwable error) {
                    error.printStackTrace(); //<==SocketException
                }
            });

            SignalRFuture<Void> con  =Connection.start();
            con.get(); 
       }
}

Update

When I run it I get a "ExecutionException: java.net.SocketException: Connection reset"

Exception in thread "main" java.util.concurrent.ExecutionException: java.net.SocketException: Connection reset
at microsoft.aspnet.signalr.client.SignalRFuture.get(SignalRFuture.java:112)
at microsoft.aspnet.signalr.client.SignalRFuture.get(SignalRFuture.java:102)
at javaapplication2.JavaApplication2.main(JavaApplication2.java:27)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:209)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:704)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:675)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1535)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1440)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at microsoft.aspnet.signalr.client.http.java.NetworkRunnable.run(NetworkRunnable.java:72)
at java.lang.Thread.run(Thread.java:745)

-If I change "localhost" to something that does not exist ( e.g locahostX) I get a java.net.UnknownHostException

-If If change "localhost" to my IP I don't event get an exception...

-All the other apps work with both (localhost or IP)

At first I thought it was a firewall issue but it wasn't that...

Obviously I'm missing something... Any ideas?

Thanks

George Vovos
  • 7,399
  • 2
  • 18
  • 40

2 Answers2

3

It turns out that I had to use an overload of start,the one that takes as a parameter a ClientTransport object

public SignalRFuture<Void> start(ClientTransport transport)

If anyone has an explanation why the parameterless start method fails ,please post it as an answer and I will mark it as the solution.

Here is a full example that works:

package javaapplication2;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import microsoft.aspnet.signalr.client.SignalRFuture;
import microsoft.aspnet.signalr.client.hubs.HubConnection;
import microsoft.aspnet.signalr.client.hubs.HubProxy;
import microsoft.aspnet.signalr.client.transport.ServerSentEventsTransport;
import microsoft.aspnet.signalr.client.hubs.SubscriptionHandler2;

public class JavaApplication2 {
   public static void main(String[] args) throws IOException, InterruptedException, ExecutionException, TimeoutException 
   {
        String ServerURI = "http://localhost:8080/signalr";
        HubConnection Connection = new HubConnection(ServerURI);
        HubProxy HubProxy = Connection.createHubProxy("MyHub");

        HubProxy.on("AddMessage", new SubscriptionHandler2<String, String>() {
          @Override
          public void run(String e1, String e2) {
             System.out.println(e1.toString()+  " -> " +e2.toString());   
          }
        }, String.class, String.class);

        SignalRFuture<Void> con  =Connection.start(new ServerSentEventsTransport(Connection.getLogger())); //Or LongPollingTransport

        con.get(); 

        Scanner inputReader = new Scanner(System.in);
        String line = inputReader.nextLine();
        while (!"exit".equals(line)) {
              HubProxy.invoke("send", "Console", line);
              line = inputReader.next();
         }

        inputReader.close();

        Connection.stop();
   }
}
George Vovos
  • 7,399
  • 2
  • 18
  • 40
  • Not sure about the problem but did you tried to use HubConnection constructor overload which takes ILogger instance ? It seems like automatic transport selection (negotiation) doesn't work and internal SignalR log could be helpful here. Try it and post the log... – Michal Levý May 24 '15 at 07:39
0

If I change "localhost" to something that does not exist ( e.g locahostX) I get a java.net.UnknownHostException

Are you sure about this?

  1. On the server command prompt run "ipconfig" to get the IP address of the server.
  2. From the client command prompt type "ping " + IP address of the server.
  3. If the ping sends packages, then try to put the IP in the string "ServerURI" to be something like "http://"+ServerIP+":8080/signalr".
  • Thanks Hristo but I had already tried the IP.At first I thought it was a firewall issue but it wasn't that either... – George Vovos May 18 '15 at 10:52