Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
xingsy97 committed May 7, 2024
1 parent 487877a commit 055d409
Show file tree
Hide file tree
Showing 6 changed files with 572 additions and 0 deletions.
33 changes: 33 additions & 0 deletions sdk/webpubsub-socketio-extension/examples/admin-ui/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

# Azure Socket.IO Admin UI Sample

This sample is modified from the sample "chat" to show how to use Socket.IO Admin UI.

Besides, it contains server-side logic for a simple echo performance benchmark. Use it with the part of benchmark in Azure Socket.IO Admin UI website.

## How to use
1. Open Azure Socket.IO Admin UI website in your browser.
2. Click the "Update" button in the right top corner and fill in the service endpoint of your resource.
3. Install the dependencies and start the server
```
$ npm install
$ npm run start -- "<connection-string>"
```
### Chat Room
4. Open a new tab to `http://localhost:3000` in your browser. Try the chat room.
5. Go back to the Admin UI and check related information.
### Simple Echo Benchmark
4. Open Admin UI page and click the "Benchmark" tab in the left sidebar.
5. Fill in parameters and then start the benchmark.
## Note
- Azure Socket.IO Admin UI only supports single server application now.
The line below is necessary to make the Admin UI work
```javascript
Namespace.prototype["fetchSockets"] = async function() { return this.local.fetchSockets(); };
```
- The simple echo benchmark is designed for dev/test use. DO NOT refer to it for production use.
The benchmark result is largely different from a serious benchmark result, for all clients are hosted in a web browser of a single machine.
87 changes: 87 additions & 0 deletions sdk/webpubsub-socketio-extension/examples/admin-ui/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const { instrument } = require("@socket.io/admin-ui");
const azure = require("@azure/web-pubsub-socket.io");
// Setup basic express server
const express = require('express');
const app = express();
const path = require('path');
const { Namespace, Server } = require("socket.io");
const server = require('http').createServer(app);

// Add an Web PubSub Option
const wpsOptions = {
hub: "eio_hub",
connectionString: process.argv[2] || process.env.WebPubSubConnectionString,
webPubSubServiceClientOptions: { allowInsecureConnection: true }
};

async function main() {
const io = await new Server(server).useAzureSocketIO(wpsOptions);
app.use(express.static(path.join(__dirname, 'public')));
app.get("/negotiate", azure.negotiate(io, () => {}));

instrument(io, { auth: false, mode: "development", username: "username", password: "assword"});

// NOTE: Azure Socket.IO AdminUI only supports single server application now. The next line is necessary to make the Admin UI work.
Namespace.prototype["fetchSockets"] = async function() { return this.local.fetchSockets(); };

// Logic for Echo Benchmark
const echoBenchmark = io.of("/echoBenchmark");

echoBenchmark.on('connection', (socket) => {
socket.on('client to server event', (data) => {
socket.emit("server to client event", (data));
});
});

// Logic for Chat Room
let numUsers = 0;

io.on('connection', socket => {
let addedUser = false;

// when the client emits 'new message', this listens and executes
socket.on('new message', (data) => {
// we tell the client to execute 'new message'
socket.broadcast.emit('new message', {
username: socket.username,
message: data
});
});

// when the client emits 'add user', this listens and executes
socket.on('add user', (username) => {
if (addedUser) return;

// we store the username in the socket session for this client
socket.username = username;
++numUsers;
addedUser = true;
socket.emit('login', {
numUsers: numUsers
});
// echo globally (all clients) that a person has connected
socket.broadcast.emit('user joined', {
username: socket.username,
numUsers: numUsers
});
});

// when the user disconnects.. perform this
socket.on('disconnect', () => {
if (addedUser) {
--numUsers;

// echo globally that this client has left
socket.broadcast.emit('user left', {
username: socket.username,
numUsers: numUsers
});
}
});
});
io.httpServer.listen(3000, () => {
console.log('Visit http://localhost:%d', 3000);
});
}

main();
17 changes: 17 additions & 0 deletions sdk/webpubsub-socketio-extension/examples/admin-ui/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "socket.io-chat",
"version": "0.0.0",
"description": "A simple chat client using socket.io",
"main": "index.js",
"private": true,
"license": "BSD",
"dependencies": {
"@azure/web-pubsub-socket.io": "^1.1.0",
"@socket.io/admin-ui": "^0.5.1",
"express": "~4.17.1",
"socket.io": "^4.6.1"
},
"scripts": {
"start": "node index.js"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Socket.IO Chat Example</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<ul class="pages">
<li class="chat page">
<div class="chatArea">
<ul class="messages"></ul>
</div>
<input class="inputMessage" placeholder="Type here..."/>
</li>
<li class="login page">
<div class="form">
<h3 class="title">What's your nickname?</h3>
<input class="usernameInput" type="text" maxlength="14" />
</div>
</li>
</ul>

<script>
var names = ["Bob", "Alice","John","Alex","Cooper","James", "Jack", "Rose", "Justin", "Julia", "White", "Black", "Green"];
var idx = Math.floor(Math.random()*100) % names.length;
document.querySelector('.usernameInput').value = names[idx];
</script>
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="https://cdn.socket.io/4.6.0/socket.io.min.js" integrity="sha384-c79GN5VsunZvi+Q/WObgk2in0CbZsHnjEqvFxC5DxHn9lTfNce2WW6h2pH6u/kF+" crossorigin="anonymous"></script>
<script src="./main.js"></script>
</body>
</html>
Loading

0 comments on commit 055d409

Please sign in to comment.