mirror of
				https://github.com/abhinavxd/libredesk.git
				synced 2025-11-03 21:43:35 +00:00 
			
		
		
		
	feat(messages): add trigram index for text content search - feat: UI animations for conversation and messages list. - Simplify websocket updates.
		
			
				
	
	
		
			51 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			51 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package main
 | 
						|
 | 
						|
import (
 | 
						|
	"fmt"
 | 
						|
 | 
						|
	amodels "github.com/abhinavxd/libredesk/internal/auth/models"
 | 
						|
	"github.com/abhinavxd/libredesk/internal/ws"
 | 
						|
	wsmodels "github.com/abhinavxd/libredesk/internal/ws/models"
 | 
						|
	"github.com/fasthttp/websocket"
 | 
						|
	"github.com/valyala/fasthttp"
 | 
						|
	"github.com/zerodha/fastglue"
 | 
						|
)
 | 
						|
 | 
						|
// ErrHandler is a custom error handler.
 | 
						|
func ErrHandler(ctx *fasthttp.RequestCtx, status int, reason error) {
 | 
						|
	fmt.Printf("error status %d: %s", status, reason)
 | 
						|
}
 | 
						|
 | 
						|
// upgrader is a websocket upgrader.
 | 
						|
var upgrader = websocket.FastHTTPUpgrader{
 | 
						|
	ReadBufferSize:  8192,
 | 
						|
	WriteBufferSize: 8192,
 | 
						|
	CheckOrigin: func(ctx *fasthttp.RequestCtx) bool {
 | 
						|
		return true
 | 
						|
	},
 | 
						|
	Error: ErrHandler,
 | 
						|
}
 | 
						|
 | 
						|
// handleWS handles the websocket connection.
 | 
						|
func handleWS(r *fastglue.Request, hub *ws.Hub) error {
 | 
						|
	var (
 | 
						|
		auser = r.RequestCtx.UserValue("user").(amodels.User)
 | 
						|
		app   = r.Context.(*App)
 | 
						|
	)
 | 
						|
	err := upgrader.Upgrade(r.RequestCtx, func(conn *websocket.Conn) {
 | 
						|
		c := ws.Client{
 | 
						|
			ID:   auser.ID,
 | 
						|
			Hub:  hub,
 | 
						|
			Conn: conn,
 | 
						|
			Send: make(chan wsmodels.WSMessage, 10000),
 | 
						|
		}
 | 
						|
		hub.AddClient(&c)
 | 
						|
		go c.Listen()
 | 
						|
		c.Serve()
 | 
						|
	})
 | 
						|
	if err != nil {
 | 
						|
		app.lo.Error("error upgrading tcp connection", "user_id", auser.ID, "error", err)
 | 
						|
	}
 | 
						|
	return nil
 | 
						|
}
 |