黄梓健
5 years ago
7 changed files with 234 additions and 0 deletions
-
4.gitignore
-
5channel.go
-
2envelope.go
-
36examples/channel/main.go
-
51hub.go
-
57melody.go
-
79session.go
@ -0,0 +1,5 @@ |
|||
package melody |
|||
|
|||
type Channel struct { |
|||
|
|||
} |
@ -0,0 +1,36 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"log" |
|||
"melody" |
|||
|
|||
"github.com/gin-gonic/gin" |
|||
) |
|||
|
|||
func main() { |
|||
m := melody.New() |
|||
router := gin.Default() |
|||
router.GET("/chat", func(context *gin.Context) { |
|||
if err := m.HandleRequest(context.Writer, context.Request); err != nil { |
|||
log.Println(err) |
|||
} |
|||
}) |
|||
|
|||
m.HandleConnect(func(session *melody.Session) { |
|||
ch := session.Request.URL.Query().Get("channel") |
|||
if err := session.Subscribe(ch); err != nil { |
|||
log.Println(err) |
|||
} |
|||
}) |
|||
|
|||
m.HandleMessage(func(session *melody.Session, msg []byte) { |
|||
if err := session.Publish(msg); err != nil { |
|||
log.Println(err) |
|||
} |
|||
}) |
|||
|
|||
m.HandleSentMessage(func(session *melody.Session, bytes []byte) { |
|||
log.Printf("%+v", string(bytes)) |
|||
}) |
|||
router.Run(":8080") |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue