Java Client API Review pt2 (#3078)
This commit is contained in:
parent
813a9e68bc
commit
7eb16afc9c
|
|
@ -3,30 +3,24 @@
|
|||
|
||||
package com.microsoft.aspnet.signalr;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
class CallbackMap {
|
||||
private ConcurrentHashMap<String, List<InvocationHandler>> handlers = new ConcurrentHashMap<>();
|
||||
private Map<String, List<InvocationHandler>> handlers = new ConcurrentHashMap<>();
|
||||
|
||||
public InvocationHandler put(String target, ActionBase action, ArrayList<Class<?>> classes) {
|
||||
InvocationHandler handler = new InvocationHandler(action, Collections.unmodifiableList(classes));
|
||||
|
||||
handlers.computeIfPresent(target, (methodName, handlerList) -> {
|
||||
handlerList.add(handler);
|
||||
return handlerList;
|
||||
public InvocationHandler put(String target, ActionBase action, Class<?>... classes) {
|
||||
InvocationHandler handler = new InvocationHandler(action, classes);
|
||||
handlers.compute(target, (key, value) -> {
|
||||
if (value == null) {
|
||||
value = new ArrayList<>();
|
||||
}
|
||||
value.add(handler);
|
||||
return value;
|
||||
});
|
||||
handlers.computeIfAbsent(target, (ac) -> new ArrayList<>(Arrays.asList(handler)));
|
||||
return handler;
|
||||
}
|
||||
|
||||
public Boolean containsKey(String key) {
|
||||
return handlers.containsKey(key);
|
||||
}
|
||||
|
||||
public List<InvocationHandler> get(String key) {
|
||||
return handlers.get(key);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@
|
|||
|
||||
package com.microsoft.aspnet.signalr;
|
||||
|
||||
class CloseMessage extends HubMessage {
|
||||
private String error;
|
||||
final class CloseMessage extends HubMessage {
|
||||
private final String error;
|
||||
|
||||
@Override
|
||||
public HubMessageType getMessageType() {
|
||||
|
|
@ -12,6 +12,7 @@ class CloseMessage extends HubMessage {
|
|||
}
|
||||
|
||||
public CloseMessage() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public CloseMessage(String error) {
|
||||
|
|
|
|||
|
|
@ -3,15 +3,14 @@
|
|||
|
||||
package com.microsoft.aspnet.signalr;
|
||||
|
||||
class CompletionMessage extends HubMessage {
|
||||
private int type = HubMessageType.COMPLETION.value;
|
||||
private String invocationId;
|
||||
private Object result;
|
||||
private String error;
|
||||
final class CompletionMessage extends HubMessage {
|
||||
private final int type = HubMessageType.COMPLETION.value;
|
||||
private final String invocationId;
|
||||
private final Object result;
|
||||
private final String error;
|
||||
|
||||
public CompletionMessage(String invocationId, Object result, String error) {
|
||||
if (error != null && result != null)
|
||||
{
|
||||
if (error != null && result != null) {
|
||||
throw new IllegalArgumentException("Expected either 'error' or 'result' to be provided, but not both");
|
||||
}
|
||||
this.invocationId = invocationId;
|
||||
|
|
|
|||
|
|
@ -82,19 +82,22 @@ class DefaultHttpClient extends HttpClient {
|
|||
@Override
|
||||
public CompletableFuture<HttpResponse> send(HttpRequest httpRequest) {
|
||||
Request.Builder requestBuilder = new Request.Builder().url(httpRequest.getUrl());
|
||||
if (httpRequest.getMethod() == "GET") {
|
||||
requestBuilder.get();
|
||||
} else if (httpRequest.getMethod() == "POST") {
|
||||
RequestBody body = RequestBody.create(null, new byte[] {});
|
||||
requestBuilder.post(body);
|
||||
} else if (httpRequest.getMethod() == "DELETE") {
|
||||
requestBuilder.delete();
|
||||
|
||||
switch (httpRequest.getMethod()) {
|
||||
case "GET":
|
||||
requestBuilder.get();
|
||||
break;
|
||||
case "POST":
|
||||
RequestBody body = RequestBody.create(null, new byte[]{});
|
||||
requestBuilder.post(body);
|
||||
break;
|
||||
case "DELETE":
|
||||
requestBuilder.delete();
|
||||
break;
|
||||
}
|
||||
|
||||
if (httpRequest.getHeaders() != null) {
|
||||
httpRequest.getHeaders().forEach((key, value) -> {
|
||||
requestBuilder.addHeader(key, value);
|
||||
});
|
||||
httpRequest.getHeaders().forEach(requestBuilder::addHeader);
|
||||
}
|
||||
|
||||
Request request = requestBuilder.build();
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ package com.microsoft.aspnet.signalr;
|
|||
import com.google.gson.Gson;
|
||||
|
||||
class HandshakeProtocol {
|
||||
private static Gson gson = new Gson();
|
||||
private static final Gson gson = new Gson();
|
||||
private static final String RECORD_SEPARATOR = "\u001e";
|
||||
|
||||
public static String createHandshakeRequestMessage(HandshakeRequestMessage message) {
|
||||
|
|
|
|||
|
|
@ -3,9 +3,9 @@
|
|||
|
||||
package com.microsoft.aspnet.signalr;
|
||||
|
||||
class HandshakeRequestMessage {
|
||||
String protocol;
|
||||
int version;
|
||||
final class HandshakeRequestMessage {
|
||||
private final String protocol;
|
||||
private final int version;
|
||||
|
||||
public HandshakeRequestMessage(String protocol, int version) {
|
||||
this.protocol = protocol;
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@
|
|||
|
||||
package com.microsoft.aspnet.signalr;
|
||||
|
||||
class HandshakeResponseMessage {
|
||||
public String error;
|
||||
final class HandshakeResponseMessage {
|
||||
private final String error;
|
||||
|
||||
public HandshakeResponseMessage() {
|
||||
this(null);
|
||||
|
|
@ -13,4 +13,8 @@ class HandshakeResponseMessage {
|
|||
public HandshakeResponseMessage(String error) {
|
||||
this.error = error;
|
||||
}
|
||||
|
||||
public String getHandshakeError() {
|
||||
return error;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ import java.util.concurrent.CompletableFuture;
|
|||
class HttpRequest {
|
||||
private String method;
|
||||
private String url;
|
||||
private Map<String, String> headers = new HashMap<>();
|
||||
private final Map<String, String> headers = new HashMap<>();
|
||||
|
||||
public void setMethod(String method) {
|
||||
this.method = method;
|
||||
|
|
@ -20,14 +20,12 @@ class HttpRequest {
|
|||
this.url = url;
|
||||
}
|
||||
|
||||
public void setHeader(String key, String value) {
|
||||
public void addHeader(String key, String value) {
|
||||
this.headers.put(key, value);
|
||||
}
|
||||
|
||||
public void setHeaders(Map<String, String> headers) {
|
||||
headers.forEach((key, value) -> {
|
||||
this.headers.put(key, value);
|
||||
});
|
||||
public void addHeaders(Map<String, String> headers) {
|
||||
this.headers.putAll(headers);
|
||||
}
|
||||
|
||||
public String getMethod() {
|
||||
|
|
@ -44,17 +42,16 @@ class HttpRequest {
|
|||
}
|
||||
|
||||
class HttpResponse {
|
||||
private int statusCode;
|
||||
private String statusText;
|
||||
private String content = null;
|
||||
private final int statusCode;
|
||||
private final String statusText;
|
||||
private final String content;
|
||||
|
||||
public HttpResponse(int statusCode) {
|
||||
this.statusCode = statusCode;
|
||||
this(statusCode, "");
|
||||
}
|
||||
|
||||
public HttpResponse(int statusCode, String statusText) {
|
||||
this.statusCode = statusCode;
|
||||
this.statusText = statusText;
|
||||
this(statusCode, statusText, "");
|
||||
}
|
||||
|
||||
public HttpResponse(int statusCode, String statusText, String content) {
|
||||
|
|
|
|||
|
|
@ -17,28 +17,28 @@ import java.util.function.Consumer;
|
|||
import java.util.function.Supplier;
|
||||
|
||||
public class HubConnection {
|
||||
private static final String RECORD_SEPARATOR = "\u001e";
|
||||
private static List<Class<?>> emptyArray = new ArrayList<>();
|
||||
private static int MAX_NEGOTIATE_ATTEMPTS = 100;
|
||||
|
||||
private String baseUrl;
|
||||
private Transport transport;
|
||||
private OnReceiveCallBack callback;
|
||||
private CallbackMap handlers = new CallbackMap();
|
||||
private HubProtocol protocol;
|
||||
private Boolean handshakeReceived = false;
|
||||
private static final String RECORD_SEPARATOR = "\u001e";
|
||||
private HubConnectionState hubConnectionState = HubConnectionState.DISCONNECTED;
|
||||
private Lock hubConnectionStateLock = new ReentrantLock();
|
||||
private Logger logger;
|
||||
private List<Consumer<Exception>> onClosedCallbackList;
|
||||
private boolean skipNegotiate = false;
|
||||
private boolean skipNegotiate;
|
||||
private Supplier<CompletableFuture<String>> accessTokenProvider;
|
||||
private Map<String, String> headers = new HashMap<>();
|
||||
private ConnectionState connectionState = null;
|
||||
private HttpClient httpClient;
|
||||
private String stopError;
|
||||
|
||||
private static ArrayList<Class<?>> emptyArray = new ArrayList<>();
|
||||
private static int MAX_NEGOTIATE_ATTEMPTS = 100;
|
||||
|
||||
public HubConnection(String url, HttpConnectionOptions options) {
|
||||
HubConnection(String url, HttpConnectionOptions options) {
|
||||
if (url == null || url.isEmpty()) {
|
||||
throw new IllegalArgumentException("A valid url is required.");
|
||||
}
|
||||
|
|
@ -75,10 +75,10 @@ public class HubConnection {
|
|||
int handshakeLength = payload.indexOf(RECORD_SEPARATOR) + 1;
|
||||
String handshakeResponseString = payload.substring(0, handshakeLength - 1);
|
||||
HandshakeResponseMessage handshakeResponse = HandshakeProtocol.parseHandshakeResponse(handshakeResponseString);
|
||||
if (handshakeResponse.error != null) {
|
||||
String errorMessage = "Error in handshake " + handshakeResponse.error;
|
||||
if (handshakeResponse.getHandshakeError() != null) {
|
||||
String errorMessage = "Error in handshake " + handshakeResponse.getHandshakeError();
|
||||
logger.log(LogLevel.Error, errorMessage);
|
||||
throw new HubException(errorMessage);
|
||||
throw new RuntimeException(errorMessage);
|
||||
}
|
||||
handshakeReceived = true;
|
||||
|
||||
|
|
@ -135,7 +135,7 @@ public class HubConnection {
|
|||
|
||||
private CompletableFuture<NegotiateResponse> handleNegotiate(String url) {
|
||||
HttpRequest request = new HttpRequest();
|
||||
request.setHeaders(this.headers);
|
||||
request.addHeaders(this.headers);
|
||||
|
||||
return httpClient.post(Negotiate.resolveNegotiateUrl(url), request).thenCompose((response) -> {
|
||||
if (response.getStatusCode() != 200) {
|
||||
|
|
@ -179,10 +179,9 @@ public class HubConnection {
|
|||
|
||||
/**
|
||||
* Starts a connection to the server.
|
||||
*
|
||||
* @throws Exception An error occurred while connecting.
|
||||
* @return A completable future that completes when the connection has been established.
|
||||
*/
|
||||
public CompletableFuture<Void> start() throws Exception {
|
||||
public CompletableFuture<Void> start() {
|
||||
if (hubConnectionState != HubConnectionState.DISCONNECTED) {
|
||||
return CompletableFuture.completedFuture(null);
|
||||
}
|
||||
|
|
@ -247,11 +246,7 @@ public class HubConnection {
|
|||
|
||||
if (response.getRedirectUrl() == null) {
|
||||
if (!response.getAvailableTransports().contains("WebSockets")) {
|
||||
try {
|
||||
throw new HubException("There were no compatible transports on the server.");
|
||||
} catch (HubException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
throw new RuntimeException("There were no compatible transports on the server.");
|
||||
}
|
||||
|
||||
String finalUrl = url;
|
||||
|
|
@ -272,6 +267,8 @@ public class HubConnection {
|
|||
|
||||
/**
|
||||
* Stops a connection to the server.
|
||||
* @param errorMessage An error message if the connected needs to be stopped because of an error.
|
||||
* @return A completable future that completes when the connection has been stopped.
|
||||
*/
|
||||
private CompletableFuture<Void> stop(String errorMessage) {
|
||||
hubConnectionStateLock.lock();
|
||||
|
|
@ -295,6 +292,7 @@ public class HubConnection {
|
|||
|
||||
/**
|
||||
* Stops a connection to the server.
|
||||
* @return A completable future that completes when the connection has been stopped.
|
||||
*/
|
||||
public CompletableFuture<Void> stop() {
|
||||
return stop(null);
|
||||
|
|
@ -414,9 +412,7 @@ public class HubConnection {
|
|||
*/
|
||||
public Subscription on(String target, Action callback) {
|
||||
ActionBase action = args -> callback.invoke();
|
||||
InvocationHandler handler = handlers.put(target, action, emptyArray);
|
||||
logger.log(LogLevel.Trace, "Registering handler for client method: %s.", target);
|
||||
return new Subscription(handlers, handler, target);
|
||||
return registerHandler(target, action);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -430,11 +426,8 @@ public class HubConnection {
|
|||
*/
|
||||
public <T1> Subscription on(String target, Action1<T1> callback, Class<T1> param1) {
|
||||
ActionBase action = params -> callback.invoke(param1.cast(params[0]));
|
||||
ArrayList<Class<?>> classes = new ArrayList<>(1);
|
||||
classes.add(param1);
|
||||
InvocationHandler handler = handlers.put(target, action, classes);
|
||||
logger.log(LogLevel.Trace, "Registering handler for client method: %s.", target);
|
||||
return new Subscription(handlers, handler, target);
|
||||
return registerHandler(target, action, param1);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -452,12 +445,7 @@ public class HubConnection {
|
|||
ActionBase action = params -> {
|
||||
callback.invoke(param1.cast(params[0]), param2.cast(params[1]));
|
||||
};
|
||||
ArrayList<Class<?>> classes = new ArrayList<>(2);
|
||||
classes.add(param1);
|
||||
classes.add(param2);
|
||||
InvocationHandler handler = handlers.put(target, action, classes);
|
||||
logger.log(LogLevel.Trace, "Registering handler for client method: %s.", target);
|
||||
return new Subscription(handlers, handler, target);
|
||||
return registerHandler(target, action, param1, param2);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -478,13 +466,7 @@ public class HubConnection {
|
|||
ActionBase action = params -> {
|
||||
callback.invoke(param1.cast(params[0]), param2.cast(params[1]), param3.cast(params[2]));
|
||||
};
|
||||
ArrayList<Class<?>> classes = new ArrayList<>(3);
|
||||
classes.add(param1);
|
||||
classes.add(param2);
|
||||
classes.add(param3);
|
||||
InvocationHandler handler = handlers.put(target, action, classes);
|
||||
logger.log(LogLevel.Trace, "Registering handler for client method: %s.", target);
|
||||
return new Subscription(handlers, handler, target);
|
||||
return registerHandler(target, action, param1, param2, param3);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -507,14 +489,7 @@ public class HubConnection {
|
|||
ActionBase action = params -> {
|
||||
callback.invoke(param1.cast(params[0]), param2.cast(params[1]), param3.cast(params[2]), param4.cast(params[3]));
|
||||
};
|
||||
ArrayList<Class<?>> classes = new ArrayList<>(4);
|
||||
classes.add(param1);
|
||||
classes.add(param2);
|
||||
classes.add(param3);
|
||||
classes.add(param4);
|
||||
InvocationHandler handler = handlers.put(target, action, classes);
|
||||
logger.log(LogLevel.Trace, "Registering handler for client method: %s.", target);
|
||||
return new Subscription(handlers, handler, target);
|
||||
return registerHandler(target, action, param1, param2, param3, param4);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -540,15 +515,7 @@ public class HubConnection {
|
|||
callback.invoke(param1.cast(params[0]), param2.cast(params[1]), param3.cast(params[2]), param4.cast(params[3]),
|
||||
param5.cast(params[4]));
|
||||
};
|
||||
ArrayList<Class<?>> classes = new ArrayList<>(5);
|
||||
classes.add(param1);
|
||||
classes.add(param2);
|
||||
classes.add(param3);
|
||||
classes.add(param4);
|
||||
classes.add(param5);
|
||||
InvocationHandler handler = handlers.put(target, action, classes);
|
||||
logger.log(LogLevel.Trace, "Registering handler for client method: %s.", target);
|
||||
return new Subscription(handlers, handler, target);
|
||||
return registerHandler(target, action, param1, param2, param3, param4, param5);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -576,16 +543,7 @@ public class HubConnection {
|
|||
callback.invoke(param1.cast(params[0]), param2.cast(params[1]), param3.cast(params[2]), param4.cast(params[3]),
|
||||
param5.cast(params[4]), param6.cast(params[5]));
|
||||
};
|
||||
ArrayList<Class<?>> classes = new ArrayList<>(6);
|
||||
classes.add(param1);
|
||||
classes.add(param2);
|
||||
classes.add(param3);
|
||||
classes.add(param4);
|
||||
classes.add(param5);
|
||||
classes.add(param6);
|
||||
InvocationHandler handler = handlers.put(target, action, classes);
|
||||
logger.log(LogLevel.Trace, "Registering handler for client method: %s.", target);
|
||||
return new Subscription(handlers, handler, target);
|
||||
return registerHandler(target, action, param1, param2, param3, param4, param5, param6);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -615,17 +573,7 @@ public class HubConnection {
|
|||
callback.invoke(param1.cast(params[0]), param2.cast(params[1]), param3.cast(params[2]), param4.cast(params[3]),
|
||||
param5.cast(params[4]), param6.cast(params[5]), param7.cast(params[6]));
|
||||
};
|
||||
ArrayList<Class<?>> classes = new ArrayList<>(7);
|
||||
classes.add(param1);
|
||||
classes.add(param2);
|
||||
classes.add(param3);
|
||||
classes.add(param4);
|
||||
classes.add(param5);
|
||||
classes.add(param6);
|
||||
classes.add(param7);
|
||||
InvocationHandler handler = handlers.put(target, action, classes);
|
||||
logger.log(LogLevel.Trace, "Registering handler for client method: %s.", target);
|
||||
return new Subscription(handlers, handler, target);
|
||||
return registerHandler(target, action, param1, param2, param3, param4, param5, param6, param7);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -657,25 +605,20 @@ public class HubConnection {
|
|||
callback.invoke(param1.cast(params[0]), param2.cast(params[1]), param3.cast(params[2]), param4.cast(params[3]),
|
||||
param5.cast(params[4]), param6.cast(params[5]), param7.cast(params[6]), param8.cast(params[7]));
|
||||
};
|
||||
ArrayList<Class<?>> classes = new ArrayList<>(8);
|
||||
classes.add(param1);
|
||||
classes.add(param2);
|
||||
classes.add(param3);
|
||||
classes.add(param4);
|
||||
classes.add(param5);
|
||||
classes.add(param6);
|
||||
classes.add(param7);
|
||||
classes.add(param8);
|
||||
InvocationHandler handler = handlers.put(target, action, classes);
|
||||
logger.log(LogLevel.Trace, "Registering handler for client method: %s.", target);
|
||||
return registerHandler(target, action, param1, param2, param3, param4, param5, param6, param7, param8);
|
||||
}
|
||||
|
||||
private Subscription registerHandler(String target, ActionBase action, Class<?>... types) {
|
||||
InvocationHandler handler = handlers.put(target, action, types);
|
||||
logger.log(LogLevel.Debug, "Registering handler for client method: '%s'.", target);
|
||||
return new Subscription(handlers, handler, target);
|
||||
}
|
||||
|
||||
private class ConnectionState implements InvocationBinder {
|
||||
private HubConnection connection;
|
||||
private AtomicInteger nextId = new AtomicInteger(0);
|
||||
private HashMap<String, InvocationRequest> pendingInvocations = new HashMap<>();
|
||||
private Lock lock = new ReentrantLock();
|
||||
private final class ConnectionState implements InvocationBinder {
|
||||
private final HubConnection connection;
|
||||
private final AtomicInteger nextId = new AtomicInteger(0);
|
||||
private final HashMap<String, InvocationRequest> pendingInvocations = new HashMap<>();
|
||||
private final Lock lock = new ReentrantLock();
|
||||
|
||||
public ConnectionState(HubConnection connection) {
|
||||
this.connection = connection;
|
||||
|
|
@ -755,7 +698,7 @@ public class HubConnection {
|
|||
return emptyArray;
|
||||
}
|
||||
|
||||
if (handlers.size() == 0) {
|
||||
if (handlers.isEmpty()) {
|
||||
throw new Exception(String.format("There are no callbacks registered for the method '%s'.", methodName));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,26 +9,23 @@ public class HubConnectionBuilder {
|
|||
private Logger logger;
|
||||
private HttpConnectionOptions options = null;
|
||||
|
||||
public HubConnectionBuilder withUrl(String url) {
|
||||
if (url == null || url.isEmpty()) {
|
||||
throw new IllegalArgumentException("A valid url is required.");
|
||||
}
|
||||
|
||||
private HubConnectionBuilder(String url) {
|
||||
this.url = url;
|
||||
return this;
|
||||
}
|
||||
|
||||
public HubConnectionBuilder withUrl(String url, Transport transport) {
|
||||
public static HubConnectionBuilder create(String url) {
|
||||
if (url == null || url.isEmpty()) {
|
||||
throw new IllegalArgumentException("A valid url is required.");
|
||||
}
|
||||
this.url = url;
|
||||
return new HubConnectionBuilder(url);
|
||||
}
|
||||
|
||||
public HubConnectionBuilder withTransport(Transport transport) {
|
||||
this.transport = transport;
|
||||
return this;
|
||||
}
|
||||
|
||||
public HubConnectionBuilder withUrl(String url, HttpConnectionOptions options) {
|
||||
this.url = url;
|
||||
public HubConnectionBuilder withOptions(HttpConnectionOptions options) {
|
||||
this.options = options;
|
||||
return this;
|
||||
}
|
||||
|
|
@ -44,9 +41,6 @@ public class HubConnectionBuilder {
|
|||
}
|
||||
|
||||
public HubConnection build() {
|
||||
if (this.url == null) {
|
||||
throw new RuntimeException("The 'HubConnectionBuilder.withUrl' method must be called before building the connection.");
|
||||
}
|
||||
if (options == null) {
|
||||
options = new HttpConnectionOptions();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,15 +3,16 @@
|
|||
|
||||
package com.microsoft.aspnet.signalr;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
class InvocationHandler {
|
||||
private List<Class<?>> classes;
|
||||
private ActionBase action;
|
||||
|
||||
InvocationHandler(ActionBase action, List<Class<?>> classes) {
|
||||
InvocationHandler(ActionBase action, Class<?>... classes) {
|
||||
this.action = action;
|
||||
this.classes = classes;
|
||||
this.classes = Arrays.asList(classes);
|
||||
}
|
||||
|
||||
public List<Class<?>> getClasses() {
|
||||
|
|
|
|||
|
|
@ -4,10 +4,10 @@
|
|||
package com.microsoft.aspnet.signalr;
|
||||
|
||||
class InvocationMessage extends HubMessage {
|
||||
int type = HubMessageType.INVOCATION.value;
|
||||
protected String invocationId;
|
||||
private String target;
|
||||
private Object[] arguments;
|
||||
private final int type = HubMessageType.INVOCATION.value;
|
||||
private final String invocationId;
|
||||
private final String target;
|
||||
private final Object[] arguments;
|
||||
|
||||
public InvocationMessage(String invocationId, String target, Object[] args) {
|
||||
this.invocationId = invocationId;
|
||||
|
|
@ -23,18 +23,10 @@ class InvocationMessage extends HubMessage {
|
|||
return target;
|
||||
}
|
||||
|
||||
public void setTarget(String target) {
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
public Object[] getArguments() {
|
||||
return arguments;
|
||||
}
|
||||
|
||||
public void setArguments(Object[] arguments) {
|
||||
this.arguments = arguments;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HubMessageType getMessageType() {
|
||||
return HubMessageType.INVOCATION;
|
||||
|
|
|
|||
|
|
@ -20,13 +20,13 @@ class HandshakeProtocolTest {
|
|||
public void VerifyParseEmptyHandshakeResponseMessage() {
|
||||
String emptyHandshakeResponse = "{}";
|
||||
HandshakeResponseMessage hsr = HandshakeProtocol.parseHandshakeResponse(emptyHandshakeResponse);
|
||||
assertNull(hsr.error);
|
||||
assertNull(hsr.getHandshakeError());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void VerifyParseHandshakeResponseMessage() {
|
||||
String handshakeResponseWithError = "{\"error\": \"Requested protocol \'messagepack\' is not available.\"}";
|
||||
HandshakeResponseMessage hsr = HandshakeProtocol.parseHandshakeResponse(handshakeResponseWithError);
|
||||
assertEquals(hsr.error, "Requested protocol 'messagepack' is not available.");
|
||||
assertEquals(hsr.getHandshakeError(), "Requested protocol 'messagepack' is not available.");
|
||||
}
|
||||
}
|
||||
|
|
@ -9,24 +9,15 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
|||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class HubConnectionBuilderTest {
|
||||
@Test
|
||||
public void callingBuildWithoutCallingWithUrlThrows() {
|
||||
HubConnectionBuilder builder = new HubConnectionBuilder();
|
||||
Throwable exception = assertThrows(RuntimeException.class, () -> builder.build());
|
||||
assertEquals("The \'HubConnectionBuilder.withUrl\' method must be called before building the connection.", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void passingInNullToWithUrlThrows() {
|
||||
HubConnectionBuilder builder = new HubConnectionBuilder();
|
||||
Throwable exception = assertThrows(IllegalArgumentException.class, () -> builder.withUrl(null));
|
||||
Throwable exception = assertThrows(IllegalArgumentException.class, () -> HubConnectionBuilder.create(null));
|
||||
assertEquals("A valid url is required.", exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void passingInEmptyStringToWihtUrlThrows() {
|
||||
HubConnectionBuilder builder = new HubConnectionBuilder();
|
||||
Throwable exception = assertThrows(IllegalArgumentException.class, () -> builder.withUrl(""));
|
||||
Throwable exception = assertThrows(IllegalArgumentException.class, () -> HubConnectionBuilder.create(""));
|
||||
assertEquals("A valid url is required.", exception.getMessage());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -63,9 +63,9 @@ class HubConnectionTest {
|
|||
Transport mockTransport = new MockTransport();
|
||||
HttpConnectionOptions options = new HttpConnectionOptions();
|
||||
options.setTransport(mockTransport);
|
||||
options.setLoglevel(LogLevel.Information);
|
||||
options.setSkipNegotiate(true);
|
||||
HubConnection hubConnection = new HubConnectionBuilder().withUrl("http://www.example.com", options).build();
|
||||
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
|
||||
|
||||
hubConnection.start();
|
||||
assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState());
|
||||
|
||||
|
|
@ -89,12 +89,12 @@ class HubConnectionTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void hubConnectionReceiveHandshakeResponseWithError() throws Exception {
|
||||
public void hubConnectionReceiveHandshakeResponseWithError() {
|
||||
MockTransport mockTransport = new MockTransport();
|
||||
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
|
||||
|
||||
hubConnection.start();
|
||||
Throwable exception = assertThrows(HubException.class, () -> mockTransport.receiveMessage("{\"error\":\"Requested protocol 'messagepack' is not available.\"}" + RECORD_SEPARATOR));
|
||||
Throwable exception = assertThrows(RuntimeException.class, () -> mockTransport.receiveMessage("{\"error\":\"Requested protocol 'messagepack' is not available.\"}" + RECORD_SEPARATOR));
|
||||
assertEquals("Error in handshake Requested protocol 'messagepack' is not available.", exception.getMessage());
|
||||
}
|
||||
|
||||
|
|
@ -912,9 +912,10 @@ class HubConnectionTest {
|
|||
|
||||
HttpConnectionOptions options = new HttpConnectionOptions();
|
||||
options.setHttpClient(client);
|
||||
HubConnection hubConnection = new HubConnectionBuilder()
|
||||
.withUrl("http://example.com", options)
|
||||
.build();
|
||||
HubConnection hubConnection = HubConnectionBuilder
|
||||
.create("http://example.com")
|
||||
.withOptions(options)
|
||||
.build();
|
||||
|
||||
try {
|
||||
hubConnection.start().get(1000, TimeUnit.MILLISECONDS);
|
||||
|
|
@ -932,8 +933,9 @@ class HubConnectionTest {
|
|||
|
||||
HttpConnectionOptions options = new HttpConnectionOptions();
|
||||
options.setHttpClient(client);
|
||||
HubConnection hubConnection = new HubConnectionBuilder()
|
||||
.withUrl("http://example.com", options)
|
||||
HubConnection hubConnection = HubConnectionBuilder
|
||||
.create("http://example.com")
|
||||
.withOptions(options)
|
||||
.build();
|
||||
|
||||
ExecutionException exception = assertThrows(ExecutionException.class, () -> hubConnection.start().get(1000, TimeUnit.MILLISECONDS));
|
||||
|
|
@ -951,9 +953,10 @@ class HubConnectionTest {
|
|||
HttpConnectionOptions options = new HttpConnectionOptions();
|
||||
options.setTransport(transport);
|
||||
options.setHttpClient(client);
|
||||
HubConnection hubConnection = new HubConnectionBuilder()
|
||||
.withUrl("http://example.com", options)
|
||||
.build();
|
||||
HubConnection hubConnection = HubConnectionBuilder
|
||||
.create("http://example.com")
|
||||
.withOptions(options)
|
||||
.build();
|
||||
|
||||
hubConnection.start().get(1000, TimeUnit.MILLISECONDS);
|
||||
|
||||
|
|
@ -971,9 +974,10 @@ class HubConnectionTest {
|
|||
HttpConnectionOptions options = new HttpConnectionOptions();
|
||||
options.setTransport(transport);
|
||||
options.setHttpClient(client);
|
||||
HubConnection hubConnection = new HubConnectionBuilder()
|
||||
.withUrl("http://example.com", options)
|
||||
.build();
|
||||
HubConnection hubConnection = HubConnectionBuilder
|
||||
.create("http://example.com")
|
||||
.withOptions(options)
|
||||
.build();
|
||||
|
||||
ExecutionException exception = assertThrows(ExecutionException.class, () -> hubConnection.start().get(1000, TimeUnit.MILLISECONDS));
|
||||
assertEquals("Test error.", exception.getCause().getMessage());
|
||||
|
|
@ -991,9 +995,10 @@ class HubConnectionTest {
|
|||
HttpConnectionOptions options = new HttpConnectionOptions();
|
||||
options.setTransport(transport);
|
||||
options.setHttpClient(client);
|
||||
HubConnection hubConnection = new HubConnectionBuilder()
|
||||
.withUrl("http://example.com", options)
|
||||
.build();
|
||||
HubConnection hubConnection = HubConnectionBuilder
|
||||
.create("http://example.com")
|
||||
.withOptions(options)
|
||||
.build();
|
||||
|
||||
hubConnection.start().get(1000, TimeUnit.MILLISECONDS);
|
||||
assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState());
|
||||
|
|
@ -1018,7 +1023,10 @@ class HubConnectionTest {
|
|||
options.setTransport(transport);
|
||||
options.setHttpClient(client);
|
||||
options.setAccessTokenProvider(() -> CompletableFuture.completedFuture("secretToken"));
|
||||
HubConnection hubConnection = new HubConnectionBuilder().withUrl("http://example.com", options).build();
|
||||
HubConnection hubConnection = HubConnectionBuilder
|
||||
.create("http://example.com")
|
||||
.withOptions(options)
|
||||
.build();
|
||||
|
||||
hubConnection.start().get(1000, TimeUnit.MILLISECONDS);
|
||||
assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState());
|
||||
|
|
@ -1044,7 +1052,10 @@ class HubConnectionTest {
|
|||
options.setTransport(transport);
|
||||
options.setHttpClient(client);
|
||||
options.setAccessTokenProvider(() -> CompletableFuture.completedFuture("secretToken"));
|
||||
HubConnection hubConnection = new HubConnectionBuilder().withUrl("http://example.com", options).build();
|
||||
HubConnection hubConnection = HubConnectionBuilder
|
||||
.create("http://example.com")
|
||||
.withOptions(options)
|
||||
.build();
|
||||
|
||||
hubConnection.start().get(1000, TimeUnit.MILLISECONDS);
|
||||
assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState());
|
||||
|
|
@ -1059,7 +1070,10 @@ class HubConnectionTest {
|
|||
HttpConnectionOptions options = new HttpConnectionOptions();
|
||||
options.setTransport(transport);
|
||||
options.setSkipNegotiate(true);
|
||||
HubConnection hubConnection = new HubConnectionBuilder().withUrl("http://example.com", options).build();
|
||||
HubConnection hubConnection = HubConnectionBuilder
|
||||
.create("http://example.com")
|
||||
.withOptions(options)
|
||||
.build();
|
||||
|
||||
hubConnection.start().get(1000, TimeUnit.MILLISECONDS);
|
||||
assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState());
|
||||
|
|
@ -1083,7 +1097,10 @@ class HubConnectionTest {
|
|||
HttpConnectionOptions options = new HttpConnectionOptions();
|
||||
options.setTransport(mockTransport);
|
||||
options.setHttpClient(client);
|
||||
HubConnection hubConnection = new HubConnectionBuilder().withUrl("http://example.com", options).build();
|
||||
HubConnection hubConnection = HubConnectionBuilder
|
||||
.create("http://example.com")
|
||||
.withOptions(options)
|
||||
.build();
|
||||
|
||||
hubConnection.start().get(1000, TimeUnit.MILLISECONDS);
|
||||
assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState());
|
||||
|
|
@ -1106,7 +1123,10 @@ class HubConnectionTest {
|
|||
HttpConnectionOptions options = new HttpConnectionOptions();
|
||||
options.setTransport(transport);
|
||||
options.setHttpClient(client);
|
||||
HubConnection hubConnection = new HubConnectionBuilder().withUrl("http://example.com", options).build();
|
||||
HubConnection hubConnection = HubConnectionBuilder
|
||||
.create("http://example.com")
|
||||
.withOptions(options)
|
||||
.build();
|
||||
|
||||
ExecutionException exception = assertThrows(ExecutionException.class, () -> hubConnection.start().get(1000, TimeUnit.MILLISECONDS));
|
||||
assertEquals("Unexpected status code returned from negotiate: 500 Internal server error.", exception.getCause().getMessage());
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ class TestUtils {
|
|||
options.setLogger(logger);
|
||||
options.setSkipNegotiate(skipNegotiate);
|
||||
options.setHttpClient(client);
|
||||
HubConnectionBuilder builder = new HubConnectionBuilder();
|
||||
return builder.withUrl(url, options).build();
|
||||
HubConnectionBuilder builder = HubConnectionBuilder.create(url);
|
||||
return builder.withOptions(options).build();
|
||||
}
|
||||
}
|
||||
|
|
@ -18,8 +18,7 @@ public class Chat {
|
|||
System.out.print("Enter your name:");
|
||||
String enteredName = reader.nextLine();
|
||||
|
||||
HubConnection hubConnection = new HubConnectionBuilder()
|
||||
.withUrl(input)
|
||||
HubConnection hubConnection = HubConnectionBuilder.create(input)
|
||||
.configureLogging(LogLevel.Information).build();
|
||||
|
||||
hubConnection.on("Send", (name, message) -> {
|
||||
|
|
|
|||
Loading…
Reference in New Issue