Skip to content

Instantly share code, notes, and snippets.

@aasumitro
Created July 27, 2023 03:14
Show Gist options
  • Select an option

  • Save aasumitro/4404506bed46a25529d722f1ff944267 to your computer and use it in GitHub Desktop.

Select an option

Save aasumitro/4404506bed46a25529d722f1ff944267 to your computer and use it in GitHub Desktop.
func (handler *designerHandler) Notify(ctx *fiber.Ctx) error {
ctx.Set(fiber.HeaderContentType, "text/event-stream")
ctx.Set(fiber.HeaderCacheControl, "no-cache")
ctx.Set(fiber.HeaderConnection, "keep-alive")
ctx.Set(fiber.HeaderTransferEncoding, "chunked")
ctx.Context().SetBodyStreamWriter(func(w *bufio.Writer) {
for {
if handler.q.Length() == 0 {
time.Sleep(time.Second * 1)
continue
}
handler.mu.Lock()
item := handler.q.Remove()
if item == nil {
log.Printf("ERR_QUEUE: failed to get message\n")
handler.mu.Unlock()
return
}
if _, err := fmt.Fprintf(w, "data: %s\n\n", *item); err != nil {
log.Printf("ERR_WRITE: %v\n", err)
handler.mu.Unlock()
return
}
if err := w.Flush(); err != nil {
log.Printf("ERR_FLUSH: %v\n", err)
handler.mu.Unlock()
return
}
handler.mu.Unlock()
}
})
return nil
}
let sseSource = null;
const apiURL = "http://localhost:3000/designer";
const sseURL = `${apiURL}/notifications`;
const confURL = `${apiURL}/configurations`;
document.addEventListener('DOMContentLoaded', () => {
if(typeof(EventSource) !== "undefined") {
sseSource = new EventSource(sseURL);
sseSource.addEventListener("message", function(event) {
const data = event.data;
showNotificationDialog(data);
});
sseSource.addEventListener('error', function() {
if (sseSource.readyState === EventSource.CLOSED) {
sseSource = new EventSource(sseURL);
}
});
} else {
alert("Sorry, your browser does not support server-sent events...");
}
document.getElementById('copyright').textContent = `
COPYRIGHT © ${new Date().getFullYear()} SKILLEDIN PTE. LTD. ALL RIGHTS RESERVED.
`;
});
@aasumitro
Copy link
Author

data, err := cred.GetToken(context.Background(), policy.TokenRequestOptions{
Scopes: []string{"https://graph.microsoft.com/.default"},
})
fmt.Println(data.Token, err)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment