Skip to content

Commit 0ccebb0

Browse files
committed
update article.md -- websocket
1 parent 56da75f commit 0ccebb0

1 file changed

Lines changed: 41 additions & 41 deletions

File tree

5-network/11-websocket/article.md

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -261,114 +261,114 @@ socket.onclose = (event) => {
261261
};
262262
```
263263

264+
## وضعیت اتصال
264265

265-
## Connection state
266+
برای اطلاع از وضعیت اتصال پراپرتی `socket.readyState` با مقادیر زیر وجود دارد:
266267

267-
To get connection state, additionally there's `socket.readyState` property with values:
268+
- **`0`** -- "CONNECTING": اتصال هنوز برقرار نشده است,
269+
- **`1`** -- "OPEN": درحال برقراری ارتباط,
270+
- **`2`** -- "CLOSING": درحال بستن ارتباط,
271+
- **`3`** -- "CLOSED": ارتباط بسته شده است.
268272

269-
- **`0`** -- "CONNECTING": the connection has not yet been established,
270-
- **`1`** -- "OPEN": communicating,
271-
- **`2`** -- "CLOSING": the connection is closing,
272-
- **`3`** -- "CLOSED": the connection is closed.
273+
## مثال چت
273274

275+
بیاید تا با هم یک مثال از پیاده‌سازی یک برنامه چت را بااستفاده از ای پی آی وب سوکت و ماژول وب سوکت node.js <https://github.com/websockets/ws> بررسی کنیم. تمرکز اصلی ما سمت کلاینت خواهد بود اما سمت سرور هم ساده است.
274276

275-
## Chat example
276-
277-
Let's review a chat example using browser WebSocket API and Node.js WebSocket module <https://github.com/websockets/ws>. We'll pay the main attention to the client side, but the server is also simple.
278-
279-
HTML: we need a `<form>` to send messages and a `<div>` for incoming messages:
277+
کد HTML: نیاز به یک تگ `<form>` برای ارسال پیامها و یک تگ `<div>` برای پیامهای دریافتی داریم
280278

281279
```html
282280
<!-- message form -->
283281
<form name="publish">
284-
<input type="text" name="message">
285-
<input type="submit" value="Send">
282+
<input type="text" name="message" />
283+
<input type="submit" value="Send" />
286284
</form>
287285

288286
<!-- div with messages -->
289287
<div id="messages"></div>
290288
```
291289

292-
From JavaScript we want three things:
293-
1. Open the connection.
290+
برای کدهای جاوااسکریپت برنامه ما نیاز به سه چیز داریم:
291+
292+
1. باز کردن اتصال.
294293
2. On form submission -- `socket.send(message)` for the message.
295294
3. On incoming message -- append it to `div#messages`.
296295

297-
Here's the code:
296+
کد رو به اینصورت خواهیم داشت:
298297

299298
```js
300299
let socket = new WebSocket("wss://javascript.info/article/websocket/chat/ws");
301300

302-
// send message from the form
303-
document.forms.publish.onsubmit = function() {
301+
// ارسال پیام از فرم
302+
document.forms.publish.onsubmit = function () {
304303
let outgoingMessage = this.message.value;
305304

306305
socket.send(outgoingMessage);
307306
return false;
308307
};
309308

310-
// message received - show the message in div#messages
311-
socket.onmessage = function(event) {
309+
// div#messagesپیام دریافت شد - نمایش پیام در
310+
socket.onmessage = function (event) {
312311
let message = event.data;
313312

314-
let messageElem = document.createElement('div');
313+
let messageElem = document.createElement("div");
315314
messageElem.textContent = message;
316-
document.getElementById('messages').prepend(messageElem);
317-
}
315+
document.getElementById("messages").prepend(messageElem);
316+
};
318317
```
319318

320-
Server-side code is a little bit beyond our scope. Here we'll use Node.js, but you don't have to. Other platforms also have their means to work with WebSocket.
319+
کد سمت سرور یک مقدار فراتر از بحث ما هست. اینجا ما از node.js استفاده میکنیم, اما شما مجبور نیستید. دیگر پلتفورم‌ها روش‌های خاص خودشون رو برای کار با وب سوکت دارا هستند.
321320

322-
The server-side algorithm will be:
321+
الگوریتم سمت سرور به اینصورت خواهد بود:
323322

324323
1. Create `clients = new Set()` -- a set of sockets.
325324
2. For each accepted websocket, add it to the set `clients.add(socket)` and set `message` event listener to get its messages.
326325
3. When a message is received: iterate over clients and send it to everyone.
327326
4. When a connection is closed: `clients.delete(socket)`.
328327

329328
```js
330-
const ws = new require('ws');
331-
const wss = new ws.Server({noServer: true});
329+
const ws = new require("ws");
330+
const wss = new ws.Server({ noServer: true });
332331

333332
const clients = new Set();
334333

335334
http.createServer((req, res) => {
336-
// here we only handle websocket connections
337-
// in real project we'd have some other code here to handle non-websocket requests
335+
// در اینجا فقط ارتباط وب سوکت را کنترل میکنیم
336+
// در پروژه‌ واقعی کدهای دیگری برای رسیدگی به درخواست‌های غیر وب سوکت خواهیم داشت
338337
wss.handleUpgrade(req, req.socket, Buffer.alloc(0), onSocketConnect);
339338
});
340339

341340
function onSocketConnect(ws) {
342341
clients.add(ws);
343342

344-
ws.on('message', function(message) {
345-
message = message.slice(0, 50); // max message length will be 50
343+
ws.on("message", function (message) {
344+
message = message.slice(0, 50); // حداکثر طول 50 را میتواند دارا باشد
346345

347-
for(let client of clients) {
346+
for (let client of clients) {
348347
client.send(message);
349348
}
350349
});
351350

352-
ws.on('close', function() {
351+
ws.on("close", function () {
353352
clients.delete(ws);
354353
});
355354
}
356355
```
357356

358-
359-
Here's the working example:
357+
یک مثال:
360358

361359
[iframe src="chat" height="100" zip]
362360

363-
You can also download it (upper-right button in the iframe) and run it locally. Just don't forget to install [Node.js](https://nodejs.org/en/) and `npm install ws` before running.
361+
شما همچنین میتونید این مثال رو دانلود کرده (دکمه بالا سمت راست در آیفریم) و در لوکال خودتون اجرا کنید. فقط فراموش نکنید که [Node.js](https://nodejs.org/en/) رو نصب کرده و دستور `npm install ws` رو قبل از راه اندازی اجرا کنید
362+
363+
## خلاصه
364364

365-
## Summary
365+
وب سوکت یک راه مدرن برای داشتن یک ارتباط مرورگر-سرور مستمر میباشد.
366366

367-
WebSocket is a modern way to have persistent browser-server connections.
367+
- وب سوکت‌ها محدودیت cross origin ندارند.
368+
- به خوبی در مرورگرها پشتیبانی میشوند.
369+
- میتوانند اطلاعات را به شکل رشته و باینری ارسال/دریافت کنند
368370

369-
- WebSockets don't have cross-origin limitations.
370-
- They are well-supported in browsers.
371-
- Can send/receive strings and binary data.
371+
که API ساده ای است
372372

373373
The API is simple.
374374

0 commit comments

Comments
 (0)