Skip to content

Commit

Permalink
add lint action
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNero committed May 10, 2021
1 parent 843775b commit b41b93f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 20 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: golangci-lint
on:
push:
pull_request:
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.29
26 changes: 26 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
run:
concurrency: 10
tests: true
skip-dirs-use-default: false
timeout: 5m

linters-settings:
golint:
min-confidence: 0.8
gofmt:
simplify: true

linters:
enable:
- gofmt
- gosimple
- govet
- unconvert
- misspell
- goimports
- golint
disable-all: false
fast: false

issues:
exclude-use-default: false
11 changes: 6 additions & 5 deletions examples/gowebsocket/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package main

import (
"github.com/sacOO7/gowebsocket"
"log"
"os"
"os/signal"

"github.com/sacOO7/gowebsocket"
)

func main() {
Expand All @@ -25,20 +26,19 @@ func main() {
socket.RequestHeader.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36")

socket.OnConnectError = func(err error, socket gowebsocket.Socket) {
log.Fatal("Recieved connect error ", err)
log.Fatal("Received connect error ", err)
}
socket.OnConnected = func(socket gowebsocket.Socket) {
log.Println("Connected to server")
}
socket.OnTextMessage = func(message string, socket gowebsocket.Socket) {
log.Println("Recieved message " + message)
log.Println("Received message " + message)
}
socket.OnPingReceived = func(data string, socket gowebsocket.Socket) {
log.Println("Recieved ping " + data)
log.Println("Received ping " + data)
}
socket.OnDisconnected = func(err error, socket gowebsocket.Socket) {
log.Println("Disconnected from server ")
return
}
socket.Connect()

Expand All @@ -48,6 +48,7 @@ func main() {
i++
}

// nolint: gosimple
for {
select {
case <-interrupt:
Expand Down
41 changes: 28 additions & 13 deletions gowebsocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,37 @@ package gowebsocket
import (
"crypto/tls"
"errors"
"github.com/gorilla/websocket"
"github.com/sacOO7/go-logger"
"fmt"
"net/http"
"net/url"
"reflect"
"sync"
"time"

"github.com/gorilla/websocket"
logging "github.com/sacOO7/go-logger"
)

type Empty struct {
type empty struct {
}

var logger = logging.GetLogger(reflect.TypeOf(Empty{}).PkgPath()).SetLevel(logging.OFF)
var logger = logging.GetLogger(reflect.TypeOf(empty{}).PkgPath()).SetLevel(logging.OFF)

// EnableLogging enables the logger
func (socket Socket) EnableLogging() {
logger.SetLevel(logging.TRACE)
}

// GetLogger gets the logger object
func (socket Socket) GetLogger() logging.Logger {
return logger
}

// Socket provides a websocket request
type Socket struct {
Conn *websocket.Conn
WebsocketDialer *websocket.Dialer
Url string
URL string
ConnectionOptions ConnectionOptions
RequestHeader http.Header
OnConnected func(socket Socket)
Expand All @@ -44,20 +49,23 @@ type Socket struct {
receiveMu *sync.Mutex
}

// ConnectionOptions contains connection options
type ConnectionOptions struct {
UseCompression bool
UseSSL bool
Proxy func(*http.Request) (*url.URL, error)
Subprotocols []string
}

// todo Yet to be done
// ReconnectionOptions provides options for reconnecting to the websocket
// TODO Yet to be done
type ReconnectionOptions struct {
}

// New creates a new websocket for the given url
func New(url string) Socket {
return Socket{
Url: url,
URL: url,
RequestHeader: http.Header{},
ConnectionOptions: ConnectionOptions{
UseCompression: false,
Expand All @@ -77,16 +85,17 @@ func (socket *Socket) setConnectionOptions() {
socket.WebsocketDialer.Subprotocols = socket.ConnectionOptions.Subprotocols
}

// Connect connects to the websocket server
func (socket *Socket) Connect() {
var err error
var resp *http.Response
socket.setConnectionOptions()

socket.Conn, resp, err = socket.WebsocketDialer.Dial(socket.Url, socket.RequestHeader)
socket.Conn, resp, err = socket.WebsocketDialer.Dial(socket.URL, socket.RequestHeader)

if err != nil {
logger.Error.Println("Error while connecting to server ", err)
logger.Error.Println("HTTP Response %d status: %s", resp.StatusCode, resp.Status)
logger.Error.Println(fmt.Sprintf("HTTP Response %d status: %s", resp.StatusCode, resp.Status))
socket.IsConnected = false
if socket.OnConnectError != nil {
socket.OnConnectError(err, *socket)
Expand Down Expand Up @@ -134,19 +143,22 @@ func (socket *Socket) Connect() {
for {
socket.receiveMu.Lock()
if socket.Timeout != 0 {
socket.Conn.SetReadDeadline(time.Now().Add(socket.Timeout))
err := socket.Conn.SetReadDeadline(time.Now().Add(socket.Timeout))
if err != nil {
logger.Error.Println(err)
}
}
messageType, message, err := socket.Conn.ReadMessage()
socket.receiveMu.Unlock()
if err != nil {
logger.Error.Println("read:", err)
logger.Error.Println(fmt.Sprintf("read: %s", err))
if socket.OnDisconnected != nil {
socket.IsConnected = false
socket.OnDisconnected(err, *socket)
}
return
}
logger.Info.Println("recv: %s", message)
logger.Info.Println(fmt.Sprintf("recv: %s", message))

switch messageType {
case websocket.TextMessage:
Expand All @@ -162,6 +174,7 @@ func (socket *Socket) Connect() {
}()
}

// SendText sends a test message to the server
func (socket *Socket) SendText(message string) {
err := socket.send(websocket.TextMessage, []byte(message))
if err != nil {
Expand All @@ -170,6 +183,7 @@ func (socket *Socket) SendText(message string) {
}
}

// SendBinary sends a binary message to the websocket
func (socket *Socket) SendBinary(data []byte) {
err := socket.send(websocket.BinaryMessage, data)
if err != nil {
Expand All @@ -185,12 +199,13 @@ func (socket *Socket) send(messageType int, data []byte) error {
return err
}

// Close closese the websocket
func (socket *Socket) Close() {
err := socket.send(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
logger.Error.Println("write close:", err)
}
socket.Conn.Close()
_ = socket.Conn.Close()
if socket.OnDisconnected != nil {
socket.IsConnected = false
socket.OnDisconnected(err, *socket)
Expand Down
5 changes: 3 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"net/url"
)

func BuildProxy(Url string) func(*http.Request) (*url.URL, error) {
uProxy, err := url.Parse(Url)
// BuildProxy creates an http proxy
func BuildProxy(rawURL string) func(*http.Request) (*url.URL, error) {
uProxy, err := url.Parse(rawURL)
if err != nil {
log.Fatal("Error while parsing url ", err)
}
Expand Down

0 comments on commit b41b93f

Please sign in to comment.