Programming/web 관련2023. 3. 15. 11:25

chatGPT 님에게 server push 예제를 알려줘라고 했더니 아래와 같이 알려주신다.

Server push is a technique that allows a server to push data to a client without the client first making a request for the data. In JavaScript, this can be achieved using the Server-Sent Events (SSE) API.
Here's an example of how you can use SSE to implement server push in JavaScript:
On the server side, you need to create an endpoint that will send the SSE messages. In this example, we'll create a simple Node.js server:
const http = require('http');

const server = http.createServer((req, res) => {
  if (req.url === '/sse') {
    res.writeHead(200, {
      'Content-Type': 'text/event-stream',
      'Cache-Control': 'no-cache',
      'Connection': 'keep-alive'
    });

    setInterval(() => {
      res.write(`data: ${new Date().toISOString()}\n\n`);
    }, 1000);
  } else {
    res.writeHead(404);
    res.end();
  }
});

server.listen(3000);
const source = new EventSource('/sse');

source.onmessage = (event) => {
  console.log(event.data);
};

 

아무튼 SSE를 검색해보는데.. jquery 연관은 잘 안보이고

EventSource.onmessage() 로 처리 가능만 하다면야 머...

[링크 : https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events]

 

 

+

golang의 REST / websocket 제거하고 cgi로 돌리려는 어마어마한 음모가! ㅠㅠ

'Programming > web 관련' 카테고리의 다른 글

webGPU  (0) 2023.05.18
chart.js log 스케일  (0) 2023.03.31
chart.js 특정 항목 보이지 않게 하기(가로줄 치기)  (0) 2023.03.10
chart.js 수직 도움선  (0) 2023.01.27
JWT 로그인 예제  (0) 2022.08.24
Posted by 구차니