-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (62 loc) · 1.4 KB
/
main.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
package main
import (
"github.com/labstack/echo/v4"
"github.com/myOmikron/echotools/worker"
"github.com/pnp-zone/common/conf"
"golang.org/x/net/websocket"
"gorm.io/gorm"
"pnp-chat/broadcast"
)
var broadcastServer = broadcast.NewServer()
func chatHandler(c echo.Context) error {
websocket.Handler(func(ws *websocket.Conn) {
defer func() {
err := ws.Close()
if err != nil {
c.Logger().Debug(err)
}
}()
socket := NewWebSocket(c, ws)
room := make(chan interface{}, 8)
group := broadcastServer.GetGroup("test")
group.Subscribe(room)
defer group.Unsubscribe(room)
for {
select {
case msg, ok := <-socket.Received:
if ok {
group.Publish(msg)
} else {
return
}
case msg := <-room:
socket.Send(msg.(string))
}
}
}).ServeHTTP(c.Response(), c.Request())
return nil
}
func MigrationHook() func(db *gorm.DB) error {
return func(db *gorm.DB) error {
return nil
}
}
func RouterHook() func(e *echo.Echo, db *gorm.DB, config *conf.Config) error {
return func(e *echo.Echo, db *gorm.DB, config *conf.Config) error {
e.GET("chat/socket", chatHandler)
return nil
}
}
func StaticFileHook() (string, string) {
return "main.js", "main.css"
}
func WorkerPoolHook() func(worker.Pool) error {
return func(wp worker.Pool) error {
go broadcastServer.Start()
return nil
}
}
var _ = MigrationHook
var _ = RouterHook
var _ = StaticFileHook
var _ = WorkerPoolHook