You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -236,7 +276,71 @@ Now let's create a web application using the `json.webpubsub.azure.v1` subprotoc
236
276
server.serve_forever()
237
277
238
278
```
279
+
280
+
# [Java](#tab/java)
281
+
282
+
Let's navigate to the */src/main/java/com/webpubsub/tutorial* directory, open the *App.java* file in your editor, use `Javalin.create` to serve static files:
Depending on your setup, you might need to explicitly set the language level to Java 8. This can be done in the pom.xml. Add the following snippet:
330
+
```xml
331
+
<build>
332
+
<plugins>
333
+
<plugin>
334
+
<artifactId>maven-compiler-plugin</artifactId>
335
+
<version>3.8.0</version>
336
+
<configuration>
337
+
<source>1.8</source>
338
+
<target>1.8</target>
339
+
</configuration>
340
+
</plugin>
341
+
</plugins>
342
+
</build>
343
+
```
240
344
---
241
345
242
346
3. Create the web page
@@ -252,6 +356,9 @@ Now let's create a web application using the `json.webpubsub.azure.v1` subprotoc
252
356
253
357
Create an HTML page with below content and save it as `public/index.html`:
254
358
359
+
# [Java](#tab/java)
360
+
361
+
Create an HTML page with below content and save it to */src/main/resources/public/index.html*:
255
362
---
256
363
257
364
```html
@@ -309,6 +416,14 @@ Now let's create a web application using the `json.webpubsub.azure.v1` subprotoc
309
416
```bash
310
417
python server.py "<connection-string>"
311
418
```
419
+
420
+
# [Java](#tab/java)
421
+
422
+
Now run the below command, replacing `<connection-string>` with the **ConnectionString** fetched in [previous step](#get-the-connectionstring-for-future-use), and open http://localhost:8080 in browser:
If you are using Chrome, you can press F12 or right-click -> **Inspect** -> **Developer Tools**, and select the **Network** tab. Load the web page, and you can see the WebSocket connection is established. Click to inspect the WebSocket connection, you can see below `connected` event message is received in client. You can see that you can get the `connectionId` generated for this client.
@@ -480,6 +595,123 @@ This will be useful if you want to stream a large amount of data to other client
480
595
481
596
The code above creates a WebSocket connection to the service and then whenever it receives some data it uses `ws.send()` to publish the data. In order to publish to others, you just need to set`type` to `sendToGroup` and specify a group name in the message.
482
597
598
+
# [Java](#tab/java)
599
+
600
+
1. Let's use another terminal and go back to the root folder to create a streaming console app `logstream-streaming` and switch into the *logstream-streaming* folder:
3. Now let's use WebSocket to connect to the service. Let's navigate to the */src/main/java/com/webpubsub/quickstart* directory, open the *App.java* file in your editor, and replace code with the below:
623
+
```java
624
+
package com.webpubsub.quickstart;
625
+
626
+
import java.io.BufferedReader;
627
+
import java.io.IOException;
628
+
import java.io.InputStreamReader;
629
+
import java.net.URI;
630
+
import java.net.http.HttpClient;
631
+
import java.net.http.HttpRequest;
632
+
import java.net.http.HttpResponse;
633
+
import java.net.http.WebSocket;
634
+
import java.util.concurrent.CompletionStage;
635
+
636
+
import com.google.gson.Gson;
637
+
638
+
public class App
639
+
{
640
+
public static void main( String[] args ) throws IOException, InterruptedException
You can see there is a new concept "group" here. Group is logical concept in a hub where you can publish message to a group of connections. In a hub, you can have multiple groups and one client can subscribe to multiple groups at the same time. When using subprotocol, you can only publish to a group instead of broadcasting to the whole hub. For details about the terms, check the [basic concepts](./key-concepts.md).
@@ -545,8 +777,19 @@ This will be useful if you want to stream a large amount of data to other client
0 commit comments