File tree Expand file tree Collapse file tree 3 files changed +80
-0
lines changed Expand file tree Collapse file tree 3 files changed +80
-0
lines changed Original file line number Diff line number Diff line change 9
9
"github.com/Euler-B/API-REST_Go/handlers"
10
10
"github.com/Euler-B/API-REST_Go/middleware"
11
11
"github.com/Euler-B/API-REST_Go/server"
12
+ "github.com/Euler-B/API-REST_Go/websocket"
12
13
"github.com/gorilla/mux"
13
14
"github.com/joho/godotenv"
14
15
)
@@ -38,6 +39,8 @@ func main() {
38
39
}
39
40
40
41
func BindRoutes (s server.Server , r * mux.Router ) {
42
+ hub := websocket .NewHub ()
43
+
41
44
r .Use (middleware .CheckAuthMiddleware (s ))
42
45
43
46
r .HandleFunc ("/" , handlers .HomeHandler (s )).Methods (http .MethodGet )
@@ -51,4 +54,6 @@ func BindRoutes(s server.Server, r *mux.Router) {
51
54
r .HandleFunc ("/posts/{id}" , handlers .UpdatePostHandler (s )).Methods (http .MethodPut )
52
55
r .HandleFunc ("/posts/{id}" , handlers .DeletePostHandler (s )).Methods (http .MethodDelete )
53
56
r .HandleFunc ("/posts" , handlers .ListPostHandler (s )).Methods (http .MethodGet )
57
+
58
+ r .HandleFunc ("/ws" , hub .HandleWebSocket (s ))
54
59
}
Original file line number Diff line number Diff line change
1
+ package websocket
2
+
3
+ import (
4
+ "github.com/gorilla/websocket"
5
+ "golang.org/x/text/message"
6
+ )
7
+
8
+ type Client struct {
9
+ hub * Hub
10
+ id string
11
+ socket * websocket.Conn
12
+ outbound chan []byte
13
+ }
14
+
15
+ func NewClient (hub * Hub , socket * websocket.Conn ) * Client {
16
+ return & Client {
17
+ hub : hub ,
18
+ socket : socket ,
19
+ outbound : make (chan []byte ),
20
+ }
21
+ }
22
+
23
+ func (c * Client ) Write () {
24
+ for {
25
+ select {
26
+ case message , ok : <- c .outbound :
27
+ if ! ok {
28
+ c .socket .WriteMessage (websocket .CloseMessage , []byte {})
29
+ return
30
+ }
31
+ c .socket .WriteMessage (websocket .TextMessage , message )
32
+
33
+ }
34
+ }
Original file line number Diff line number Diff line change
1
+ package websocket
2
+
3
+ import (
4
+ "http"
5
+ "log"
6
+ "net/http"
7
+ "sync"
8
+
9
+ "github.com/gorilla/websocket"
10
+ )
11
+
12
+ var upgrader = websocket.Upgrader {
13
+ CheckOrigin : func (r * http.Request ) bool { return true },
14
+ }
15
+
16
+ type Hub struct {
17
+ clients []* Client
18
+ register chan * Client
19
+ unregister chan * Client
20
+ mutex * sync.Mutex
21
+ }
22
+
23
+ func NewHub () * Hub {
24
+ return & Hub {
25
+ clients : make ([]* Client , 0 ),
26
+ register : make (chan * Client ),
27
+ mutex : & sync.Mutex {},
28
+ }
29
+ }
30
+
31
+ func (hub * Hub ) HandleWebSocket (w http.ResponseWriter , r * http.Request ) {
32
+ socket , err := upgrader .Upgrade (w , r , nil )
33
+ if err != nil {
34
+ log .Println (err )
35
+ http .Error (w , "Could not open websocket connection" , http .StatusBadRequest )
36
+ }
37
+ client := NewClient (hub , socket )
38
+ hub .register <- client
39
+
40
+ go client .Write ()
41
+ }
You can’t perform that action at this time.
0 commit comments