iris icon indicating copy to clipboard operation
iris copied to clipboard

How Can I Get Websocket Timeout Err Message Or Event

Open fanglina opened this issue 2 years ago • 2 comments

Hey Bro, Thank you for your project.

Is there a way to get errors? When the client disconnects, let me know whether it is a timeout disconnection or something else

ws := websocket.New(gorilla.Upgrader(
		gorillaWs.Upgrader{CheckOrigin: func(*http.Request) bool { return true }},
	), websocket.Events{
		websocket.OnNativeMessage: func(nsConn *websocket.NSConn, msg websocket.Message) error {
			Listen.WS = nsConn.Conn.Server()
			Listen.Ctx = websocket.GetContext(nsConn.Conn) //todo
			Listen.Get(msg, nsConn.Conn.ID())
			nsConn.Conn.Socket().NetConn().SetReadDeadline(time.Now().Add(1 * time.Minute))
			return nil
		},
		//websocket.OnAnyEvent 不可用
	})
	ws.FireDisconnectAlways = true

	ws.OnUpgradeError = func(err error) {
		util.GetWsLog().Infof("设备连接失败 [%s]", err.Error())
	}
	ws.OnConnect = func(c *websocket.Conn) error {
		util.GetWsLog().Infof("Websocket连接 [%s]", c.ID())
		return nil
	}

	ws.OnDisconnect = Listen.onLeave

	util.GetWsLog().Infof("websocketController:%p", Listen)
	go Listen.RedisReadMsg()

	app.Get("/ws", websocket.Handler(ws))

fanglina avatar Sep 20 '22 05:09 fanglina

Hey @fanglina, I guess this is the wrong repo...? You should be looking for this repo, right?

dudmz avatar Nov 04 '22 13:11 dudmz

Hello @fanglina, thank YOU for using it.

Sorry for the late response.

You should receive websocket connection errors on the OnUpgradeError listener and inside all event listeners accepting the websocket.Message instance, which holds the Err field. After that you can check if the provided error's type is a timeout error. Iris provides the isCanceled := iris.IsErrCanceled(err) which you can use directly or copy its code to your own utility source file.

Please reply if the suggested solution works for you or we should implement something else for this case.

kataras avatar Nov 28 '22 16:11 kataras