example_test.go 572 B

1234567891011121314151617181920212223242526272829303132
  1. package sse
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "testing"
  6. "time"
  7. )
  8. func TestSSE(t *testing.T) {
  9. router := gin.Default()
  10. stream := NewServer()
  11. user := "18926340"
  12. go func() {
  13. for {
  14. time.Sleep(time.Second * 3)
  15. now := time.Now().Format("2006-01-02 15:04:05")
  16. currentTime := fmt.Sprintf("The Current Time Is %v", now)
  17. stream.Push(user, "message", currentTime)
  18. }
  19. }()
  20. router.GET("/stream", stream.HandlerFunc(func(c *gin.Context) (string, error) {
  21. return user, nil
  22. }))
  23. router.StaticFile("/", "./client.html")
  24. _ = router.Run(":8085")
  25. }