0
Fork 0
mirror of https://codeberg.org/SafeTwitch/safetwitch-backend.git synced 2024-12-22 13:13:00 -05:00

Implement websocket echo, get ready for IRC chat relay

This commit is contained in:
dragongoose 2023-05-30 10:22:20 -04:00
parent fecf5f0488
commit 0ef2b61d6e
No known key found for this signature in database
GPG key ID: 50DB99B921579009
2 changed files with 37 additions and 1 deletions

View file

@ -0,0 +1,24 @@
package chat
import (
"net"
"github.com/gin-gonic/gin"
"github.com/gobwas/ws/wsutil"
)
func CreateWebsocketServer(conn net.Conn, context *gin.Context) {
defer conn.Close()
for {
msg, op, err := wsutil.ReadClientData(conn)
if err != nil {
break
}
err = wsutil.WriteServerMessage(conn, op, msg)
if err != nil {
break
}
}
}

View file

@ -2,14 +2,26 @@ package root
import (
"safetwitch-backend/extractor"
"safetwitch-backend/extractor/chat"
"github.com/gin-gonic/gin"
"github.com/gobwas/ws"
)
func Routes(route *gin.Engine) {
auth := route.Group("/")
auth.GET("/", func(context *gin.Context) {
upgradeHeader := context.Request.Header.Get("Upgrade")
if upgradeHeader == "websocket" {
conn, _, _, err := ws.UpgradeHTTP(context.Request, context.Writer)
if err != nil {
context.Error(err)
}
go chat.CreateWebsocketServer(conn, context)
} else {
context.JSON(200, extractor.FormatMessage("SafeTwitch backend running", true))
}
})
}