Skip to content

Instantly share code, notes, and snippets.

@syfun
Created June 25, 2018 02:43
Show Gist options
  • Save syfun/cd6af58dd6412760653cfc6a1866226e to your computer and use it in GitHub Desktop.
Save syfun/cd6af58dd6412760653cfc6a1866226e to your computer and use it in GitHub Desktop.
package main
import (
"log"
"github.com/gorilla/websocket"
)
type Message struct {
Action string `json:"action"`
Data interface{} `json:"data,omitempty"`
}
func main() {
conn, _, err := websocket.DefaultDialer.Dial("ws://localhost:10001/ws", nil)
if err != nil {
log.Fatal("dial: ", err)
}
defer conn.Close()
msg := Message{"connect", "hello"}
conn.WriteJSON(msg)
var rst Message
conn.ReadJSON(&rst)
log.Printf("recv: %s", rst)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment