Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gofmt #12

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions examples/gowebsocket/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"log"
"github.com/sacOO7/gowebsocket"
"log"
"os"
"os/signal"
)
Expand All @@ -11,12 +11,12 @@ func main() {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)

socket := gowebsocket.New("ws://echo.websocket.org/");
socket := gowebsocket.New("ws://echo.websocket.org/")
socket.ConnectionOptions = gowebsocket.ConnectionOptions{
//Proxy: gowebsocket.BuildProxy("http://example.com"),
UseSSL: false,
UseCompression: false,
Subprotocols: [] string{"chat", "superchat"},
Subprotocols: []string{"chat", "superchat"},
}

socket.RequestHeader.Set("Accept-Encoding", "gzip, deflate, sdch")
Expand All @@ -26,24 +26,24 @@ func main() {

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

i := 0
for (i < 10) {
for i < 10 {
socket.SendText("This is my sample test message")
i++
}
Expand Down
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/sacOO7/gowebsocket

go 1.16

require (
github.com/gorilla/websocket v1.4.2
github.com/sacOO7/go-logger v0.0.0-20180719173527-9ac9add5a50d
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/sacOO7/go-logger v0.0.0-20180719173527-9ac9add5a50d h1:5T+fbRuQbpi+WZtB2yfuu59r00F6T2HV/zGYrwX8nvE=
github.com/sacOO7/go-logger v0.0.0-20180719173527-9ac9add5a50d/go.mod h1:L5EJe2k8GwpBoGXDRLAEs58R239jpZuE7NNEtW+T7oo=
24 changes: 12 additions & 12 deletions gowebsocket.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package gowebsocket

import (
"crypto/tls"
"errors"
"github.com/gorilla/websocket"
"github.com/sacOO7/go-logger"
"net/http"
"errors"
"crypto/tls"
"net/url"
"sync"
"github.com/sacOO7/go-logger"
"reflect"
"sync"
"time"
)

Expand All @@ -22,7 +22,7 @@ func (socket Socket) EnableLogging() {
}

func (socket Socket) GetLogger() logging.Logger {
return logger;
return logger
}

type Socket struct {
Expand All @@ -33,7 +33,7 @@ type Socket struct {
RequestHeader http.Header
OnConnected func(socket Socket)
OnTextMessage func(message string, socket Socket)
OnBinaryMessage func(data [] byte, socket Socket)
OnBinaryMessage func(data []byte, socket Socket)
OnConnectError func(err error, socket Socket)
OnDisconnected func(err error, socket Socket)
OnPingReceived func(data string, socket Socket)
Expand All @@ -48,7 +48,7 @@ type ConnectionOptions struct {
UseCompression bool
UseSSL bool
Proxy func(*http.Request) (*url.URL, error)
Subprotocols [] string
Subprotocols []string
}

// todo Yet to be done
Expand All @@ -57,7 +57,7 @@ type ReconnectionOptions struct {

func New(url string) Socket {
return Socket{
Url: url,
Url: url,
RequestHeader: http.Header{},
ConnectionOptions: ConnectionOptions{
UseCompression: false,
Expand All @@ -78,7 +78,7 @@ func (socket *Socket) setConnectionOptions() {
}

func (socket *Socket) Connect() {
var err error;
var err error
var resp *http.Response
socket.setConnectionOptions()

Expand Down Expand Up @@ -163,22 +163,22 @@ func (socket *Socket) Connect() {
}

func (socket *Socket) SendText(message string) {
err := socket.send(websocket.TextMessage, [] byte (message))
err := socket.send(websocket.TextMessage, []byte(message))
if err != nil {
logger.Error.Println("write:", err)
return
}
}

func (socket *Socket) SendBinary(data [] byte) {
func (socket *Socket) SendBinary(data []byte) {
err := socket.send(websocket.BinaryMessage, data)
if err != nil {
logger.Error.Println("write:", err)
return
}
}

func (socket *Socket) send(messageType int, data [] byte) error {
func (socket *Socket) send(messageType int, data []byte) error {
socket.sendMu.Lock()
err := socket.Conn.WriteMessage(messageType, data)
socket.sendMu.Unlock()
Expand Down
2 changes: 1 addition & 1 deletion utils.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package gowebsocket

import (
"log"
"net/http"
"net/url"
"log"
)

func BuildProxy(Url string) func(*http.Request) (*url.URL, error) {
Expand Down