2016-11-30 1 views
1

Sever 측 ServieStack 또는 SignalR을 Java 클라이언트와 연결하기위한 방법을 찾고 있습니다. Java 클라이언트를 사용하여 서버에서 SSE 메시지를 가져오고 싶습니다. 나는 SSE 자바 클라이언트를 만들 수 있고, 그것은 wohle 사이트이었다 그것은 내가 구독 한 채널을 않네, 이벤트를 얻을 수 있지만, 수 뉴저지로서비스 클래스 또는 SignalR에 대한 SSE Java 클라이언트

...

안부

답변

0
import java.util.concurrent.ExecutionException; 

import microsoft.aspnet.signalr.client.*; 
import microsoft.aspnet.signalr.client.hubs.*; 
import microsoft.aspnet.signalr.client.transport.*; 

import com.google.gson.JsonElement; 

public class Client { 

public static void main(String[] args) throws Exception { 

    // Initialize the Logger 
    Logger logger = new Logger() { 

     @Override 
     public void log(String message, LogLevel level) { 
      System.out.println(message); 
     } 
    }; 

    String serverUri = "http://localhost:8080/signalr"; 

    // Connection to the Server 
    HubConnection conn = new HubConnection(serverUri); 

    // To invoke methods in the client, it have to be the same name as in 
    // the Server 
    HubProxy proxy = conn.createHubProxy("Hub"); 

    // Initializes the transport with a logger 
    ClientTransport transport = new ServerSentEventsTransport(conn.getLogger()); 

    //Listening to the Server 
    proxy.on("machineNotification",new SubscriptionHandler1<SentTestEvent>(){ 
     @Override 
     public void run(SentTestEvent send){ 
      System.out.println(send.Channel +"|"+send.From+"|"+send.Message+"|"+send.Selector+"|"+send.ToUserId); 
      //logger.log("result :="+msg, LogLevel.Information); 
     } 
    },SentTestEvent.class); 

    // Starts the connection synchronously by calling get() 
    SignalRFuture<Void> awaitConnection = conn.start(transport); 
    try { 
     awaitConnection.get(); 


     System.out.println("Connected to server at "+serverUri); 
    } catch (InterruptedException e) { 
     logger.log("Check " + e, LogLevel.Information); 
     e.printStackTrace(); 
    } catch (ExecutionException e) { 
     logger.log("Check " + e, LogLevel.Information); 
     e.printStackTrace(); 
    } 

    // Creates subscriptions for the connection 
    proxy.subscribe(awaitConnection); 

    System.out.println("connected.Id = "+conn.getConnectionId()); 
} 

public class SentTestEvent{ 
    String Channel; 
    String From; 
    String Message; 
    String Selector; 
    String ToUserId; 

    public SentTestEvent(String channel, String from, String message, String selector, String toUserId){ 
     Channel = channel; 
     From = from; 
     Message = message; 
     Selector = selector; 
     ToUserId = toUserId; 
    } 

} 
J.oster

이것은 SignalR Java 클라이언트이며 SignalR C# Server와 작동합니다. 참고! 모든 이름 (클래스, 허브, 메소드)은 동일해야합니다!

관련 문제