Add HubConnectionBuilder to Java client (#2780)

This commit is contained in:
Mikael Mengistu 2018-08-13 13:58:48 -07:00 committed by GitHub
parent 6310cffd7c
commit 94f99c3a29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 4 deletions

View File

@ -3,8 +3,6 @@
package com.microsoft.aspnet.signalr;
import com.microsoft.aspnet.signalr.HubConnection;
import java.util.Scanner;
public class Chat {
@ -13,7 +11,10 @@ public class Chat {
Scanner reader = new Scanner(System.in); // Reading from System.in
String input;
input = reader.nextLine();
HubConnection hubConnection = new HubConnection(input, LogLevel.Information);
HubConnection hubConnection = new HubConnectionBuilder()
.withUrl(input)
.configureLogging(LogLevel.Information).build();
hubConnection.on("Send", (message) -> {
System.out.println("REGISTERED HANDLER: " + message);

View File

@ -24,7 +24,13 @@ public class HubConnection {
public HubConnection(String url, Transport transport, Logger logger){
this.url = url;
this.protocol = new JsonHubProtocol();
this.logger = logger;
if (logger != null) {
this.logger = logger;
} else {
this.logger = new NullLogger();
}
this.callback = (payload) -> {
if (!handshakeReceived) {

View File

@ -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.");
}
}