-
-
Save aurodev/f503b0be05538b8f08e2c2bbbd2cbc75 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package main | |
| import ( | |
| "fmt" | |
| "time" | |
| "github.com/gorilla/websocket" | |
| ) | |
| func main() { | |
| URL := "ws://echo.websocket.org/" | |
| var dialer *websocket.Dialer | |
| conn, _, err := dialer.Dial(URL, nil) | |
| if err != nil { | |
| fmt.Println(err) | |
| return | |
| } | |
| go timeWriter(conn) | |
| for { | |
| _, message, err := conn.ReadMessage() | |
| if err != nil { | |
| fmt.Println("read:", err) | |
| return | |
| } | |
| fmt.Printf("received: %s\n", message) | |
| } | |
| } | |
| func timeWriter(conn *websocket.Conn) { | |
| for { | |
| time.Sleep(time.Second * 2) | |
| conn.WriteMessage(websocket.TextMessage, []byte(time.Now().Format("2006-01-02 15:04:05"))) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment