Servicestack或SignalR的SSE Java客户端

时间:2016-11-30 09:36:16

标签: java jersey client signalr server-sent-events

我正在寻找一个可以将Sever支持的ServieStack或SignalR与Java客户端连接的组合。我想用我的Java客户端从服务器获取SSE消息。

使用泽西岛我可以创建一个SSE Java客户端并且可以获得事件但是,它不是我订阅的频道,它是真正的网站......

最好的问候 J.oster

2 个答案:

答案 0 :(得分: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;
    }

}

这是一个SignalR Java客户端,它与SignalR C#Server一起使用。 注意!所有名称(类,集线器,方法)都必须相同!

答案 1 :(得分:0)

ServiceStack具有功能齐全的Java Server Events Client,可用于通过来自Java 7/8 +,Kotlin和Android Apps的HTTP服务器发送事件提供实时通知。

请参阅Android Java Chat应用,该应用利用服务器事件进行实时通知,并使用Facebook,Twitter和Google的本地SDK启用无缝的OAuth登录。

enter image description here