You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1022 B
50 lines
1022 B
|
1 year ago
|
package webstocetmsg
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"log"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/gin-gonic/gin"
|
||
|
|
"github.com/gorilla/websocket"
|
||
|
|
"github.com/sirupsen/logrus"
|
||
|
|
)
|
||
|
|
|
||
|
|
func (a *ApiMethod) WebsocketDemo(c *gin.Context) {
|
||
|
|
ws, err := upGraderes.Upgrade(c.Writer, c.Request, nil)
|
||
|
|
fmt.Printf("err--1->%v\n", err)
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
defer func(ws *websocket.Conn) {
|
||
|
|
err = ws.Close()
|
||
|
|
if err != nil {
|
||
|
|
log.Fatalln(err)
|
||
|
|
}
|
||
|
|
}(ws)
|
||
|
|
MsgHandler(c, ws)
|
||
|
|
}
|
||
|
|
|
||
|
|
func MsgHandler(c *gin.Context, ws *websocket.Conn) {
|
||
|
|
// fmt.Printf("err--2->%s\n", ws)
|
||
|
|
for {
|
||
|
|
|
||
|
|
msg, err := Subscribe(c, PublishKey)
|
||
|
|
fmt.Printf("err--2->%v--2->%v\n", msg, err)
|
||
|
|
if err != nil {
|
||
|
|
logrus.Errorf("Subscribe error: %s", err.Error())
|
||
|
|
fmt.Printf("Subscribe error: %s\n", err.Error())
|
||
|
|
}
|
||
|
|
|
||
|
|
tm := time.Now().Format("2006-01-02 15:04:05")
|
||
|
|
fmt.Printf("tm--->%v\n", tm)
|
||
|
|
m := fmt.Sprintf("[ws][%s]:%s", tm, msg)
|
||
|
|
err = ws.WriteMessage(1, []byte(m))
|
||
|
|
fmt.Printf("m--->%v\n", m)
|
||
|
|
fmt.Printf("err--->%v\n", err)
|
||
|
|
if err != nil {
|
||
|
|
log.Fatalln(err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|