Servicestack 或 SignalR 的 SSE Java 客户端
SSE Java client for Servicestack or SignalR
我正在寻找将服务器端 ServieStack 或 SignalR 与 Java 客户端连接的可能性。我想使用 Java 客户端从服务器获取 SSE 消息。
有了 Jersey,我可以创建一个 SSE Java 客户端并可以获取事件,但是,它不是我订阅的频道,它是 wohle 站点...
此致
J.oster
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# 服务器一起工作。
笔记!所有名称(class、中心、方法)必须相同!
ServiceStack 具有功能齐全的 Java Server Events Client,可用于通过来自 Java 7/8+、Kotlin 和 Android 应用程序的 HTTP 服务器发送事件提供实时通知.
查看 Android Java Chat 应用程序,它利用服务器事件进行实时通知,并使用 Facebook、Twitter 和 Google 的本地 SDK 实现无缝 OAuth 登录。
我正在寻找将服务器端 ServieStack 或 SignalR 与 Java 客户端连接的可能性。我想使用 Java 客户端从服务器获取 SSE 消息。
有了 Jersey,我可以创建一个 SSE Java 客户端并可以获取事件,但是,它不是我订阅的频道,它是 wohle 站点...
此致 J.oster
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# 服务器一起工作。 笔记!所有名称(class、中心、方法)必须相同!
ServiceStack 具有功能齐全的 Java Server Events Client,可用于通过来自 Java 7/8+、Kotlin 和 Android 应用程序的 HTTP 服务器发送事件提供实时通知.
查看 Android Java Chat 应用程序,它利用服务器事件进行实时通知,并使用 Facebook、Twitter 和 Google 的本地 SDK 实现无缝 OAuth 登录。