Skip to content

Commit

Permalink
fix host
Browse files Browse the repository at this point in the history
  • Loading branch information
dimdenGD committed Oct 10, 2024
1 parent 60235d2 commit a0b52ef
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,14 @@ const express = require("express");
const ws = require("ws");

const app = express();
const wsServer = new ws.WebSocketServer({ noServer: true, path: "/wspath" }); // path is optional
const wsServer = new ws.WebSocketServer({ noServer: true, path: "/wspath" });

app.get("/", (_, res) => res.send("Hello, world!"));

const server = http.createServer(app);
server.on("upgrade", async (request, socket, head) => {
const user = await getUserFromDatabase(request.headers['authorization']); // your auth logic
// your auth logic
const user = await getUserFromDatabase(request.headers['authorization']);
if(!user) return socket.destroy();

wsServer.handleUpgrade(request, socket, head, (ws) => {
Expand All @@ -147,7 +148,7 @@ server.on("upgrade", async (request, socket, head) => {
server.listen(3000);
```

You can do this:
You should do this:
```js
const { WebSocketServer } = require("ultimate-ws");
const express = require("ultimate-express");
Expand All @@ -156,10 +157,13 @@ const app = express();

const wsServer = new WebSocketServer({
server: app,
path: "/wspath",
handleUpgrade: async (request) => {
const user = await getUserFromDatabase(request.headers['authorization']); // your auth logic
// your auth logic
const user = await getUserFromDatabase(request.headers['authorization']);
if(!user) {
// request has `req` and `res` properties, which are instances of `uws.HttpRequest` and `uws.HttpResponse`
// request has `req` and `res` properties
// which are instances of `uws.HttpRequest` and `uws.HttpResponse`
request.res.cork(() => {
request.res.writeStatus("401 Unauthorized");
request.res.end();
Expand Down Expand Up @@ -250,7 +254,7 @@ Below is the list of supported features and their compatibility:
- ✅ server.address()
- ✅ server.clients
- ✅ server.close(callback)
- ❌ server.handleUpgrade(request, socket, head, callback) - this is unneeded. Just pass `server` (uWS.App or [µExpress app](https://github.com/dimdenGD/ultimate-express)) to `WebSocketServer` as option. See above for an example.
- ❌ server.handleUpgrade(request, socket, head, callback) - see examples above for alternatives
- ❌ server.shouldHandle(request)

### Client
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ultimate-ws",
"version": "1.0.8",
"version": "1.0.9",
"description": "The Ultimate WebSocket server. ws-compatible server, based on uWebSockets.",
"main": "src/index.js",
"directories": {
Expand Down
6 changes: 0 additions & 6 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,6 @@ module.exports = class WebSocketServer extends EventEmitter {
compression: typeof this.options.perMessageDeflate !== 'number' && this.options.perMessageDeflate ?
(uWS.DEDICATED_COMPRESSOR_4KB | uWS.DEDICATED_DECOMPRESSOR) : this.options.perMessageDeflate,
upgrade: async (res, req, context) => {
if(this.options.host) {
const host = req.getHeader('host');
if(host !== this.options.host) {
return req.setYield(true);
}
}
const headers = [];
const msg = new IncomingMessage(this, req, res);
res.onAborted(() => {
Expand Down

0 comments on commit a0b52ef

Please sign in to comment.