https://github.com/dnsge/go-basic-websocket
A simple WebSocket client
https://github.com/dnsge/go-basic-websocket
golang websocket websocket-client
Last synced: 5 months ago
JSON representation
A simple WebSocket client
- Host: GitHub
- URL: https://github.com/dnsge/go-basic-websocket
- Owner: dnsge
- License: mit
- Created: 2020-08-20T22:17:12.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-01-26T20:07:52.000Z (over 2 years ago)
- Last Synced: 2024-01-27T21:25:23.357Z (over 2 years ago)
- Topics: golang, websocket, websocket-client
- Language: Go
- Homepage:
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-basic-websocket
Simple WebSocket client
Example program using https://www.websocket.org/echo.html:
```go
package main
import (
"fmt"
"github.com/dnsge/go-basic-websocket"
"net/http"
"sync"
"time"
)
func main() {
// simple echo websocket
headers := http.Header{
"User-Agent": []string{"echo-client/1.0"},
}
ws := basicws.NewBasicWebsocket("wss://echo.websocket.org", headers)
wg := sync.WaitGroup{}
ws.OnConnect = func() {
fmt.Println("Connected to server!")
time.AfterFunc(time.Second*3, func() {
ws.SendString("Hello, world!")
})
}
ws.OnMessage = func(b []byte) error {
fmt.Printf("Received message: %s\n", string(b))
ws.Disconnect()
wg.Done()
return nil
}
wg.Add(1)
_ = ws.Connect()
wg.Wait()
}
```
Output:
```text
Connected to server!
Received message: Hello, world!
```