Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocket: Поправил Backend-пример #1990

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions 5-network/11-websocket/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,13 @@ socket.onmessage = function(event) {
4. Когда подключение закрыто: `clients.delete(socket)`.

```js
const ws = new require('ws');
const http = require('http');
const ws = require('ws');
const wss = new ws.Server({noServer: true});

const clients = new Set();

http.createServer((req, res) => {
const server = http.createServer((req, res) => {
// в реальном проекте здесь может также быть код для обработки отличных от websoсket-запросов
// здесь мы работаем с каждым запросом как с веб-сокетом
wss.handleUpgrade(req, req.socket, Buffer.alloc(0), onSocketConnect);
Expand All @@ -353,6 +354,8 @@ function onSocketConnect(ws) {
clients.delete(ws);
});
}

server.listen(80);
```

Вот рабочий пример:
Expand Down