php小编鱼仔为您介绍如何注册新的websocket连接gooptic。GoOptic是一个开源的Go语言websocket库,用于构建高性能、实时通信的应用程序。要注册新的websocket连接,首先需要在Go程序中导入GoOptic库。然后,可以使用GoOptic提供的函数来创建websocket服务器,并指定要监听的地址和端口。接下来,可以使用GoOptic提供的HandleFunc函数来处理websocket连接的各种事件,例如接收消息、发送消息等。最后,调用GoOptic提供的Serve函数来启动websocket服务器,使其可以接受新的连接。通过这些步骤,您就可以成功注册新的websocket连接gooptic。
我正在尝试设置一个简单的 websocket 服务器,该服务器应该以未知的时间间隔为客户端提供一些内容。
我的代码目前如下所示:
router.go
func setuproutes(app *fiber.app) error { app.get("/whop/validate", handler.handlewhopvalidate) /*other non-websocket routes*/ /*...*/ app.get("/ws/monitor", websocket.new(wshandler.handlewsmonitor)) app.use(func(c *fiber.ctx) error { c.sendstatus(404) return c.next() }) return nil }
handler.go
package handlers import ( "fmt" "log" "github.com/gofiber/websocket/v2" ) var register = make(chan *websocket.conn) var unregister = make(chan *websocket.conn) func handlewsmonitor(c *websocket.conn) { go socketlistener() defer func() { unregister <- c //may need to check whether connection is already closed before re-closing? c.close() }() //sends conn into channel register <- c for { messagetype, message, err := c.readmessage() if err != nil { if websocket.isunexpectedcloseerror(err, websocket.closegoingaway, websocket.closeabnormalclosure) { log.println("read error:", err) } return } if messagetype == websocket.textmessage { log.println("got textmessage:", string(message)) } else { log.println("received message of type:", messagetype) } } } func socketlistener() { for { select { case c := <-register: messagetype, message, err := c.readmessage() if err != nil { log.println(err) unregister <- c return } fmt.printf("got message of type: %d\nmessage:%s\n", messagetype, string(message)) fmt.printf("connection params: %s\n", c.params("id")) //append to list of co case c := <-unregister: //remove conection from list of clients c.close() fmt.printf("closed connection\n") } } }
我遇到的问题是,当我连接到 websocket 时,我的注册选择案例未命中(我想使用之前提供给客户端的 uuid 将客户端连接注册到地图) .
client.go
package main import ( "flag" "log" "net/url" "github.com/fasthttp/websocket" ) type Client struct { C *websocket.Conn } func main() { addr := flag.String("addr", "localhost:8080", "http service address") u := url.URL{ Scheme: "ws", Host: *addr, Path: "/ws/monitor", RawQuery: "id=12", } wsClient := &Client{} log.Printf("connecting to %s\n", u.String()) // Connect to the WebSocket server conn, resp, err := websocket.DefaultDialer.Dial(u.String(), nil) if err != nil { log.Fatal("Dial:", err) } wsClient.C = conn if resp != nil { log.Println("Got response:", resp) } defer wsClient.closeConn() } func (client *Client) closeConn() { err := client.C.WriteMessage( websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""), ) if err != nil { log.Println("Write close:", err) return } client.C.Close() log.Println("Connection closed") }
handler.go 中是否缺少某些内容,或者我在使用客户端连接到服务器时是否应该采取不同的方法?
根据我的测试,寄存器的选择案例确实命中(我使用的代码附在这个答案的底部)。
但是我在代码中发现了其他问题:
unregister
chan 是无缓冲的,socketlistener
中的 unregister <- c
将被阻塞。当代码到达 unregister <- c
时,它和 case c := <-unregister
之间出现死锁。socketlistener
goroutine。如果是这种情况,应将其移至 handlewsmonitor
之外。handlewsmonitor
和 socketlistener
都从连接中读取。 socketlistener
的职责是什么?看来它不应该从连接中读取。再想一想,好像可以直接在handlewsmonitor
中的地图上添加连接和删除连接。 socketlistener
可以完全删除。简单性应该是设计的一个关键目标。请参阅kiss 原则。
package main import ( "log" "github.com/gofiber/fiber/v2" "github.com/gofiber/websocket/v2" ) var ( register = make(chan *websocket.Conn) unregister = make(chan *websocket.Conn) ) func main() { // Make it easy to find out which line prints the log. log.SetFlags(log.Lshortfile) app := fiber.New() app.Get("/ws/monitor", websocket.New(HandleWsMonitor)) log.Fatal(app.Listen(":8080")) } func HandleWsMonitor(c *websocket.Conn) { // It seems the we only need one SocketListener goroutine for the whole server. // If this is the case, the next line should be moved outside of this func. go SocketListener() defer func() { unregister <- c c.Close() }() register <- c for { messageType, message, err := c.ReadMessage() if err != nil { if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) { log.Println("read error:", err) } return } if messageType == websocket.TextMessage { log.Println("got textmessage:", string(message)) } else { log.Println("received message of type:", messageType) } } } func SocketListener() { for { select { case c := <-register: // This did appear in the log. log.Println("case c := <-register") messageType, message, err := c.ReadMessage() if err != nil { log.Println(err) // unregister is unbuffered, the sending will be blocked. unregister <- c // If we use only one SocketListener goroutine then it should // not return here. return } log.Printf("Got message of type: %d\nMessage:%s\n", messageType, string(message)) log.Printf("Connection Params: %s\n", c.Params("id")) case c := <-unregister: c.Close() log.Println("Closed connection") } } }
以上是如何注册新的 websocket 连接 gooptic的详细内容。更多信息请关注PHP中文网其他相关文章!