Warning: mkdir(): No space left on device in /var/www/tgoop/post.php on line 37

Warning: file_put_contents(aCache/aDaily/post/dotnetcode/--): Failed to open stream: No such file or directory in /var/www/tgoop/post.php on line 50
DotNet | دات نت@dotnetcode P.3075
DOTNETCODE Telegram 3075
DotNet | دات نت
قسمت شماره ۱: خداحافظی با کدهای تکراری در C# 14! سلام به همه برنامه‌نویس‌های خفن دات‌نتی! 👋 خبرهای داغ داریم! 🔥 .NET 10 و C# 14 با یه سری ویژگی‌های فوق‌العاده از راه رسیدن که کدنویسی رو خیلی شیرین‌تر می‌کنه. بیاید چندتا از باحال‌ترین‌هاش رو با هم ببینیم:…
قسمت ۳ : پشتیبانی داخلی از (SSE) Server-Sent Events

اغلب وقتی می‌خوایم دیتای زنده از سرور به کلاینت بفرستیم (مثل نوتیفیکیشن، قیمت لحظه‌ای ارز یا وضعیت یه پردازش طولانی)، اولین فکرمون میره سراغ WebSockets.

اما WebSockets یه پروتکل دوطرفه و پیچیده‌ست. خیلی وقت‌ها ما فقط نیاز داریم سرور یک‌طرفه به کلاینت دیتا «پوش» کنه.
اینجا SSE (Server-Sent Events) وارد می‌شه. SSE بر پایه HTTP کار می‌کنه، سبکه و پیاده‌سازیش خیلی ساده‌تره.

در ASP.NET Core 10، پشتیبانی از SSE داخلی شده:
مثال (سمت سرور - C#):

app.MapGet("/live-clock", async (HttpContext context, CancellationToken ct) =>
{
// به مرورگر می‌گیم که این یه استریم SSE هست
await context.Response.WriteSseHeadersAsync(ct);

// یه حلقه که هر ثانیه، زمان سرور رو برای کلاینت می‌فرسته
while (!ct.IsCancellationRequested)
DRAFT:
{
// یه "ایونت" به اسم 'timeUpdate' می‌فرستیم
await context.Response.WriteSseEventAsync(
"timeUpdate", // اسم ایونت
$"ساعت سرور: {DateTime.Now:T}", // دیتا
ct);

await Task.Delay(1000, ct); // ۱ ثانیه صبر کن
}
});

مثال (سمت کلاینت - JavaScript):
// به همین سادگی به سرور وصل می‌شیم
const eventSource = new EventSource("/live-clock");

// به ایونت 'timeUpdate' گوش می‌دیم
eventSource.addEventListener("timeUpdate", (event) => {
console.log(event.data);
// خروجی: "ساعت سرور: 14:30:01"
// خروجی: "ساعت سرور: 14:30:02"
// ...
});


🎺برای یادگیری بیشتر و دریافت مطالب مفید در زمینه .NET و برنامه‌نویسی، به کانال ما بپیوندید!

📚💻 @dotnetcode 🖥👨‍💻
Please open Telegram to view this post
VIEW IN TELEGRAM
6🔥4👏3



tgoop.com/dotnetcode/3075
Create:
Last Update:

قسمت ۳ : پشتیبانی داخلی از (SSE) Server-Sent Events

اغلب وقتی می‌خوایم دیتای زنده از سرور به کلاینت بفرستیم (مثل نوتیفیکیشن، قیمت لحظه‌ای ارز یا وضعیت یه پردازش طولانی)، اولین فکرمون میره سراغ WebSockets.

اما WebSockets یه پروتکل دوطرفه و پیچیده‌ست. خیلی وقت‌ها ما فقط نیاز داریم سرور یک‌طرفه به کلاینت دیتا «پوش» کنه.
اینجا SSE (Server-Sent Events) وارد می‌شه. SSE بر پایه HTTP کار می‌کنه، سبکه و پیاده‌سازیش خیلی ساده‌تره.

در ASP.NET Core 10، پشتیبانی از SSE داخلی شده:
مثال (سمت سرور - C#):

app.MapGet("/live-clock", async (HttpContext context, CancellationToken ct) =>
{
// به مرورگر می‌گیم که این یه استریم SSE هست
await context.Response.WriteSseHeadersAsync(ct);

// یه حلقه که هر ثانیه، زمان سرور رو برای کلاینت می‌فرسته
while (!ct.IsCancellationRequested)
DRAFT:
{
// یه "ایونت" به اسم 'timeUpdate' می‌فرستیم
await context.Response.WriteSseEventAsync(
"timeUpdate", // اسم ایونت
$"ساعت سرور: {DateTime.Now:T}", // دیتا
ct);

await Task.Delay(1000, ct); // ۱ ثانیه صبر کن
}
});

مثال (سمت کلاینت - JavaScript):
// به همین سادگی به سرور وصل می‌شیم
const eventSource = new EventSource("/live-clock");

// به ایونت 'timeUpdate' گوش می‌دیم
eventSource.addEventListener("timeUpdate", (event) => {
console.log(event.data);
// خروجی: "ساعت سرور: 14:30:01"
// خروجی: "ساعت سرور: 14:30:02"
// ...
});


🎺برای یادگیری بیشتر و دریافت مطالب مفید در زمینه .NET و برنامه‌نویسی، به کانال ما بپیوندید!

📚💻 @dotnetcode 🖥👨‍💻

BY DotNet | دات نت




Share with your friend now:
tgoop.com/dotnetcode/3075

View MORE
Open in Telegram


Telegram News

Date: |

Although some crypto traders have moved toward screaming as a coping mechanism, several mental health experts call this therapy a pseudoscience. The crypto community finds its way to engage in one or the other way and share its feelings with other fellow members. Members can post their voice notes of themselves screaming. Interestingly, the group doesn’t allow to post anything else which might lead to an instant ban. As of now, there are more than 330 members in the group. Add the logo from your device. Adjust the visible area of your image. Congratulations! Now your Telegram channel has a face Click “Save”.! Telegram users themselves will be able to flag and report potentially false content. According to media reports, the privacy watchdog was considering “blacklisting” some online platforms that have repeatedly posted doxxing information, with sources saying most messages were shared on Telegram.
from sg


Telegram DotNet | دات نت
FROM Singapore