Skip to content

Commit 04efb64

Browse files
committed
chore: expand the webpack example (#282)
* chore: expand the webpack example --------- Signed-off-by: Kevin Viglucci <[email protected]>
1 parent a89b9bf commit 04efb64

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-5
lines changed

packages/rsocket-examples/src/webpack/browser-bundle/README.md

+38
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,41 @@ __index.html__
2626
Note: `index.html` does not show how to load the built `rsocket.js` file as that will be up to you/your implementation to decide.
2727

2828
Note: when running the `serve` npm script webpack will automatically host the `index.html` file and inject the `rsocket.js` script into the `<head/>` block.
29+
30+
## Run the server
31+
32+
**Open a terminal:**
33+
34+
Open a terminal in the `simple/server` directory one level up from this README.
35+
36+
**Install dependencies:**
37+
38+
```bash
39+
npm install
40+
```
41+
42+
**Run the server:**
43+
44+
```bash
45+
npm run start
46+
```
47+
48+
## Run the client
49+
50+
**Open a terminal in this folder and install dependencies:**
51+
52+
```bash
53+
npm install
54+
```
55+
56+
**Run the NPM server script:**
57+
58+
```
59+
npm run serve
60+
```
61+
62+
The above script will run the webpack dev server, which will first compile the "app" and then host the index.html.
63+
64+
**Open in browser:**
65+
66+
Visit [localhost:9000](http://localhost:9000).

packages/rsocket-examples/src/webpack/browser-bundle/index.html

+32-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,18 @@ <h1>RSocket Webpack Example</h1>
1515
var state = 'CONNECTING';
1616
var outputDiv = document.querySelector("#output");
1717
var _rsocket = null;
18+
var errorColor = '#eb4034';
19+
var infoColor = '#348CEBFF';
20+
var messageColor = '#2ccd20';
1821

1922
function sendMessage(message) {
20-
if (state !== 'CONNECTED') return;
23+
if (state !== 'CONNECTED') {
24+
const div = document.createElement("div");
25+
div.textContent = `[${new Date().toISOString()}] not connected. cannot send messages!`;
26+
div.style.color = errorColor;
27+
outputDiv.appendChild(div);
28+
return;
29+
}
2130
const bufferData = rsocket.createBuffer(message || "");
2231
_rsocket.requestResponse(
2332
{
@@ -29,9 +38,10 @@ <h1>RSocket Webpack Example</h1>
2938
},
3039
onNext: function(payload, isComplete) {
3140
const div = document.createElement("div");
32-
div.textContent = `[${new Date().toISOString()}] payload[data: ${
41+
div.textContent = `[${new Date().toISOString()}] received: payload[data: ${
3342
payload.data
3443
}; metadata: ${payload.metadata}]|${isComplete}`;
44+
div.style.color = messageColor;
3545
outputDiv.appendChild(div);
3646
},
3747
onComplete: function() {
@@ -48,7 +58,17 @@ <h1>RSocket Webpack Example</h1>
4858
sendButton.addEventListener("click", function() {
4959
var input = document.querySelector("#input-field");
5060
var value = input.value;
51-
if (!value.length) return;
61+
if (!value.length) {
62+
const div = document.createElement("div");
63+
div.textContent = `[${new Date().toISOString()}] please include a message!`;
64+
div.style.color = errorColor;
65+
outputDiv.appendChild(div);
66+
return;
67+
}
68+
const div = document.createElement("div");
69+
div.textContent = `[${new Date().toISOString()}] sending: ${value}`;
70+
div.style.color = infoColor;
71+
outputDiv.appendChild(div);
5272
sendMessage(value);
5373
});
5474

@@ -59,14 +79,23 @@ <h1>RSocket Webpack Example</h1>
5979
.then(function (_r) {
6080
state = 'CONNECTED';
6181
_rsocket = _r;
82+
const div = document.createElement("div");
83+
div.textContent = `[${new Date().toISOString()}] connected!`;
84+
div.style.color = infoColor;
85+
outputDiv.appendChild(div);
6286
})
6387
.catch(function (err) {
88+
const errorMessage = err?.message || "failed to connect to rsocket! check the console for more details.";
6489
if (err) {
6590
console.error("failed to connect rsocket: " + err.message)
6691
}
6792
else {
6893
console.error("failed to connect rsocket!")
6994
}
95+
const div = document.createElement("div");
96+
div.textContent = `[${new Date().toISOString()}] ${errorMessage}`;
97+
div.style.color = errorColor;
98+
outputDiv.appendChild(div);
7099
});
71100
})();
72101
</script>

packages/rsocket-examples/src/webpack/simple/server/server.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ const server = new RSocketServer({
1919
() =>
2020
responderStream.onNext(
2121
{
22-
data: Buffer.concat([Buffer.from("Echo: "), payload.data]),
22+
data: Buffer.concat([Buffer.from("ECHO: "), payload.data]),
2323
},
2424
true
2525
),
26-
1000
26+
100
2727
);
2828
return {
2929
cancel: () => {

0 commit comments

Comments
 (0)