Skip to content

Commit 229bc12

Browse files
authored
Formatting urls when using the WebSocketsTransport (#2571)
1 parent b7f666e commit 229bc12

File tree

2 files changed

+67
-12
lines changed

2 files changed

+67
-12
lines changed

clients/java/signalr/src/main/java/WebSocketTransport.java

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,50 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
import com.google.gson.Gson;
54
import org.java_websocket.client.WebSocketClient;
65
import org.java_websocket.handshake.ServerHandshake;
76

87
import java.net.URI;
98
import java.net.URISyntaxException;
109

1110
public class WebSocketTransport implements Transport {
12-
private WebSocketClient _webSocket;
11+
private WebSocketClient webSocketClient;
1312
private OnReceiveCallBack onReceiveCallBack;
14-
private URI _url;
13+
private URI url;
14+
15+
private static final String HTTP = "http";
16+
private static final String HTTPS = "https";
17+
private static final String WS = "ws";
18+
private static final String WSS = "wss";
1519

1620
public WebSocketTransport(String url) throws URISyntaxException {
17-
// To Do: Format the incoming URL for a websocket connection.
18-
_url = new URI(url);
19-
_webSocket = createWebSocket();
21+
this.url = formatUrl(url);
22+
}
23+
24+
public URI getUrl(){
25+
return url;
26+
}
27+
28+
private URI formatUrl(String url) throws URISyntaxException {
29+
if(url.startsWith(HTTPS)){
30+
url = WSS + url.substring(HTTPS.length());
31+
}
32+
else if(url.startsWith(HTTP)){
33+
url = WS + url.substring(HTTP.length());
34+
}
35+
return new URI(url);
2036
}
2137

2238
@Override
2339
public void start() throws InterruptedException {
24-
_webSocket.connectBlocking();
25-
_webSocket.send((new DefaultJsonProtocolHandShakeMessage()).createHandshakeMessage());
40+
webSocketClient = createWebSocket();
41+
webSocketClient.connectBlocking();
42+
webSocketClient.send((new DefaultJsonProtocolHandShakeMessage()).createHandshakeMessage());
2643
}
2744

2845
@Override
2946
public void send(String message) {
30-
_webSocket.send(message);
47+
webSocketClient.send(message);
3148
}
3249

3350
@Override
@@ -42,14 +59,14 @@ public void onReceive(String message) throws Exception {
4259

4360
@Override
4461
public void stop() {
45-
_webSocket.closeConnection(0, "HubConnection Stopped");
62+
webSocketClient.closeConnection(0, "HubConnection Stopped");
4663
}
4764

4865
private WebSocketClient createWebSocket() {
49-
return new WebSocketClient(_url) {
66+
return new WebSocketClient(url) {
5067
@Override
5168
public void onOpen(ServerHandshake handshakedata) {
52-
System.out.println("Connected to " + _url);
69+
System.out.println("Connected to " + url);
5370
}
5471

5572
@Override
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.junit.runners.Parameterized;
7+
8+
import java.net.URISyntaxException;
9+
import java.util.Arrays;
10+
import java.util.Collection;
11+
12+
import static org.junit.Assert.*;
13+
14+
@RunWith(Parameterized.class)
15+
public class WebSocketTransportTest {
16+
private String url;
17+
private String expectedUrl;
18+
19+
public WebSocketTransportTest(String url, String expectedProtocol){
20+
this.url = url;
21+
this.expectedUrl = expectedProtocol;
22+
}
23+
24+
@Parameterized.Parameters
25+
public static Collection protocols(){
26+
return Arrays.asList(new String[][] {
27+
{"http://example.com", "ws://example.com"},
28+
{"https://example.com", "wss://example.com"},
29+
{"ws://example.com", "ws://example.com"},
30+
{"wss://example.com", "wss://example.com"}});
31+
}
32+
33+
@Test
34+
public void checkWebsocketUrlProtocol() throws URISyntaxException {
35+
WebSocketTransport webSocketTransport = new WebSocketTransport(this.url);
36+
assertEquals(this.expectedUrl, webSocketTransport.getUrl().toString());
37+
}
38+
}

0 commit comments

Comments
 (0)