Skip to content

Commit

Permalink
chore: expand the webpack example
Browse files Browse the repository at this point in the history
  • Loading branch information
viglucci committed Aug 2, 2024
1 parent 4a08254 commit 2849ad9
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
38 changes: 38 additions & 0 deletions packages/rsocket-examples/src/webpack/browser-bundle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,41 @@ __index.html__
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.

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.

## Run the server

**Open a terminal:**

Open a terminal in the `simple/server` directory one level up from this README.

**Install dependencies:**

```bash
npm install
```

**Run the server:**

```bash
npm run start
```

## Run the client

**Open a terminal in this folder and install dependencies:**

```bash
npm install
```

**Run the NPM server script:**

```
npm run serve
```

The above script will run the webpack dev server, which will first compile the "app" and then host the index.html.

**Open in browser:**

Visit [localhost:9000](http://localhost:9000).
35 changes: 32 additions & 3 deletions packages/rsocket-examples/src/webpack/browser-bundle/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,18 @@ <h1>RSocket Webpack Example</h1>
var state = 'CONNECTING';
var outputDiv = document.querySelector("#output");
var _rsocket = null;
var erorrColor = '#eb4034';
var infoColor = '#348CEBFF';
var messageColor = '#2ccd20';

function sendMessage(message) {
if (state !== 'CONNECTED') return;
if (state !== 'CONNECTED') {
const div = document.createElement("div");
div.textContent = `[${new Date().toISOString()}] not connected. cannot send messages!`;
div.style.color = erorrColor;
outputDiv.appendChild(div);
return;
}
const bufferData = rsocket.createBuffer(message || "");
_rsocket.requestResponse(
{
Expand All @@ -29,9 +38,10 @@ <h1>RSocket Webpack Example</h1>
},
onNext: function(payload, isComplete) {
const div = document.createElement("div");
div.textContent = `[${new Date().toISOString()}] payload[data: ${
div.textContent = `[${new Date().toISOString()}] received: payload[data: ${
payload.data
}; metadata: ${payload.metadata}]|${isComplete}`;
div.style.color = messageColor;
outputDiv.appendChild(div);
},
onComplete: function() {
Expand All @@ -48,7 +58,17 @@ <h1>RSocket Webpack Example</h1>
sendButton.addEventListener("click", function() {
var input = document.querySelector("#input-field");
var value = input.value;
if (!value.length) return;
if (!value.length) {
const div = document.createElement("div");
div.textContent = `[${new Date().toISOString()}] please include a message!`;
div.style.color = erorrColor;
outputDiv.appendChild(div);
return;
}
const div = document.createElement("div");
div.textContent = `[${new Date().toISOString()}] sending: ${value}`;
div.style.color = infoColor;
outputDiv.appendChild(div);
sendMessage(value);
});

Expand All @@ -59,14 +79,23 @@ <h1>RSocket Webpack Example</h1>
.then(function (_r) {
state = 'CONNECTED';
_rsocket = _r;
const div = document.createElement("div");
div.textContent = `[${new Date().toISOString()}] connected!`;
div.style.color = infoColor;
outputDiv.appendChild(div);
})
.catch(function (err) {
const erorrMessage = err?.message || "failed to connect to rsocket! check the console for more details.";
if (err) {
console.error("failed to connect rsocket: " + err.message)
}
else {
console.error("failed to connect rsocket!")
}
const div = document.createElement("div");
div.textContent = `[${new Date().toISOString()}] ${erorrMessage}`;
div.style.color = erorrColor;
outputDiv.appendChild(div);
});
})();
</script>
Expand Down
4 changes: 2 additions & 2 deletions packages/rsocket-examples/src/webpack/simple/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ const server = new RSocketServer({
() =>
responderStream.onNext(
{
data: Buffer.concat([Buffer.from("Echo: "), payload.data]),
data: Buffer.concat([Buffer.from("ECHO: "), payload.data]),
},
true
),
1000
100
);
return {
cancel: () => {
Expand Down

0 comments on commit 2849ad9

Please sign in to comment.