-
-
Save lesismal/316b711a7f39cc539cebaad6c8e5b0fd to your computer and use it in GitHub Desktop.
listener_wraper.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"net" | |
"net/http" | |
"os" | |
"os/signal" | |
"sync" | |
"time" | |
"github.com/lesismal/nbio/nbhttp" | |
) | |
var connections = sync.Map{} | |
func nbioOnEcho(w http.ResponseWriter, r *http.Request) { | |
res, ok := w.(*nbhttp.Response) | |
if ok { | |
conn := res.Parser.Processor.Conn() | |
yourData, ok := connections.Load(conn) | |
if ok { | |
fmt.Println("nbioServer onEcho:", yourData) | |
} else { | |
fmt.Println("nbioServer onEcho: not found connection") | |
} | |
} | |
w.Write([]byte(time.Now().Format("20060102 15:04:05"))) | |
} | |
func nbioServer() { | |
mux := &http.ServeMux{} | |
mux.HandleFunc("/", nbioOnEcho) | |
engine := nbhttp.NewServer(nbhttp.Config{ | |
Network: "tcp", | |
Addrs: []string{"localhost:8080"}, | |
Handler: mux, | |
IOMod: nbhttp.IOModBlocking, | |
}) | |
engine.OnOpen(func(conn net.Conn) { | |
fmt.Println("nbioServer onOpen:", conn.RemoteAddr()) | |
yourData := "data: " + conn.RemoteAddr().String() // or other things | |
connections.Store(conn, yourData) | |
}) | |
engine.OnClose(func(conn net.Conn, err error) { | |
connections.Delete(conn) | |
fmt.Println("nbioServer onClose:", conn.RemoteAddr(), err) | |
}) | |
err := engine.Start() | |
if err != nil { | |
fmt.Printf("nbio.Start failed: %v\n", err) | |
return | |
} | |
interrupt := make(chan os.Signal, 1) | |
signal.Notify(interrupt, os.Interrupt) | |
<-interrupt | |
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) | |
defer cancel() | |
engine.Shutdown(ctx) | |
} | |
func netOnEcho(w http.ResponseWriter, r *http.Request) { | |
yourData, ok := connections.Load(r.RemoteAddr) | |
if ok { | |
fmt.Println("netServer onEcho:", yourData) | |
} else { | |
fmt.Println("netServer onEcho: not found connection " + r.RemoteAddr) | |
} | |
w.Write([]byte(time.Now().Format("20060102 15:04:05"))) | |
} | |
func netServer() { | |
mux := &http.ServeMux{} | |
mux.HandleFunc("/", netOnEcho) | |
server := &http.Server{ | |
Addr: "localhost:8080", | |
Handler: mux, | |
ConnState: func(conn net.Conn, state http.ConnState) { | |
switch state { | |
case http.StateNew: | |
fmt.Println("netServer onOpen:", conn.RemoteAddr()) | |
remoteAddr := conn.RemoteAddr().String() | |
yourData := "data: " + remoteAddr // or other things | |
connections.Store(remoteAddr, yourData) | |
case http.StateActive: | |
case http.StateIdle: | |
case http.StateHijacked: | |
case http.StateClosed: | |
connections.Delete(conn) | |
fmt.Println("netServer onClose:", conn.RemoteAddr()) | |
} | |
}, | |
} | |
err := server.ListenAndServe() | |
fmt.Println("server exit:", err) | |
} | |
func main() { | |
netServer() | |
// nbioServer() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output: