Skip to content

Commit

Permalink
support close
Browse files Browse the repository at this point in the history
  • Loading branch information
dimdenGD committed Oct 3, 2024
1 parent b418281 commit 15be3cd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ WIP

### Server events

- close
- close
- ✅ connection
- ❌ error
- ❌ headers
Expand All @@ -70,15 +70,15 @@ WIP

- ❌ server.address()
- ❌ server.clients
- server.close(callback)
- server.close(callback)
- ❌ server.handleUpgrade(request, socket, head, callback)
- ❌ server.shouldHandle(request)

### Client events

- ❌ close
- ❌ error
- message
- message
- ❌ ping
- ❌ pong
- ❌ redirect
Expand Down
8 changes: 8 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,12 @@ module.exports = class WebSocketServer extends EventEmitter {
if(callback) callback(this.port);
});
}

close(callback) {
this.uwsApp.close();
process.nextTick(() => {
this.emit("close");
if(callback) callback();
});
}
}
22 changes: 22 additions & 0 deletions tests/tests/server/close.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// must support close event

const ws = require("ws");

const wss = new ws.WebSocketServer({ port: 8080 }, cb);

wss.on("close", () => {
console.log("Closed server");
process.exit(0);
});

function cb() {
const c = new ws.WebSocket("ws://localhost:8080");
c.on("open", () => {
console.log("Connected to server");
c.close();
});
c.on("close", () => {
console.log("Closed connection");
process.exit(0);
});
};

0 comments on commit 15be3cd

Please sign in to comment.