An open API service indexing awesome lists of open source software.

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

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!
```