You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ارتباط از طریق وب سوکت از "frame" ها یا همان برشهایی از اطلاعات ساخته شده که میتواند از هر سمت ارسال شده و انواع متفاوتی داشته باشد:
167
167
168
-
- "text frames" -- contain text data that parties send to each other.
169
-
- "binary data frames" -- contain binary data that parties send to each other.
170
-
- "ping/pong frames" are used to check the connection, sent from the server, the browser responds to these automatically.
171
-
-there's also "connection close frame" and a few other service frames.
168
+
- "text frames" -- دیتای متنی ردوبدل شده را شامل میشود.
169
+
- "binary data frames" -- دیتای باینری رد و بدل شده را شامل میشود.
170
+
- "ping/pong frames" -- برای بررسی اتصال از سمت سرور ارسال میشود و مرورگر به صورت خودکار به آن پاسخ میدهد.
171
+
-همچنین فریمی به نام "connection close frame" و تعداد دیگری از سرویس فریمها وجود دارند.
172
172
173
-
In the browser, we directly work only with text or binary frames.
173
+
در مرورگر، ما مستقیما با متن یا binary frames کار میکنیم.
174
174
175
-
**WebSocket `.send()` method can send either text or binary data.**
175
+
**متد `()send.` وب سوکت توانایی ارسال هم متن و هم دیتای باینری را دارا میباشد**
176
176
177
-
A call`socket.send(body)`allows `body` in string or a binary format, including `Blob`, `ArrayBuffer`, etc. No settings are required: just send it out in any format.
177
+
صدا زدن`socket.send(body)`اجازهی استفاده از هم رشته و هم فرمت باینری را در `body` میدهد که شامل `Blob`, `ArrayBuffer` و موارد مشابه میباشد. هیچ تنظیماتی نیاز نیست: میتوانید با هر فرمتی ارسالش کنید.
178
178
179
-
**When we receive the data, text always comes as string. And for binary data, we can choose between `Blob`and`ArrayBuffer`formats.**
179
+
**هنگام دریافت دیتا، متن همیشه به صورت رشته میآید. و برای دیتای باینری میتوانیم بین فرمتهای `Blob`و`ArrayBuffer`انتخاب کنیم**
180
180
181
-
That's set by`socket.binaryType`property, it's `"blob"` by default, so binary data comes as`Blob`objects.
181
+
که با مشخصه`socket.binaryType`قابل تنظیم بوده و به صورت پیشفرض `""blob""` است بنابراین دیتای باینری به شکل آبجکتهای`Blob`دریافت میشود.
182
182
183
183
[Blob](info:blob) is a high-level binary object, it directly integrates with `<a>`, `<img>` and other tags, so that's a sane default. But for binary processing, to access individual data bytes, we can change it to `"arraybuffer"`:
Imagine, our app is generating a lot of data to send. But the user has a slow network connection, maybe on a mobile internet, outside of a city.
194
+
تصور کنید که برنامهی ما مقدار زیادی دیتا ارسال میکند اما اینترنت کاربر سرعت پایینی دارد شاید بشه اینترنت همراه خارج از شهر رو مثال زد.
195
195
196
-
We can call `socket.send(data)`again and again. But the data will be buffered (stored) in memory and sent out only as fast as network speed allows.
196
+
ما `socket.send(data)`را بارها و بارها صدا میزنیم. اما دیتا در حافظه بافر (ذخیره) شده و زمانی که سرعت شبکه به حد کافی برسد به بیرون ارسال خواهد شد.
197
197
198
-
The`socket.bufferedAmount`property stores how many bytes remain buffered at this moment, waiting to be sent over the network.
198
+
مشخصه`socket.bufferedAmount`تعداد بایتهای ذخیره شده درلحظه و درحال انتظار برای ارسال تحت شبکه را ذخیره میکند.
199
199
200
-
We can examine it to see whether the socket is actually available for transmission.
200
+
با ارزیابی این پارامتر میتونیم بفهمیم که آیا سوکت واقعا برای انتقال دردسترس است یا نه
201
201
202
202
```js
203
-
//every 100ms examine the socket and send more data
204
-
//only if all the existing data was sent out
203
+
//هر صد میلی ثانیه سوکت را بررسی کرده و دیتای بیشتری را ارسال میکند
204
+
//تنها زمانی که همهی دیتای موجود ارسال شده باشد
205
205
setInterval(() => {
206
206
if (socket.bufferedAmount==0) {
207
207
socket.send(moreData());
208
208
}
209
209
}, 100);
210
210
```
211
211
212
+
## بستن اتصال
212
213
213
-
## Connection close
214
+
به طور معمولی زمانی که یک طرف قصد بستن اتصال را داشته باشد(هر دوی مروگر و سرور حق برابری برای اینکار دارا هستند.), آنها عبارت `connection close frame` را به همراه یک کد عددی و دلیل اینکار را به شکل متنی ارسال میکنند.
214
215
215
-
Normally, when a party wants to close the connection (both browser and server have equal rights), they send a "connection close frame" with a numeric code and a textual reason.
216
+
روش انجام این کار به شکل زیر است:
216
217
217
-
The method for that is:
218
218
```js
219
219
socket.close([code], [reason]);
220
220
```
221
221
222
-
-`code`is a special WebSocket closing code (optional)
223
-
-`reason`is a string that describes the reason of closing (optional)
222
+
-`code`یک کد خاص برای بستن وب سوکت (اختیاری)
223
+
-`reason`رشتهای که علت بستن اتصال را توضیح میدهد (اختیاری)
224
224
225
-
Then the other party in the `close`event handler gets the code and the reason, e.g.:
225
+
سپس طرف دیگر رویداد کد `close`و علت آنرا دریافت میکند. برای مثال:
226
226
227
227
```js
228
-
//closing party:
228
+
//سمتی که ارتباط را میبندد:
229
229
socket.close(1000, "Work complete");
230
230
231
-
//the other party
232
-
socket.onclose=event=> {
231
+
//سمت دیگر:
232
+
socket.onclose=(event)=> {
233
233
// event.code === 1000
234
234
// event.reason === "Work complete"
235
235
// event.wasClean === true (clean close)
236
236
};
237
237
```
238
238
239
-
Most common code values:
239
+
رایجعترین کدها و مقادیر آنها:
240
240
241
-
-`1000` -- the default, normal closure (used if no `code`supplied),
242
-
-`1006` -- no way to set such code manually, indicates that the connection was lost (no close frame).
241
+
-`1000` -- بستن پیشفرض و عادی (زمانی که `code`نباشد استفاده میشود),
242
+
-`1006` -- راهی برای تنظیم این کد به صورت دستی وجود نداشته و نمایانگر از دست رفتن ارتباط هست (no close frame)
243
243
244
-
There are other codes like:
244
+
کدهای دیگر مثل موارد زیر هم وجود دارند:
245
245
246
246
-`1001` -- the party is going away, e.g. server is shutting down, or a browser leaves the page,
247
-
-`1009` -- the message is too big to process,
248
-
-`1011` -- unexpected error on server,
249
-
- ...and so on.
247
+
-`1009` -- حجم پیام برای انجام پردازش زیاد است,
248
+
-`1011` -- خطای پیشبینی نشده در سرور,
249
+
- ...و غیره.
250
250
251
-
The full list can be found in [RFC6455, §7.4.1](https://tools.ietf.org/html/rfc6455#section-7.4.1).
251
+
لیست کامل رو میتونید در [RFC6455, §7.4.1](https://tools.ietf.org/html/rfc6455#section-7.4.1) پیدا کنید.
252
252
253
-
WebSocket codes are somewhat like HTTP codes, but different. In particular, codes lower than `1000`are reserved, there'll be an error if we try to set such a code.
253
+
کدهای وب سوکت تاحدی مشابه کدهای HTTP میباشند اما متفاوتند. به صورت خاص کدهای کمتر از `1000`از قبل رزرو شده اند و اگر تلاش کنیم تا یکی از این کدهارو استفاده کنیم به ارور برخورد خواهیم کرد.
0 commit comments