-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add simple webpack example (#246)
Signed-off-by: Kevin Viglucci <[email protected]>
- Loading branch information
Showing
6 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
packages/rsocket-examples/src/webpack/simple/client/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>RSocket Webpack Example</title> | ||
</head> | ||
<body> | ||
<h1>RSocket Webpack Example</h1> | ||
<div id="output"></div> | ||
</body> | ||
</html> |
37 changes: 37 additions & 0 deletions
37
packages/rsocket-examples/src/webpack/simple/client/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { RSocketConnector } from "rsocket-core"; | ||
import { WebsocketClientTransport } from "rsocket-websocket-client"; | ||
|
||
(async () => { | ||
const outputDiv = document.querySelector("#output"); | ||
|
||
const connector = new RSocketConnector({ | ||
transport: new WebsocketClientTransport({ | ||
url: "ws://localhost:9090", | ||
wsCreator: (url) => new WebSocket(url), | ||
}), | ||
}); | ||
|
||
const rsocket = await connector.connect(); | ||
|
||
rsocket.requestResponse( | ||
{ | ||
data: Buffer.from("Hello World"), | ||
}, | ||
{ | ||
onError: (e) => reject(e), | ||
onNext: (payload, isComplete) => { | ||
const div = document.createElement("div"); | ||
div.textContent = `[${new Date().toISOString()}] payload[data: ${ | ||
payload.data | ||
}; metadata: ${payload.metadata}]|${isComplete}`; | ||
outputDiv.appendChild(div); | ||
}, | ||
onComplete: () => { | ||
const div = document.createElement("div"); | ||
div.textContent = `Stream completed...`; | ||
outputDiv.appendChild(div); | ||
}, | ||
onExtension: () => {}, | ||
} | ||
); | ||
})(); |
31 changes: 31 additions & 0 deletions
31
packages/rsocket-examples/src/webpack/simple/client/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "rsocket-examples-websocket-simple-client", | ||
"version": "0.0.0", | ||
"license": "Apache-2.0", | ||
"private": true, | ||
"files": [ | ||
"dist", | ||
"LICENSE" | ||
], | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"build": "webpack", | ||
"serve": "webpack serve" | ||
}, | ||
"engines": { | ||
"node": "^16.17.0" | ||
}, | ||
"devDependencies": { | ||
"buffer": "^6.0.3", | ||
"rsocket-adapter-rxjs": "^1.0.0-alpha.4", | ||
"rsocket-composite-metadata": "^1.0.0-alpha.3", | ||
"rsocket-core": "^1.0.0-alpha.3", | ||
"rsocket-websocket-client": "^1.0.0-alpha.3", | ||
"webpack": "^5.74.0", | ||
"webpack-cli": "^4.10.0", | ||
"webpack-dev-server": "^4.11.0" | ||
}, | ||
"dependencies": { | ||
"html-webpack-plugin": "^5.5.0" | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
packages/rsocket-examples/src/webpack/simple/client/webpack.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const path = require("path"); | ||
const webpack = require("webpack"); | ||
const HtmlWebpackPlugin = require("html-webpack-plugin"); | ||
|
||
module.exports = { | ||
entry: "./index.js", | ||
mode: "development", | ||
output: { | ||
filename: "main.js", | ||
path: path.resolve(__dirname, "dist"), | ||
}, | ||
devtool: "source-map", | ||
devServer: { | ||
static: { | ||
directory: path.join(__dirname, "dist"), | ||
}, | ||
compress: false, | ||
port: 9000, | ||
}, | ||
resolve: { | ||
fallback: { | ||
buffer: require.resolve("buffer/"), | ||
}, | ||
}, | ||
plugins: [ | ||
new HtmlWebpackPlugin({ | ||
template: "./index.html", | ||
}), | ||
new webpack.ProvidePlugin({ | ||
Buffer: ["buffer", "Buffer"], | ||
}), | ||
], | ||
}; |
30 changes: 30 additions & 0 deletions
30
packages/rsocket-examples/src/webpack/simple/server/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "rsocket-examples-websocket-simple-server", | ||
"version": "0.0.0", | ||
"license": "Apache-2.0", | ||
"private": true, | ||
"files": [ | ||
"dist", | ||
"LICENSE" | ||
], | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"start": "node server.js" | ||
}, | ||
"engines": { | ||
"node": "^16.17.0" | ||
}, | ||
"dependencies": { | ||
"rsocket-adapter-rxjs": "^1.0.0-alpha.4", | ||
"rsocket-composite-metadata": "^1.0.0-alpha.3", | ||
"rsocket-core": "^1.0.0-alpha.3", | ||
"rsocket-tcp-client": "^1.0.0-alpha.3", | ||
"rsocket-tcp-server": "^1.0.0-alpha.3", | ||
"rsocket-websocket-client": "^1.0.0-alpha.3", | ||
"rsocket-websocket-server": "^1.0.0-alpha.3", | ||
"ws": "~8.2.3" | ||
}, | ||
"devDependencies": { | ||
|
||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
packages/rsocket-examples/src/webpack/simple/server/server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const { RSocketServer } = require("rsocket-core"); | ||
const { WebsocketServerTransport } = require("rsocket-websocket-server"); | ||
const WebSocket = require("ws"); | ||
|
||
const port = 9090; | ||
|
||
const server = new RSocketServer({ | ||
transport: new WebsocketServerTransport({ | ||
wsCreator: (options) => { | ||
return new WebSocket.Server({ | ||
port: port, | ||
}); | ||
}, | ||
}), | ||
acceptor: { | ||
accept: async () => ({ | ||
requestResponse: (payload, responderStream) => { | ||
const timeout = setTimeout( | ||
() => | ||
responderStream.onNext( | ||
{ | ||
data: Buffer.concat([Buffer.from("Echo: "), payload.data]), | ||
}, | ||
true | ||
), | ||
1000 | ||
); | ||
return { | ||
cancel: () => { | ||
clearTimeout(timeout); | ||
console.log("cancelled"); | ||
}, | ||
onExtension: () => { | ||
console.log("Received Extension request"); | ||
}, | ||
}; | ||
}, | ||
}), | ||
}, | ||
}); | ||
|
||
(async () => { | ||
await server.bind(); | ||
console.log(`Server listening on port ${port}`); | ||
})(); |