https://github.com/gbrlsnchs/websocket
Go WebSocket client and server implementation
https://github.com/gbrlsnchs/websocket
go golang websocket websocket-client websocket-server websockets
Last synced: about 1 year ago
JSON representation
Go WebSocket client and server implementation
- Host: GitHub
- URL: https://github.com/gbrlsnchs/websocket
- Owner: gbrlsnchs
- License: mit
- Created: 2018-08-20T21:25:30.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-01-10T16:24:49.000Z (over 7 years ago)
- Last Synced: 2025-02-07T13:54:09.004Z (over 1 year ago)
- Topics: go, golang, websocket, websocket-client, websocket-server, websockets
- Language: Go
- Homepage:
- Size: 73.2 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# websocket (WebSocket client and server for Go)
## About
This is an easy-to-use WebSocket client and server implementation in [Go](https://golang.org).
It passes the [Autobahn Test Suite](https://crossbar.io/autobahn/testsuite/).
## Usage
Full documentation [here](https://godoc.org/github.com/gbrlsnchs/websocket).
### Installing
#### Go 1.10
`vgo get -u github.com/gbrlsnchs/websocket`
#### Go 1.11
`go get -u github.com/gbrlsnchs/websocket`
### Importing
```go
import (
// ...
"github.com/gbrlsnchs/websocket"
)
```
## Examples
### Upgrading an HTTP request and listening to messages
```go
func upgradingHandler(w http.ResponseWriter, r *http.Request) {
ws, err := websocket.UpgradeHTTP(w, r)
if err != nil {
// handle error
}
for ws.Next() {
payload, opcode := ws.Message()
ws.SetOpcode(opcode)
ws.Write(payload)
}
if err := ws.Err(); err != nil {
fmt.Println(err)
}
fmt.Println(ws.CloseCode())
}
```
### Openning connection to a WebSocket server (client mode)
```go
ws, err := websocket.Open("ws://echo.websocket.org", 15*time.Second)
if err != nil {
// handle error
}
ws.Write([]byte("Hello, WebSocket!"))
for ws.Next() {
payload, _ := ws.Message()
fmt.Printf("Message sent by server: %s\n", payload)
}
if err := ws.Err(); err != nil {
fmt.Println(err)
}
fmt.Println(ws.CloseCode())
```
## Contributing
### How to help
- For bugs and opinions, please [open an issue](https://github.com/gbrlsnchs/websocket/issues/new)
- For pushing changes, please [open a pull request](https://github.com/gbrlsnchs/websocket/compare)