Throw if the Websocket cant connect in Java client(#2801)
This commit is contained in:
parent
c6aaa13db9
commit
85c934dcdd
|
|
@ -4,7 +4,7 @@
|
||||||
package com.microsoft.aspnet.signalr;
|
package com.microsoft.aspnet.signalr;
|
||||||
|
|
||||||
public interface Transport {
|
public interface Transport {
|
||||||
void start() throws InterruptedException;
|
void start() throws Exception;
|
||||||
void send(String message) throws Exception;
|
void send(String message) throws Exception;
|
||||||
void setOnReceive(OnReceiveCallBack callback);
|
void setOnReceive(OnReceiveCallBack callback);
|
||||||
void onReceive(String message) throws Exception;
|
void onReceive(String message) throws Exception;
|
||||||
|
|
|
||||||
|
|
@ -39,10 +39,14 @@ public class WebSocketTransport implements Transport {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start() throws InterruptedException {
|
public void start() throws Exception {
|
||||||
logger.log(LogLevel.Debug, "Starting Websocket connection");
|
logger.log(LogLevel.Debug, "Starting Websocket connection.");
|
||||||
webSocketClient = createWebSocket();
|
webSocketClient = createWebSocket();
|
||||||
webSocketClient.connectBlocking();
|
if (!webSocketClient.connectBlocking()) {
|
||||||
|
String errorMessage = "There was an error starting the Websockets transport.";
|
||||||
|
logger.log(LogLevel.Debug, errorMessage);
|
||||||
|
throw new Exception(errorMessage);
|
||||||
|
}
|
||||||
logger.log(LogLevel.Information, "WebSocket transport connected to: %s", webSocketClient.getURI());
|
logger.log(LogLevel.Information, "WebSocket transport connected to: %s", webSocketClient.getURI());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,38 +2,22 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
import com.microsoft.aspnet.signalr.NullLogger;
|
import com.microsoft.aspnet.signalr.NullLogger;
|
||||||
|
import com.microsoft.aspnet.signalr.Transport;
|
||||||
import com.microsoft.aspnet.signalr.WebSocketTransport;
|
import com.microsoft.aspnet.signalr.WebSocketTransport;
|
||||||
import java.net.URISyntaxException;
|
import org.junit.Rule;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collection;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.rules.ExpectedException;
|
||||||
import org.junit.runners.Parameterized;
|
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
@RunWith(Parameterized.class)
|
|
||||||
public class WebSocketTransportTest {
|
public class WebSocketTransportTest {
|
||||||
private String url;
|
|
||||||
private String expectedUrl;
|
|
||||||
|
|
||||||
public WebSocketTransportTest(String url, String expectedProtocol){
|
@Rule
|
||||||
this.url = url;
|
public ExpectedException expectedEx = ExpectedException.none();
|
||||||
this.expectedUrl = expectedProtocol;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Parameterized.Parameters
|
|
||||||
public static Collection protocols(){
|
|
||||||
return Arrays.asList(new String[][] {
|
|
||||||
{"http://example.com", "ws://example.com"},
|
|
||||||
{"https://example.com", "wss://example.com"},
|
|
||||||
{"ws://example.com", "ws://example.com"},
|
|
||||||
{"wss://example.com", "wss://example.com"}});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkWebsocketUrlProtocol() throws URISyntaxException {
|
public void WebsocketThrowsIfItCantConnect() throws Exception {
|
||||||
WebSocketTransport webSocketTransport = new WebSocketTransport(this.url, new NullLogger());
|
expectedEx.expect(Exception.class);
|
||||||
assertEquals(this.expectedUrl, webSocketTransport.getUrl().toString());
|
expectedEx.expectMessage("There was an error starting the Websockets transport");
|
||||||
|
Transport transport = new WebSocketTransport("www.notarealurl12345.fake", new NullLogger());
|
||||||
|
transport.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
import com.microsoft.aspnet.signalr.NullLogger;
|
||||||
|
import com.microsoft.aspnet.signalr.WebSocketTransport;
|
||||||
|
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.junit.runners.Parameterized;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
@RunWith(Parameterized.class)
|
||||||
|
public class WebSocketTransportUrlFormatTest {
|
||||||
|
private String url;
|
||||||
|
private String expectedUrl;
|
||||||
|
|
||||||
|
public WebSocketTransportUrlFormatTest(String url, String expectedProtocol) {
|
||||||
|
this.url = url;
|
||||||
|
this.expectedUrl = expectedProtocol;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Parameterized.Parameters
|
||||||
|
public static Collection protocols() {
|
||||||
|
return Arrays.asList(new String[][]{
|
||||||
|
{"http://example.com", "ws://example.com"},
|
||||||
|
{"https://example.com", "wss://example.com"},
|
||||||
|
{"ws://example.com", "ws://example.com"},
|
||||||
|
{"wss://example.com", "wss://example.com"}});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void checkWebsocketUrlProtocol() throws URISyntaxException {
|
||||||
|
WebSocketTransport webSocketTransport = new WebSocketTransport(this.url, new NullLogger());
|
||||||
|
assertEquals(this.expectedUrl, webSocketTransport.getUrl().toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue