diff --git a/packages/rsocket-examples/src/webpack/simple/client/index.html b/packages/rsocket-examples/src/webpack/simple/client/index.html new file mode 100644 index 0000000..cc2c9d8 --- /dev/null +++ b/packages/rsocket-examples/src/webpack/simple/client/index.html @@ -0,0 +1,11 @@ + + + + + RSocket Webpack Example + + +

RSocket Webpack Example

+
+ + diff --git a/packages/rsocket-examples/src/webpack/simple/client/index.js b/packages/rsocket-examples/src/webpack/simple/client/index.js new file mode 100644 index 0000000..2316cf1 --- /dev/null +++ b/packages/rsocket-examples/src/webpack/simple/client/index.js @@ -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: () => {}, + } + ); +})(); diff --git a/packages/rsocket-examples/src/webpack/simple/client/package.json b/packages/rsocket-examples/src/webpack/simple/client/package.json new file mode 100644 index 0000000..4aaef5d --- /dev/null +++ b/packages/rsocket-examples/src/webpack/simple/client/package.json @@ -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" + } +} diff --git a/packages/rsocket-examples/src/webpack/simple/client/webpack.config.js b/packages/rsocket-examples/src/webpack/simple/client/webpack.config.js new file mode 100644 index 0000000..79fef9c --- /dev/null +++ b/packages/rsocket-examples/src/webpack/simple/client/webpack.config.js @@ -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"], + }), + ], +}; diff --git a/packages/rsocket-examples/src/webpack/simple/server/package.json b/packages/rsocket-examples/src/webpack/simple/server/package.json new file mode 100644 index 0000000..be29a75 --- /dev/null +++ b/packages/rsocket-examples/src/webpack/simple/server/package.json @@ -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": { + + } +} diff --git a/packages/rsocket-examples/src/webpack/simple/server/server.js b/packages/rsocket-examples/src/webpack/simple/server/server.js new file mode 100644 index 0000000..24ce292 --- /dev/null +++ b/packages/rsocket-examples/src/webpack/simple/server/server.js @@ -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}`); +})();