Merge pull request #14 from mariuspass/feature/update-readme
Feature/update readme
This commit is contained in:
@@ -1,57 +1,17 @@
|
|||||||
# recws
|
# recws
|
||||||
|
|
||||||
[](https://goreportcard.com/report/github.com/mariuspass/recws)
|
|
||||||
[](https://github.com/Naereen/StrapDown.js/blob/master/LICENSE)
|
|
||||||
|
|
||||||
Reconnecting WebSocket is a websocket client based on [gorilla/websocket](https://github.com/gorilla/websocket) that will automatically reconnect if the connection is dropped.
|
Reconnecting WebSocket is a websocket client based on [gorilla/websocket](https://github.com/gorilla/websocket) that will automatically reconnect if the connection is dropped.
|
||||||
|
|
||||||
## Basic example
|
[](https://godoc.org/github.com/mariuspass/recws)
|
||||||
|
[](https://goreportcard.com/report/github.com/mariuspass/recws)
|
||||||
|
[](https://github.com/Naereen/StrapDown.js/blob/master/LICENSE)
|
||||||
|
|
||||||
```go
|
## Installation
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
```bash
|
||||||
"context"
|
go get github.com/mariuspass/recws
|
||||||
"github.com/mariuspass/recws"
|
|
||||||
"log"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
ws := recws.RecConn{}
|
|
||||||
ws.Dial("wss://echo.websocket.org", nil)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
time.Sleep(2 * time.Second)
|
|
||||||
cancel()
|
|
||||||
}()
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
go ws.Close()
|
|
||||||
log.Printf("Websocket closed %s", ws.GetURL())
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
if !ws.IsConnected() {
|
|
||||||
log.Printf("Websocket disconnected %s", ws.GetURL())
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := ws.WriteMessage(1, []byte("Incoming")); err != nil {
|
|
||||||
log.Printf("Error: WriteMessage %s", ws.GetURL())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
_, message, err := ws.ReadMessage()
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error: ReadMessage %s", ws.GetURL())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("Success: %s", message)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
recws is open-source software licensed under the [MIT license](https://opensource.org/licenses/MIT).
|
||||||
+3
-1
@@ -9,7 +9,9 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
ws := recws.RecConn{}
|
ws := recws.RecConn{
|
||||||
|
KeepAliveTimeout: 10 * time.Second,
|
||||||
|
}
|
||||||
ws.Dial("wss://echo.websocket.org", nil)
|
ws.Dial("wss://echo.websocket.org", nil)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
|||||||
Reference in New Issue
Block a user