第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在收到 os.Interrupt后從服務(wù)器向客戶(hù)端發(fā)送信號(hào)

如何在收到 os.Interrupt后從服務(wù)器向客戶(hù)端發(fā)送信號(hào)

Go
滄海一幻覺(jué) 2023-01-03 14:08:43
在我的代碼中,我os.Interrupt在關(guān)閉之前收聽(tīng)了以下內(nèi)容package mainimport (    "net/http"    "os"    "os/signal"    "syscall")var passer *DataPasserconst maxClients = 1func init() {    passer = &DataPasser{        data:       make(chan sseData),        logs:       make(chan string),        connection: make(chan struct{}, maxClients),    }}func main() {    http.HandleFunc("/sse", passer.HandleSignal)    go http.ListenAndServe(":1234", nil)    // Listen to Ctrl+C (you can also do something else that prevents the program from exiting)    c := make(chan os.Signal, 1)    signal.Notify(c, os.Interrupt, syscall.SIGTERM)    <-c    if client.IsConnected() {        client.Disconnect()    }}這passer.HandleSignal是一個(gè)將 SSE 發(fā)送Server Sent Event到客戶(hù)端的函數(shù),如下所示:package mainimport (    "fmt"    "net/http")type sseData struct {    event, message string}type DataPasser struct {    data       chan sseData    connection chan struct{} // To control maximum allowed clients connections}func (p *DataPasser) HandleSignal(w http.ResponseWriter, r *http.Request) {    w.Header().Set("Content-Type", "text/event-stream; charset=utf-8")    w.Header().Set("Cache-Control", "no-cache")    w.Header().Set("Connection", "keep-alive")    fmt.Println("Client connected from IP:", r.RemoteAddr)    p.connection <- struct{}{}    flusher, ok := w.(http.Flusher)    if !ok {        http.Error(w, "Internal error", 500)        return    }    fmt.Fprint(w, "event: notification\ndata: Connection to WhatsApp server ...\n\n")    flusher.Flush()    // Connect to the WhatsApp client    go Connect()    for {        select {        case data := <-p.data:            fmt.Println("recieved")        我的問(wèn)題是,如果用戶(hù)使用 Ctrl+C 中斷了應(yīng)用程序,該main函數(shù)正在響應(yīng)它,我需要它向它發(fā)送數(shù)據(jù),parser以便處理程序向客戶(hù)端發(fā)送服務(wù)器已關(guān)閉的通知
查看完整描述

1 回答

?
HUX布斯

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超6個(gè)贊

我找到了解決方案,它很簡(jiǎn)單,只需將日期傳遞給parser在 os.interupt 范圍內(nèi)定義為全局變量的對(duì)象,如下所示:


    <-c

    if client.IsConnected() {

        passer.data <- sseData{

            event:   "notification",

            message: "Server is shut down at the host machine...",

        }

        client.Disconnect()

    }


查看完整回答
反對(duì) 回復(fù) 2023-01-03
  • 1 回答
  • 0 關(guān)注
  • 92 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)