-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement websocket to view the dashboard of top ten product
- Loading branch information
Le Minh Tan
committed
Jul 30, 2021
1 parent
c048aac
commit 02ea940
Showing
5 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,9 @@ | ||
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= | ||
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= | ||
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985 h1:4CSI6oo7cOjJKajidEljs9h+uP0rRZBPPPhcCbj5mw8= | ||
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= | ||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= | ||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package product | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"golang.org/x/net/websocket" | ||
) | ||
|
||
type message struct { | ||
Data string `json:"data"` | ||
Type string `json:"type"` | ||
} | ||
|
||
func productSocket(ws *websocket.Conn) { | ||
done := make(chan struct{}) | ||
fmt.Println("new websocket connection established") | ||
go func(c *websocket.Conn) { | ||
|
||
for { | ||
var msg message | ||
if err := websocket.JSON.Receive(ws, &msg); err != nil { | ||
log.Println(err) | ||
break | ||
} | ||
fmt.Println("received message ", msg.Data) | ||
} | ||
close(done) | ||
}(ws) | ||
|
||
loop: | ||
for { | ||
select { | ||
case <-done: | ||
fmt.Println("connection was closed, lets break out of here") | ||
break loop | ||
default: | ||
products, err := GetTopTenProducts() | ||
if err != nil { | ||
log.Println(err) | ||
break | ||
} | ||
if err := websocket.JSON.Send(ws, products); err != nil { | ||
log.Println(err) | ||
break | ||
} | ||
time.Sleep(10 * time.Second) | ||
} | ||
} | ||
fmt.Println("close websocket") | ||
defer ws.Close() | ||
} |