Add HubConnectionBuilder to Java client (#2780)
This commit is contained in:
parent
6310cffd7c
commit
94f99c3a29
|
|
@ -3,8 +3,6 @@
|
||||||
|
|
||||||
package com.microsoft.aspnet.signalr;
|
package com.microsoft.aspnet.signalr;
|
||||||
|
|
||||||
import com.microsoft.aspnet.signalr.HubConnection;
|
|
||||||
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class Chat {
|
public class Chat {
|
||||||
|
|
@ -13,7 +11,10 @@ public class Chat {
|
||||||
Scanner reader = new Scanner(System.in); // Reading from System.in
|
Scanner reader = new Scanner(System.in); // Reading from System.in
|
||||||
String input;
|
String input;
|
||||||
input = reader.nextLine();
|
input = reader.nextLine();
|
||||||
HubConnection hubConnection = new HubConnection(input, LogLevel.Information);
|
|
||||||
|
HubConnection hubConnection = new HubConnectionBuilder()
|
||||||
|
.withUrl(input)
|
||||||
|
.configureLogging(LogLevel.Information).build();
|
||||||
|
|
||||||
hubConnection.on("Send", (message) -> {
|
hubConnection.on("Send", (message) -> {
|
||||||
System.out.println("REGISTERED HANDLER: " + message);
|
System.out.println("REGISTERED HANDLER: " + message);
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,13 @@ public class HubConnection {
|
||||||
public HubConnection(String url, Transport transport, Logger logger){
|
public HubConnection(String url, Transport transport, Logger logger){
|
||||||
this.url = url;
|
this.url = url;
|
||||||
this.protocol = new JsonHubProtocol();
|
this.protocol = new JsonHubProtocol();
|
||||||
this.logger = logger;
|
|
||||||
|
if (logger != null) {
|
||||||
|
this.logger = logger;
|
||||||
|
} else {
|
||||||
|
this.logger = new NullLogger();
|
||||||
|
}
|
||||||
|
|
||||||
this.callback = (payload) -> {
|
this.callback = (payload) -> {
|
||||||
|
|
||||||
if (!handshakeReceived) {
|
if (!handshakeReceived) {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
package com.microsoft.aspnet.signalr;
|
||||||
|
|
||||||
|
public class HubConnectionBuilder {
|
||||||
|
private boolean built;
|
||||||
|
private String url;
|
||||||
|
private Transport transport;
|
||||||
|
private Logger logger;
|
||||||
|
|
||||||
|
public HubConnectionBuilder withUrl(String url) {
|
||||||
|
this.url = url;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HubConnectionBuilder withUrl(String url, Transport transport) {
|
||||||
|
this.url = url;
|
||||||
|
this.transport = transport;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HubConnectionBuilder configureLogging(LogLevel logLevel) {
|
||||||
|
this.logger = new ConsoleLogger(logLevel);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HubConnectionBuilder configureLogging(Logger logger) {
|
||||||
|
this.logger = logger;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HubConnection build() throws Exception {
|
||||||
|
if (!built) {
|
||||||
|
built = true;
|
||||||
|
return new HubConnection(url, transport, logger);
|
||||||
|
}
|
||||||
|
throw new Exception("HubConnectionBuilder allows creation only of a single instance of HubConnection.");
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue