Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
dtkdtk wants to merge1 commit intojavascript-tutorial:master
base:master
Choose a base branch
Loading
fromdtkdtk:master
Open
Changes fromall 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 deletions5-network/11-websocket/article.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp