๐ A dead simple WebSocket connection written in Go.
websocket-conn provides you with easy handling for WebSockets, it's based on github.com/gorilla/websocket.
$ go get -u github.com/shiwano/websocket-conn/v4
func Connect(ctx context.Context, settings Settings, url string, requestHeader http.Header) (*Conn *http.Response, error)
func UpgradeFromHTTP(ctx context.Context, settings Settings, w http.ResponseWriter, r *http.Request) (*Conn, error)
type Conn struct {
Stream() <-chan Message
Err() error
SendBinaryMessage(data []byte) error
SendTextMessage(text string) error
SendJSONMessage(v interface{}) error
Close() error
}
Server:
package main
import (
"context"
"net/http"
wsconn "github.com/shiwano/websocket-conn/v4"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
c, _ := wsconn.UpgradeFromHTTP(r.Context(), wsconn.DefaultSettings(), w, r)
for m := range c.Stream() {
switch t := m.Text(); t {
case "Hello":
if err := c.SendTextMessage(t + " World"); err != nil {
log.Fatal(err)
}
case "Close":
c.Close()
}
}
})
http.ListenAndServe(":5000", nil)
}
Client:
package main
import (
"context"
"log"
wsconn "github.com/shiwano/websocket-conn/v4"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c, _, _ := wsconn.Connect(ctx, wsconn.DefaultSettings(), "ws://localhost:5000", nil)
c.SendTextMessage("Hello")
m := <-c.Stream()
log.Println(m.Text()) // Output: Hello World
c.SendTextMessage("Close")
for range c.Stream() {
// wait for closing.
}
}
See also examples directory.
Copyright (c) 2016 Shogo Iwano Licensed under the MIT license.