2015-05-15 9 views
9

Ich habe Probleme beim Verbinden meiner Java-Anwendung mit meinem SignalR Server.
Der Server ist sehr einfach und kann hier bestellt werden:.
https://code.msdn.microsoft.com/windowsdesktop/Using-SignalR-in-WinForms-f1ec847bSignalR Java kann keine Verbindung zum Server herstellen

ich Web-Clients (Javascript) und Windows-Clients (C#) aber ich habe mit meinem Java-Client Probleme verbinden kann (https://github.com/SignalR/java-client)

Hier ist mein Code so weit:

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(); 
     } 
} 

aktualisieren

Wenn ich es laufen erhalte ich eine „ExecutionException: java.net.SocketException: Anschluss 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) 

-Wenn ich „localhost“, um etwas zu ändern, die nicht (zB locahostX) Ich habe ein Java existiert bekommen .net.UnknownHostException

-Wenn Wenn ändern „localhost“ auf meine IP-I-Ereignis keine Ausnahme erhalten ...

-Alle anderen Anwendungen arbeiten sowohl mit (localhost oder IP)

Zuerst dachte ich, es wäre ein Firewall-Problem, aber es war nicht das ...

Offensichtlich vermisse ich etwas ... Irgendwelche Ideen?

Dank

Antwort

3

Es stellt sich heraus, dass ich eine Überlastung von Start zu verwenden, die eine, die als Parameter

public SignalRFuture<Void> start(ClientTransport transport) 

Wenn jemand eine Erklärung hat, warum die parameterlos Startmethode nicht ein ClientTransport Aufgabe übernimmt, bitte posten es als Antwort und ich werde es als die Lösung markieren. Hier

ist ein vollständiges Beispiel, das funktioniert:

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(); 
    } 
} 
+0

Nicht sicher über das Problem, aber haben Sie versucht, HubConnection-Konstruktor-Überladung, die ILogger-Instanz verwendet verwenden?Es scheint, als ob die automatische Transportauswahl (Verhandlung) nicht funktioniert und das interne SignalR-Protokoll könnte hier hilfreich sein. Probieren Sie es aus und veröffentlichen Sie das Protokoll ... –

0

Wenn ich "localhost", um etwas zu ändern, die nicht (z locahostX) existiert ich ein java.net.UnknownHostException

Sind Sie sicher?

  1. In der Server-Eingabeaufforderung "ipconfig" ausführen, um die IP-Adresse des Servers abzurufen.
  2. Geben Sie an der Clientbefehlseingabe "ping" + IP-Adresse des Servers ein.
  3. Wenn der Ping Pakete sendet, dann versuchen Sie, die IP in der Zeichenfolge "ServerURI" so etwas wie "http: //" + ServerIP + ": 8080/Signal".
+0

Dank Hristo aber ich hatte bereits versucht, die IP.At Zuerst dachte ich, es war ein Firewall-Problem, aber es war auch nicht so ... –

Verwandte Themen