From d029f56262836345e41c943c61f3d97052270ccf Mon Sep 17 00:00:00 2001 From: pnguyen215 Date: Sun, 23 Jul 2023 18:11:41 +0700 Subject: [PATCH] :ok_hand: chore: update readme.md #2 --- README.md | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d3a8057..4e210ce 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,35 @@ # WsConn -## Example Publish Event \ No newline at end of file +## Example Publish Event + +```go +package main + +import ( + "net/http" + "time" + + "github.com/gin-gonic/gin" + "github.com/sivaosorg/govm/wsconnx" + "github.com/sivaosorg/wsconn/wsconn" +) + +func main() { + r := gin.Default() + ws := wsconn.NewWebsocket() + s := wsconn.NewWebsocketService(ws) + + r.GET("/subscribe", s.SubscribeMessage) // ws://localhost:8081/subscribe + r.POST("/message", func(c *gin.Context) { + var message wsconnx.WsConnMessagePayload + message.SetGenesisTimestamp(time.Now()) + if err := c.ShouldBindJSON(&message); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request payload"}) + return + } + s.BroadcastMessage(message) + c.JSON(http.StatusOK, gin.H{"message": "Message sent successfully", "data": message}) + }) + r.Run(":8081") +} +```