Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/xgfone/go-websocket
Another websocket implementation, which is inspired by https://github.com/noVNC/websockify and https://github.com/gorilla/websocket.
https://github.com/xgfone/go-websocket
go go-websocket golang novnc proxy vnc vnc-proxy websocket websocket-client websocket-library websocket-server websocket-vnc ws
Last synced: about 1 month ago
JSON representation
Another websocket implementation, which is inspired by https://github.com/noVNC/websockify and https://github.com/gorilla/websocket.
- Host: GitHub
- URL: https://github.com/xgfone/go-websocket
- Owner: xgfone
- License: apache-2.0
- Created: 2018-11-05T12:37:06.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-10-19T12:50:36.000Z (about 1 year ago)
- Last Synced: 2024-10-30T20:49:14.603Z (2 months ago)
- Topics: go, go-websocket, golang, novnc, proxy, vnc, vnc-proxy, websocket, websocket-client, websocket-library, websocket-server, websocket-vnc, ws
- Language: Go
- Homepage:
- Size: 85 KB
- Stars: 10
- Watchers: 4
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# websocket [![GoDoc](https://pkg.go.dev/badge/github.com/xgfone/go-websocket)](https://pkg.go.dev/github.com/xgfone/go-websocket) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg?style=flat-square)](https://raw.githubusercontent.com/xgfone/go-websocket/master/LICENSE)
This is a websocket implementation supporting `Go1.7+`, which is inspired by [websockify](https://github.com/novnc/websockify) and [websocket](https://github.com/gorilla/websocket).
#### Difference from `gorilla/websocket`
- `gorilla/websocket` only has a goroutine to read from websocket and a goroutine to write to websocket, that's, read or write can be done concurrently.
- Though this library cannot read from websocket concurrently, it's able to write to websocket concurrently. Moreover, it will be enhanced to read concurrently.## Install
```shell
$ go get -u github.com/xgfone/go-websocket
```## VNC Proxy on WebSocket
The sub-package [vncproxy](https://github.com/xgfone/go-websocket/tree/master/vncproxy) provides a HTTP handler about VNC Proxy on Websocket.
## Example
### Websocket Server Example
```go
package mainimport (
"log"
"net/http"
"time""github.com/xgfone/go-websocket"
)var upgrader = websocket.Upgrader{
MaxMsgSize: 1024,
Timeout: time.Second * 30,
CheckOrigin: func(r *http.Request) bool { return true },
}func websocketHandler(rw http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(rw, r, nil)
if err != nil {
log.Printf("failed to upgrade to websocket: %s\n", err)
return
}ws.Run(func(msgType int, message []byte) {
switch msgType {
case websocket.MsgTypeBinary:
ws.SendBinaryMsg(message)
case websocket.MsgTypeText:
ws.SendTextMsg(message)
}
})
}func main() {
http.ListenAndServe(":80", http.HandlerFunc(websocketHandler))
}
```### Websocket Client Example
```go
package mainimport (
"fmt"
"time""github.com/xgfone/go-websocket"
)func main() {
ws, err := websocket.NewClientWebsocket("ws://127.0.0.1/")
if err == nil {
go func() {
tick := time.NewTicker(time.Second * 10)
defer tick.Stop()
for {
select {
case now := <-tick.C:
if err := ws.SendTextMsg([]byte(now.String())); err != nil {
fmt.Println(err)
return
}
}
}
}()err = ws.Run(func(msgType int, msg []byte) {
fmt.Printf("Receive: %s\n", string(msg))
})
}
fmt.Println(err)
}
```The client will output like this:
```
Receive: 2019-07-13 18:33:47.951688 +0800 CST m=+10.007139340
Receive: 2019-07-13 18:33:57.951479 +0800 CST m=+20.006605995
Receive: 2019-07-13 18:34:07.948628 +0800 CST m=+30.003442484
Receive: 2019-07-13 18:34:17.949763 +0800 CST m=+40.004270178
Receive: 2019-07-13 18:34:27.947877 +0800 CST m=+50.002081112
Receive: 2019-07-13 18:34:37.949986 +0800 CST m=+60.003888082
...
```