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:
parent
fecf5f0488
commit
0ef2b61d6e
2 changed files with 37 additions and 1 deletions
24
extractor/chat/chatRelayServer.go
Normal file
24
extractor/chat/chatRelayServer.go
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue