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
برای اطلاع از وضعیت اتصال پراپرتی `socket.readyState` با مقادیر زیر وجود دارد:
266
267
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": ارتباط بسته شده است.
268
272
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
+
## مثال چت
273
274
275
+
بیاید تا با هم یک مثال از پیادهسازی یک برنامه چت را بااستفاده از ای پی آی وب سوکت و ماژول وب سوکت node.js <https://github.com/websockets/ws> بررسی کنیم. تمرکز اصلی ما سمت کلاینت خواهد بود اما سمت سرور هم ساده است.
274
276
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>` برای پیامهای دریافتی داریم
280
278
281
279
```html
282
280
<!-- message form -->
283
281
<formname="publish">
284
-
<inputtype="text"name="message">
285
-
<inputtype="submit"value="Send">
282
+
<inputtype="text"name="message" />
283
+
<inputtype="submit"value="Send" />
286
284
</form>
287
285
288
286
<!-- div with messages -->
289
287
<divid="messages"></div>
290
288
```
291
289
292
-
From JavaScript we want three things:
293
-
1. Open the connection.
290
+
برای کدهای جاوااسکریپت برنامه ما نیاز به سه چیز داریم:
291
+
292
+
1. باز کردن اتصال.
294
293
2. On form submission -- `socket.send(message)` for the message.
295
294
3. On incoming message -- append it to `div#messages`.
296
295
297
-
Here's the code:
296
+
کد رو به اینصورت خواهیم داشت:
298
297
299
298
```js
300
299
let socket =newWebSocket("wss://javascript.info/article/websocket/chat/ws");
301
300
302
-
//send message from the form
303
-
document.forms.publish.onsubmit=function() {
301
+
//ارسال پیام از فرم
302
+
document.forms.publish.onsubmit=function() {
304
303
let outgoingMessage =this.message.value;
305
304
306
305
socket.send(outgoingMessage);
307
306
returnfalse;
308
307
};
309
308
310
-
//message received - show the message in div#messages
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 استفاده میکنیم, اما شما مجبور نیستید. دیگر پلتفورمها روشهای خاص خودشون رو برای کار با وب سوکت دارا هستند.
321
320
322
-
The server-side algorithm will be:
321
+
الگوریتم سمت سرور به اینصورت خواهد بود:
323
322
324
323
1. Create `clients = new Set()` -- a set of sockets.
325
324
2. For each accepted websocket, add it to the set `clients.add(socket)` and set `message` event listener to get its messages.
326
325
3. When a message is received: iterate over clients and send it to everyone.
327
326
4. When a connection is closed: `clients.delete(socket)`.
328
327
329
328
```js
330
-
constws=newrequire('ws');
331
-
constwss=newws.Server({noServer:true});
329
+
constws=newrequire("ws");
330
+
constwss=newws.Server({noServer:true});
332
331
333
332
constclients=newSet();
334
333
335
334
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
+
//در پروژه واقعی کدهای دیگری برای رسیدگی به درخواستهای غیر وب سوکت خواهیم داشت
message =message.slice(0, 50); //max message length will be 50
343
+
ws.on("message", function(message) {
344
+
message =message.slice(0, 50); //حداکثر طول 50 را میتواند دارا باشد
346
345
347
-
for(let client of clients) {
346
+
for(let client of clients) {
348
347
client.send(message);
349
348
}
350
349
});
351
350
352
-
ws.on('close', function() {
351
+
ws.on("close", function() {
353
352
clients.delete(ws);
354
353
});
355
354
}
356
355
```
357
356
358
-
359
-
Here's the working example:
357
+
یک مثال:
360
358
361
359
[iframe src="chat" height="100" zip]
362
360
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
+
## خلاصه
364
364
365
-
## Summary
365
+
وب سوکت یک راه مدرن برای داشتن یک ارتباط مرورگر-سرور مستمر میباشد.
366
366
367
-
WebSocket is a modern way to have persistent browser-server connections.
367
+
- وب سوکتها محدودیت cross origin ندارند.
368
+
- به خوبی در مرورگرها پشتیبانی میشوند.
369
+
- میتوانند اطلاعات را به شکل رشته و باینری ارسال/دریافت کنند
0 commit comments