-
Notifications
You must be signed in to change notification settings - Fork 14
/
websocket_controller.go
74 lines (59 loc) · 1.67 KB
/
websocket_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package controller
import (
"encoding/json"
"net/http"
"github.com/coretrix/hitrix"
model "github.com/coretrix/hitrix/example/model/socket"
"github.com/coretrix/hitrix/service"
"github.com/coretrix/hitrix/service/component/socket"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
)
var upGrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
EnableCompression: true,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
type DTOMessage struct {
Type string
SocketID string
Data interface{}
}
type WebsocketController struct {
}
//if u want to test with websocket-demo.html u should add it to CORS policy
func (controller *WebsocketController) InitConnection(c *gin.Context) {
ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
panic(err)
}
socketRegistryService := service.DI().SocketRegistry()
errorLogger := service.DI().ErrorLogger()
connection := &socket.Connection{Send: make(chan []byte, 256), Ws: ws}
socketHolder := &socket.Socket{
ErrorLogger: errorLogger,
Connection: connection,
ID: "unique connection hash based on userID, deviceID and timestamp",
Namespace: model.DefaultWebsocketNamespace,
}
socketRegistryService.Register <- socketHolder
hitrix.Goroutine(func() {
socketHolder.WritePump()
})
hitrix.Goroutine(func() {
socketHolder.ReadPump(socketRegistryService, func(rawData []byte) {
dto := &DTOMessage{}
err = json.Unmarshal(rawData, dto)
if err != nil {
errorLogger.LogError(err)
return
}
//return back the received message
s, _ := socketRegistryService.Sockets.Load(socketHolder.ID)
s.(*socket.Socket).Emit(dto)
})
})
}