惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
N
News | PayPal Newsroom
S
Security @ Cisco Blogs
Schneier on Security
Schneier on Security
V
V2EX - 技术
S
Secure Thoughts
W
WeLiveSecurity
Google DeepMind News
Google DeepMind News
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
S
Securelist
S
Security Archives - TechRepublic
Know Your Adversary
Know Your Adversary
V
Vulnerabilities – Threatpost
Security Latest
Security Latest
Recent Commits to openclaw:main
Recent Commits to openclaw:main
G
GRAHAM CLULEY
H
Hacker News: Front Page
Microsoft Azure Blog
Microsoft Azure Blog
I
Intezer
Google Online Security Blog
Google Online Security Blog
美团技术团队
阮一峰的网络日志
阮一峰的网络日志
T
The Exploit Database - CXSecurity.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Webroot Blog
Webroot Blog
Jina AI
Jina AI
Engineering at Meta
Engineering at Meta
P
Proofpoint News Feed
The Cloudflare Blog
I
InfoQ
L
LangChain Blog
U
Unit 42
P
Proofpoint News Feed
S
Schneier on Security
S
Security Affairs
Y
Y Combinator Blog
T
Tenable Blog
N
News and Events Feed by Topic
MyScale Blog
MyScale Blog
量子位
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
博客园 - 聂微东
D
Darknet – Hacking Tools, Hacker News & Cyber Security
GbyAI
GbyAI
AWS News Blog
AWS News Blog

WishMeLz - NodeJS

Electron 主进程起一个可用的 HTTPS 静态服务器 - WishMeLz 宝塔Node管理器安装node版本提示:文件下载失败,请手动安装! - WishMeLz Minio 之 Nodejs - WishMeLz nuxt项目本地启动,多开标签一直显示加载中 - WishMeLz nvm win10安装 - WishMeLz nodejs 对接邮箱服务 imap - WishMeLz PM2搭配nvm使用不同版本Node启动项目 - WishMeLz nodejs 生成网站sitemap.xml - WishMeLz EA Racenet API - WishMeLz
SSE(Server-Sent Events) - WishMeLz
WishMeLz · 2026-06-01 · via WishMeLz - NodeJS

服务端

const express = require("express");
const cors = require("cors");

const app = express();
const port = 6548;

app.use(cors());

app.get("/sse", (req, res) => {
  res.set({
    "Content-Type": "text/event-stream",
    "Cache-Control": "no-cache",
    Connection: "keep-alive",
  });

  res.flushHeaders();

  setInterval(() => {
    const data = {
      message: `Server time is ${new Date().toLocaleTimeString()}`,
    };

    res.write(`data: ${JSON.stringify(data)}\n\n`);
  }, 1000);
});

app.listen(port, () => {
  console.log(`Server started on port ${port}`);
});

前端

 const events = document.getElementById("events");
      const source = new EventSource("http://127.0.0.1:6548/sse");
      source.onmessage = (event) => {
        const data = JSON.parse(event.data);
        const message = data.message;
        console.log(event);
     
      };

最后编辑时间为: June 25th , 2024 at 07:04 pm
本文由 Wish 创作,采用 知识共享署名 4.0 国际许可协议进行许可
可自由转载、引用,但需署名作者且注明文章出处

标签 : 无标签