Skip to content

Commit

Permalink
websocket: update doc for basic example
Browse files Browse the repository at this point in the history
  • Loading branch information
lesismal committed Oct 30, 2023
1 parent 6cf29cb commit 9321c78
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,27 +167,43 @@ package main

import (
"fmt"
"log"
"net/http"

"github.com/lesismal/nbio/nbhttp/websocket"
)

func echo(w http.ResponseWriter, r *http.Request) {
var (
upgrader = newUpgrader()
)

func newUpgrader() *websocket.Upgrader {
u := websocket.NewUpgrader()
u.OnMessage(func(c *websocket.Conn, mt websocket.MessageType, data []byte) {
c.WriteMessage(mt, data)
u.OnOpen(func(c *websocket.Conn) {
// echo
fmt.Println("OnOpen:", c.RemoteAddr().String())
})
u.OnMessage(func(c *websocket.Conn, messageType websocket.MessageType, data []byte) {
// echo
fmt.Println("OnMessage:", messageType, string(data))
c.WriteMessage(messageType, data)
})
_, err := u.Upgrade(w, r, nil)
u.OnClose(func(c *websocket.Conn, err error) {
fmt.Println("OnClose:", c.RemoteAddr().String(), err)
})
return u
}

func onWebsocket(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
panic(err)
}
fmt.Println("Upgraded:", conn.RemoteAddr().String())
}

func main() {
mux := &http.ServeMux{}
mux.HandleFunc("/ws", echo)
mux.HandleFunc("/ws", onWebsocket)
server := http.Server{
Addr: "localhost:8080",
Handler: mux,
Expand Down

0 comments on commit 9321c78

Please sign in to comment.